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
3
votes
1 answer

How to extract unique strings from a macro?

I'm trying to automate a reshape using Stata. I have a series of variables measured yearly. They are all named varname_yy, where yy is a number referring to the year of measurement. I managed to extract all the stubs varname_ from the variables and…
Kenji
  • 571
  • 4
  • 20
2
votes
1 answer

Outputting multiple regressions into a LaTeX document

Outreg2 is a community-contributed command, that helps us easily output the results of regressions run on Stata into a clean table, which can then be viewed in text, Word documents, or in LaTeX. Using the auto.dta dataset, I run the following…
JodeCharger100
  • 903
  • 1
  • 12
  • 25
2
votes
2 answers

Assign value to local variable with if-statement

I'm trying to assign a conditional value to a local macro variable in Stata 15. I have a local variable that only can have two values; "o" or "u". Then I have another local variable that I want to get the other letter of these two than the first…
MarJer
  • 87
  • 2
  • 9
2
votes
4 answers

What types of languages allow programmatic creation of variable names?

This question comes purely out of intellectual curiosity. Having browsed the Python section relatively often, I've seen a number of questions similar to this, where someone is asking for a programmatic way to define global variables. Some of them…
Wilduck
  • 13,822
  • 10
  • 58
  • 90
2
votes
1 answer

Equivalent of Stata macros in Python

I am trying to use Python for statistical analysis. In Stata I can define local macros and expand them as necessary: program define reg2 syntax varlist(min=1 max=1), indepvars(string) results(string) if "`results'" == "y" { reg…
user10162045
2
votes
1 answer

Preserving data more than once

I am writing some code in Stata and I have already used preserve once. However, now I would like to preserve again, without using restore. I know this will give an error message, but does it save up to the new preserve area?
Tommy Atlee
  • 23
  • 1
  • 4
2
votes
2 answers

Multiple local in foreach command macro

I have a dataset with multiple subgroups (variable economist) and dates (variable temps99). I want to run a tabsplit command that does not accept bysort or by prefixes. So I created a macro to apply my tabsplit command to each of my subgroups…
Homard
  • 39
  • 6
2
votes
2 answers

Find union of two variable-name scalars

I have a Stata program that outputs a local scalar of space-separated variable names. I have to run the program twice on two samples (same dta) and store the union (intersection - variable names appearing in both scalars) as a new space-separated…
km5041
  • 351
  • 1
  • 4
  • 13
2
votes
2 answers

Looping over variables with wildcards in local macros

Suppose I have a dataset of variables with the following names (note the stub of x and hm): x9, xdog, x_99, hma8j, hm40, hm0 I want to develop a programmatic way to provide a list of variable names (which may contain wildcards) and then loop…
QuestionAnswer
  • 321
  • 4
  • 15
2
votes
2 answers

Generate a new macro prefixing words of an old macro

Define a macro try: local try "a b c" This will generate a macro with the value a b c. Now I want a new macro try2 which takes the value da db dc. That is, I want to add the same prefix to each element of the old macro and define it as a new macro.
sxgn
  • 137
  • 7
2
votes
1 answer

When are global macros deleted?

I have a do file from which I would like to select a few lines of code and then run them using the Execute (do) button. The problem is that there are a bunch of local macros defined in this do file. I can run them, but then in the Stata command…
bill999
  • 2,147
  • 8
  • 51
  • 103
2
votes
0 answers

Macros constantly resulting in 'invalid name'

I'm a newcomer to Stata after using R for a long time. I'm trying to perform any action which involves a macro, such as a loop and it is constantly giving me an invalid name r(198) error. For example, when I first start up Stata, I enter the…
Mach
  • 73
  • 2
  • 3
  • 9
2
votes
3 answers

Macro equivalent of Stata

Can macros be used in R? I did look another question similar to this one, but couldn't understand it well. Let's say I want to create scalars, each with a different name and content. Basically, what I want to run in R can be illustrated by the…
PGupta
  • 183
  • 1
  • 7
2
votes
2 answers

How to manipulate filenames with a macro

I want to save the results of my Stata forvalues loop into individual files. One component of the filename should be the value j assigned to the macro within a forvalues loop. Apparently my code leads to an instruction always to save with 1995. As…
2
votes
2 answers

How to store a mean value in a local macro and then save it in another file?

I have a Stata file file1.dta and one of the variables is income. I need to calculate average_income, assign it to a local macro, and store in a different Stata file, New.dta. I have tried the following in a do file: #delimit; clear; set mem…
Buras
  • 3,069
  • 28
  • 79
  • 126
1
2
3
11 12