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
1
vote
1 answer

Comparing elements of two different local macros

I have a local macro called peer_list that contains 280 distinct elements, all of which are strings. I also have another local macro called used_list that contains a subset of the elements contained in the local peer_list. For each element in…
1
vote
2 answers

Local macros for subset of observations

I would like to create a local macro for a subset of my dataset to use for future regressions (see Some Uses for Macros Outside of Loops section). I've started off with code that is along the following lines: quietly reg y x1 x2 x3 local subset if…
Traci
  • 91
  • 1
  • 2
  • 6
1
vote
1 answer

"word # of string" idiom (part of extended string functions)

I'm trying to label variables in a forvalues loop on Stata version 11.2. However, I can't get the extended string function word # of string idiom to work: local names `""Growth" "Mature" "All""' forvalues i = 0/2 { local name : word `i' of…
Richard Herron
  • 9,760
  • 12
  • 69
  • 116
1
vote
1 answer

Using do-file with local macro

I am getting the following error in Stata while using a do file with a local macro: clear capture log close sysuse auto,clear log using stata,text replace local varlist price mpg rep78 weight length reg 'varlist' local close Output local…
Metrics
  • 15,172
  • 7
  • 54
  • 83
1
vote
2 answers

Mlogit macro with dummy variables

I am new to Stata and macros. I am trying to loop over several variables to generate estimates from the mlogit command and then save them in datasets. That portion is working well. The problem I have is a categorical variable that I need to split…
Maggie
  • 65
  • 6
0
votes
1 answer

Writing loop to retrieve multiple variables from an external Python database

I am using a Python package that allows me to integrate data retrieved in Python to be transferred to Stata. As I am doing this across 190 countries, it takes a long time to retrieve multiple variables when I call a command. I was hoping to write a…
0
votes
1 answer

How to get a graph bar with two categorical variables

I have this graph in Stata 14: wrong chart built with this data: data columns I used two commands to generate the last two variables to build the graph I want: // Command 1 gen mayores_65=0 if Edad>=65 & ID !=. replace mayores_65=1 if Edad<65 label…
0
votes
1 answer

Loop over a different set of control variables in stata

I am trying to loop over a different set of control variables. I run the below code but get an error { required. Any help would be appreciated. local tmax "tmax_3 tmax_2 tmax_1 tmax0 tmax1 tmax2 tmax3" local tmin "tmin_3 tmin_2 tmin_1 tmin0 tmin1…
0
votes
1 answer

meglm in foreach loop keeps giving "factor-variable operators not allowed" error however meglm works perfectly outside foreach loop

I have a meglm statement with multiple binomial outcomes and I am trying to put it in a foreach loop but whenever I run this code, it gives me the error message "factor-variable operators not allowed": foreach variable of varlist burn_post2 { …
0
votes
1 answer

How to use linear regression B/SE estimates into a new formula to calculate Wald Ratio method statistic?

In Stata how to perform one-sample Mendelian randomization using the Wald ratio method for causal estimates, SE and P value? Is there a package or special command? Essentially I want to perform regression of A on X, and then regression of B on X and…
0
votes
1 answer

Contour plot with survey weights

I am analyzing NHANES data (https://wwwn.cdc.gov/nchs/nhanes/) which use a complex, multistage, probability sampling design. I want to plot some data in Stata using a contour plot. With non-weighted data, I would use the following command twoway…
Dr.M
  • 101
  • 1
0
votes
2 answers

Export browse window to MS Word using Stata asdoc

How can I export the contents of the Stata browse window to MS Word? I'm currently using the asdoc command. However, any method outside "copy and paste" is acceptable. my attempt ssc install asdoc, replace sysuse auto asdoc, row( make, price, mpg,…
24thDan
  • 113
  • 1
  • 9
0
votes
2 answers

Macro within a loop and include

In Stata, I'm trying to use the command include to run several regressions in one do file. The overall goal is to help in forecasting Natural Gas production. I have a do file for each product and basin that I'm interested in. Within each do file I…
0
votes
1 answer

Group variable at a class level repeated across observations converted to a smaller dataset in which the observations are the class

In Stata, I created the average grade in course i of each class in every school in time t (using bysort, egen). Now I have a repeated group variable at a class level repeated across observations. How do I change to a smaller dataset in which the…
RMLP
  • 3
  • 2
0
votes
1 answer

Stata - Generate all possible combinations

I need to find all possible combinations of the following variables, each containing a X number of observations Variable Obs Black 1 Pink 2 Yellow 6 Red 15 Green 17 e.g. (black, pink), (black, pink, yellow), (black, pink, yellow, red), (red,…
user19745561
  • 145
  • 10