Example: tourism industry

SyntaxDescriptionRemarks and examplesAlso see

Macro definition and manipulationSyntaxDescriptionRemarks and examplesAlso seeSyntaxglobalmname[=exp|:extendedfcn|" [string]"| "[string]" ]locallclname[=exp|:extendedfcn|"[string ]"| "[string]" ]tempvarlclname[lclname[..]]tempnamelcln ame[lclname[..]]tempfilelclname[lclname[ ..]]local{++lclname|--lclname}macro dirmacro drop{mname[mname[..]]|mname*|all}macro list[mname[mname[..]]|all]macro shift[#][..] expansionoptr [..]whereexpansionoptrislclname|++lclnam e|lclname++|--lclname|lclname--|=exp|:ex tendedfcn|.classdirective|macval(lclname )and whereextendedfcnis any of the following:Macro extended function for extracting program propertiespropertiescommand12 macro Macro definition and manipulationMacro extended functions for extracting data attributes{type|format|value label|variable label}varnamedata labelsortedbylabel{valuelabelname|(varna me)}{maxlength|#[#2]}[, strict]constraint{#|dir}char{varname[ ]|varname[charname]}orchar{dta[ ]|dta[charname]}Macro extended function for naming variablespermnamesuggestedname[, length(#)]

macro— Macro definition and manipulation 3 Macro extended functions related to matrices rownamesjcolnamesjrowfullnamesjcolfullnames matname roweqjcoleq

Tags:

  Syntaxdescriptionremarks and examplesalso see, Syntaxdescriptionremarks, Examplesalso

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of SyntaxDescriptionRemarks and examplesAlso see

1 Macro definition and manipulationSyntaxDescriptionRemarks and examplesAlso seeSyntaxglobalmname[=exp|:extendedfcn|" [string]"| "[string]" ]locallclname[=exp|:extendedfcn|"[string ]"| "[string]" ]tempvarlclname[lclname[..]]tempnamelcln ame[lclname[..]]tempfilelclname[lclname[ ..]]local{++lclname|--lclname}macro dirmacro drop{mname[mname[..]]|mname*|all}macro list[mname[mname[..]]|all]macro shift[#][..] expansionoptr [..]whereexpansionoptrislclname|++lclnam e|lclname++|--lclname|lclname--|=exp|:ex tendedfcn|.classdirective|macval(lclname )and whereextendedfcnis any of the following:Macro extended function for extracting program propertiespropertiescommand12 macro Macro definition and manipulationMacro extended functions for extracting data attributes{type|format|value label|variable label}varnamedata labelsortedbylabel{valuelabelname|(varna me)}{maxlength|#[#2]}[, strict]constraint{#|dir}char{varname[ ]|varname[charname]}orchar{dta[ ]|dta[charname]}Macro extended function for naming variablespermnamesuggestedname[, length(#)]

2 ]Macro extended functions for filenames and file pathsadosubdir["]filename["]dir["]dir["] {files|dirs|other}["]pattern["][, nofail respectcase]sysdir[STATA|BASE|SITE|PLUS| PERSONAL|dirname]Macro extended function for accessing operating-system parametersenvironmentnameMacro extended functions for names of stored resultse(scalars|macros|matrices|functio ns)r(scalars|macros|matrices|functions)s (macros)all{globals|scalars|matrices}["p attern"]all{numeric|string}scalars["patt ern"]Macro extended function for formatting resultsdisplay..Macro extended function for manipulating listslist..macro Macro definition and manipulation 3 Macro extended functions related to matrices{rownames|colnames|rowfullnames| colfullnames}matname{roweq|coleq}matname [, quoted]Macro extended function related to time-series operatorstsnormstring[, varname]Macro extended function for copying a macrocopy{local|global}macnameMacro extended functions for parsingword{count|#of}stringpiece#piecen umber #lengthofpiecesof[ ]"string"[ ][, nobreak]length{local|global}macnamesubin str{globalmname2|locallclname2}{"from"| "from" }{"to"| "to" }[, all count(globalmname3|locallclname3) word]Descriptionglobalassigns strings to specified global macro names (mnames).

3 Localassigns strings to localmacro names (lclnames). Both double quotes ("and") and compound double quotes ( "and" ) areallowed; see[U] Double quotes. If thestringhas embedded quotes, compound double quotesare names to the specified local macro names that may be used as temporary variablenames in the dataset. When the program or do-file concludes, any variables with these assigned namesare names to the specified local macro names that may be used as temporary scalaror matrix names. When the program or do-file concludes, any scalars or matrices with these assignednames are names to the specified local macro names that may be used as names fortemporary files. When the program or do-file concludes, any datasets created with these assignednames are global and local [U] Macrosfor information on macro macro Macro definition and manipulationRemarks and are presented under the following headings.

4 Formal definition of a macroGlobal and local macro namesMacro assignmentMacro extended functionsMacro extended function for extracting program propertiesMacro extended functions for extracting data attributesMacro extended function for naming variablesMacro extended functions for filenames and file pathsMacro extended function for accessing operating-system parametersMacro extended functions for names of stored resultsMacro extended function for formatting resultsMacro extended function for manipulating listsMacro extended functions related to matricesMacro extended function related to time-series operatorsMacro extended function for copying a macroMacro extended functions for parsingMacro expansion operators and functionThe tempvar, tempname, and tempfile commandsTemporary variablesTemporary scalars and matricesTemporary filesManipulation of macrosMacros as argumentsMacros are a tool used in programming Stata, and this entry assumes that you have read[U] 18 Pro-gramming Stataand especially[U] Macros.

5 This entry concerns advanced issues not definition of a macroAmacrohas amacro nameandmacro contents. Everywhere a punctuated macro name appears ina command punctuation is defined below the macro contents are substituted for the macro come in two types, global and local. Macro names are up to 32 characters long for globalmacros and up to 31 characters long for local macros. The contents of global macros are defined withtheglobalcommand and those of local macros with thelocalcommand. Global macros, oncedefined, are available anywhere in Stata. Local macros exist solely within the program or do-file inwhich they are defined. If that program or do-file calls another program or do-file, the local macrospreviously defined temporarily cease to exist, and their existence is reestablished when the callingprogram regains control.

6 When a program or do-file ends, its local macros are permanently substitute the macro contents of a global macro name, the macro name is typed (punctuated)with a dollar sign ($) in front. To substitute the macro contents of a local macro name, the macro nameis typed (punctuated) with surrounding left and right single quotes ( ). In either case, braces ({ })can be used to clarify meaning and to form nested constructions. When the contents of an undefinedmacro are substituted, the macro name and punctuation are removed, and nothing is substituted in Macro definition and manipulation 5 For example,The input..is equivalent to..global a "myvar"gen $a = oldvar gen myvar = oldvargen a = oldvar gen a = oldvarlocal a "myvar"gen a = oldvar gen myvar = oldvargen a = oldvar gen a = oldvarglobal a "newvar"global i = 2gen $a$i = oldvar gen newvar2 = oldvarlocal a "newvar"local i = 2gen a i = oldvar gen newvar2 = oldvarglobal b1 "newvar"global i=1gen ${b$i} = oldvar gen newvar = oldvarlocal b1 "newvar"local i=1gen b i = oldvar gen newvar = oldvarglobal b1 "newvar"global a "b"global i = 1gen ${$a$i} = oldvar

7 Gen newvar = oldvarlocal b1 "newvar"local a "b"local i = 1gen a i = oldvar gen newvar = oldvarGlobal and local macro namesWhat we say next is an exceedingly fine point: global macro names that begin with an underscoreare really local macros; this is why local macro names can have only 31 characters. Thelocalcommand is formally defined as equivalent toglobal. Thus the following are equivalent:local x globalxlocal i=1 globali=1local name "Bill" globalname "Bill"local fmt : format myvar globalfmt : format myvarlocal 3 2 global3 $2tempvaris formally defined as equivalent tolocalname: tempvarfor each name specifiedaftertempvar.

8 Thustempvar a b cis equivalent tolocal a : tempvarlocal b : tempvarlocal c : tempvarwhich in turn is equivalent toglobal _a : tempvarglobal _b : tempvarglobal _c : tempvartempfileis defined macro Macro definition and manipulationMacro assignmentWhen you type. local name "something"or. local name "something" somethingbecomes the contents of the macro. The compound double quotes ( "and" ) are neededwhensomethingitself contains quotation marks. When you type. local name =somethingsomethingis evaluated as an expression, and the result becomes the contents of the macro. Note thepresence and lack of the equal sign. That is, if you type. local problem "2+2". local result = 2+2thenproblemcontains2+2, , when you type.

9 Local name :somethingsomethingis interpreted as an extended macro function. (Note the colon rather than nothing or theequal sign.) Of course, all of this applies toglobalas well as ++lclname, orlocal --lclname, is used to increment, or decrement, instance, typing. local ++xis equivalent to typing. local x = x + 1 Macro extended functionsMacro extended functions are of the form. localmacname:..For instance,. local x : type mpg. local y : matsize. local z : display % sqrt(2)We document the macro extended functions below. Macro extended functions are typically used inprograms, but you can experiment with them interactively. For instance, if you are unsure what localx : type mpg does, you could type.

10 Local x : type mpg. display " x "intmacro Macro definition and manipulation 7 Macro extended function for extracting program propertiespropertiescommandreturns the properties declared forcommand; see [P]program extended functions for extracting data attributestypevarnamereturns the storage type ofvarname, which might beint,long,float,double,str1,str2, the display format associated withvarname, for instance,% labelvarnamereturns the name of the value label associated withvarname, which might be (meaning nolabel), or, for example,make, meaning that the value label s name labelvarnamereturns the variable label associated withvarname, which might be (meaning no label), or, forexample,Repair Record labelreturns the dataset label associated with the dataset currently in memory, which might be (meaning no label)