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
9
votes
3 answers

Stata: Check if a local macro is undefined

I am writing a Stata do file and I would like to provide default values if the user does not supply some parameters. To do so, I woud like to check if a macro is undefined. I have come up with a hacky way to do this: *** For a local macro with the…
Wilduck
  • 13,822
  • 10
  • 58
  • 90
7
votes
2 answers

How do I create a "macro" for regressors in R?

For long and repeating models I want to create a "macro" (so called in Stata and there accomplished with global var1 var2 ...) which contains the regressors of the model formula. For example from library(car) lm(income ~ education + prestige, data…
jay.sf
  • 60,139
  • 8
  • 53
  • 110
6
votes
4 answers

R equivalent of Stata's for-loop over local macro list of stubnames

I'm a Stata user that's transitioning to R and there's one Stata crutch that I find hard to give up. This is because I don't know how to do the equivalent with R's "apply" functions. In Stata, I often generate a local macro list of stubnames and…
esiotrot
  • 125
  • 2
  • 6
6
votes
5 answers

How to rename many variables with string suffixes

In Stata, I have a set of variables that all begin with pkg. In their current state, their endings are numeric: pkg1, pkg2, pkg3, pkg4 and so on. I need to change all of these variables' endings to strings: pkgmz, pkggmz, pkgsp, pkgsptc etc. I…
ben
  • 787
  • 3
  • 16
  • 32
5
votes
4 answers

Tabulate multiple variables with common prefix using a local macro

I have a number of variables whose name begins with the prefix indoor. What comes after indoor is not numeric (that would make everything simpler). I would like a tabulation for each of these variables. My code is the following: local indoor…
John Wyclif
  • 65
  • 1
  • 1
  • 5
5
votes
3 answers

R equivalent of Stata local or global macros

I am a Stata user trying to learn R. I have a couple of lengthy folder paths which, in my Stata code, I stored as local macros. I have multiple files in both those folders to use in my analysis. I know, in R, I can change the working directory…
user2012406
  • 147
  • 3
  • 9
4
votes
1 answer

Stata type mismatch with local macro?

I'm trying to create a loop in Stata to run a bunch of regressions. This is a very simplified version of how it looks: foreach y of local years{ forval i=1/50 { local cov `: word `i' of `covariates'' local int `: word `i' of…
4
votes
1 answer

Stata: Creating Variables Based on local varlist

I have a data set that looks like this: A B 0 1 0 1 0 1 1 0 I want to create new variables A't' and B't' for t=1,2,3 that give A and B values for the past 1,2 and 3 periods. I tried the following code but I get the error: "A invalid name. local…
4
votes
1 answer

How to keep a list of variables given some of them may not exist?

I have 100 dta files. I have a list of variables that I need to keep and save temporary copies on the fly. Some variables may or may not exist in a certain dta. I need Stata to keep all variables that exist in a dta and ignore those that do not…
CHEBURASHKA
  • 1,623
  • 11
  • 53
  • 85
3
votes
1 answer

How to get number of items (length) from a Stata macro?

Say I have a global macro x, how do I extract the number of items (space-separated unquoted words) in it? global x abc xyz foo I am sure it is an easy task, but I've looked in help macro and haven't find anything so far.
Marcelo Avila
  • 2,314
  • 1
  • 14
  • 22
3
votes
2 answers

Drop variables with all missing values

I have 5000 variables and 91,534 observations in my dataset. I want to drop all variables that have all their values missing: X1 X2 X3 1 2 . . 3 . 3 . . . 5 . X1 X2 1 2 . 3 3 …
JodeCharger100
  • 903
  • 1
  • 12
  • 25
3
votes
2 answers

Plot only interaction terms in coefplot

After a regression in Stata, I am trying to plot only the coefficients of the interaction terms. I was unable to do this using the community-contributed command coefplot. Here is a reproducible example and my attempted solutions: sysuse auto,…
LucasMation
  • 2,408
  • 2
  • 22
  • 45
3
votes
1 answer

How to separate Stata macro `varlist' with commas for using in mi( ) and inlist( )?

I want to store a list of variables in a macro and then call that macro inside a mi() statement. The original application is for a programme that uses data I cannot bring online for secrecy reasons, and which will include the following statement:…
Kenji
  • 571
  • 4
  • 20
3
votes
1 answer

Is it possible to make Stata throw an error by default when a global macro is not defined, instead of a missing string?

A feature of Stata that is sometimes inconvenient is that calling a non-defined macro returns the missing value . [edit: Stata actually returns a missing string "", not a numerical missing value], instead of throwing an error. A piece of code,…
Giuseppe
  • 518
  • 10
  • 22
3
votes
1 answer

Append local macro in Stata

In Stata, I want to explore regressions with many combinations of different dependent and independent variables. For this, I decided to use a loop that does all these regressions, and then saves the relevant results (coefficients, R2, etc.) in a…
Marie. P.
  • 255
  • 1
  • 4
  • 17
1
2 3
11 12