Questions tagged [stata-macros]

A macro in Stata is comprised of a name and contents. When a properly punctuated macro name appears in a command or script, its contents are expanded and replace the macro name.

Macros in Stata can be distinguished in two flavours: global and local. Both are defined with the commands of the same name. The contents of the former can be expanded anywhere. In contrast, the latter are only available within a program or script where they were defined.

The chapter Macro definition and manipulation in the manual offers more detailed information.

For questions involving the use of macros in the context of Stata, please use the tag in addition to the main tag.

171 questions
0
votes
1 answer

Loop for changing date format and generating graphs

I would like to loop through a number of csv files in order to change the format of date variables and generate graphs each with a title that is simply the name of the respective file. Here is an example of one of the csv files created using…
ababuji
  • 1,683
  • 2
  • 14
  • 39
0
votes
1 answer

How to include interaction term in global macro for use in the reghdfe command?

I want to create an interaction term in the community-contributed regdhdfe command. One of the two interaction variables is a continuous variable and used with a global. The other variable is a gender dummy. My code looks something like…
Timo K
  • 29
  • 6
0
votes
1 answer

Looping over many names which don't have rules

When there are just few names, looping in Stata is easy. Also, when there is a rule as to how the names change (e.g. increment) I can do the following: forval i = 1/5 { ... } However, there are cases where i have hundreds of names that I need…
user42459
  • 883
  • 3
  • 12
  • 29
0
votes
1 answer

Part of file name as a variable

I have a list of files with similar names, for example: 2002_file 2003_file 2004_file I am going to run a simple script: foreach year in 2002 2003 2004 2005 2006 2007 2008 2009 2011 2012 2013 2014 2015 { use "$input\`year'_file.dta ",…
j_3265
  • 207
  • 3
  • 12
0
votes
1 answer

File paths and local macros

Suppose that I am in a folder ~/top. There is a file path like this: ~/top/middle/bottom folder. I want to define a local macro and then use it in the file path. What I have done is the following: local target ""bottom folder"" cd "middle" cd…
TCW
  • 25
  • 9
0
votes
1 answer

Rename all variable with their label values

I would like to rename all variables in my Stata dataset with their label values automatically. Any idea of how this can be done? Example data: input str13 usa str9 v338 str13(fra gbr) "1.443888e+05" "0" "7.402382e+04" …
TeYaP
  • 303
  • 6
  • 21
0
votes
2 answers

Return a matrix from distinct command

I have a simple question about the distinct command in Stata. When using with a by prefix, can it return a one dimension matrix of r(N)? For example: sysuse auto,clear bysort foreign: distinct rep78 Can I store a [2,1] matrix, with each row…
Yan Song
  • 2,285
  • 4
  • 18
  • 27
0
votes
1 answer

Export regression results with categorical exposure variables

I would like to run several linear regressions using categorical exposure variables and to output the results to an excel sheet. The code below works fine when the exposure is continuous. However, for categorical exposures the code only outputs the…
aelhak
  • 441
  • 4
  • 14
0
votes
1 answer

Can the user decide whether to loop code or only run it once?

In my .do file, I am asking the user some questions about what regressions they want to run, using display _request(macroname). Then I interpret their answer (which is either yes or no in most cases) and define another macro that appears in the…
ahorn
  • 217
  • 2
  • 12
0
votes
1 answer

Displaying results from ranksum

I'm trying to modify the code posted by @Nick Cox in my previous question but I have some issues. I have set my varlist and my group variable. I have also changed the col option to fit my varname. I would like to add the number of observations for…
0
votes
2 answers

Keep a list of variables when some variables don't exist

For several countries, I have one dataset and I want to keep a list of variables (specified in a global vlist) for each of them. Some variables do not exist in some countries. I want Stata to ignore the non-existence of these variables and execute…
Timo K
  • 29
  • 6
0
votes
1 answer

A local within a local evaluates as empty in a program

I am trying to write a mini-program that simply takes a list of variables, and returns a sub-list that contains variables that are actually in the dataset. I do this many times in a do file (not sequentially, so no loops), so it is easier to just…
Aaron Wolf
  • 337
  • 3
  • 13
0
votes
1 answer

Find _n of Observation(s) that have a certain value

I want to find the observation numbers that correspond to the observations that have a particular value, say 29. I would then like to save these observation numbers in a macro. Is there a better way to do so than the following clunky and…
bill999
  • 2,147
  • 8
  • 51
  • 103
0
votes
1 answer

How to nest a global macro within a global macro in Stata?

I would like to put a global macro within a global macro in a Stata. For example, $hello$oneworld, which is the global macro $one inside the macro $helloworld. How do I get a global macro within a global macro?
Nicolas
  • 374
  • 4
  • 18
0
votes
2 answers

Stata input command not allowing local macros

I found this curious behavior in the input command for Stata. When you pass a local macro as an argument either for one variable or multiple, the input command gives this error: '`' cannot be read as a number Here are two examples that give the…
Eric HB
  • 867
  • 6
  • 17