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

generate new variable using parallel lists in Stata

I'm trying to generate a new set of variables from two parallel lists in Stata. I have a set of columns that are the percentage of each demographic group in the labor force and another set of columns that are the percentage of each demographic group…
anastasia
  • 3
  • 1
0
votes
2 answers

Storing AC results from correlogram

I am having issues in storing the results of auto-correlations: sysuse sp500.dta tsset date corrgram open di `r(ac10)' di `r(ac11)' As you see, the corrgram command opens a table with AC, PAC, Q etc. I wish to store the data, but somehow I cannot…
0
votes
2 answers

Grouping observations with same ID

I've taken a project in which the dataset separates the patient information and the disease observation, but the two observations have the same ID. Example: ID: 12345 Patient age: 23, patient weight: 55, patient height: 180 ID: 12345 Disease…
user3078100
  • 169
  • 4
  • 12
0
votes
2 answers

Renaming long list of variables with loop in Stata

I am trying to write a code in Stata. I am currently working with a data set with the unit of observation as households. There are also variables for each member of the household e.g hv101_01 for person one in the household, hv101_02 for the second…
Oyin
  • 11
  • 1
  • 4
0
votes
1 answer

Saving category names of multinomial outcome in model results

I run multinomial logistic regression models in Stata and export the results using the code below: sysuse auto.dta, clear describe summ postfile temp str100 exp str40 outcome adjust N beta se lci uci pval using /// "\test_multinom_models.dta",…
aelhak
  • 441
  • 4
  • 14
0
votes
1 answer

Preseting mandatory string options in syntax with default value

Consider the following toy program: program define table_v1.01, rclass version 15 syntax varlist(fv min = 1 max = 1) [if] [in], pat_ID(string) = Patient_ID end I would like to make the option pat_ID compulsory. In addition, since 90% of the…
khoshnaw
  • 53
  • 9
0
votes
1 answer

How to set a specific file order for each file in a local macro

I have a loop in Stata referring to a local macro which I use to run several actions (create a file, run script associated with each file etc.). Everything is fine but I would like to change the order in which the files are read in the loop, which…
ggg
  • 73
  • 1
  • 7
0
votes
1 answer

How to store strings with apostrophes in a local macro

I want to create a local macro (phrases) that stores a number of phrases: "I'm sorry" "I can help" "so what" Evidently, some of these phrases have apostrophes in them and I do not know how to store them. I have tried the following: local…
trinitysara
  • 175
  • 1
  • 8
0
votes
1 answer

Extracting random integers from a given variable

My task is picking 3 years randomly within a time interval 500 times for simulation purpose. More specifically,I want to select 3 random years from 2007 to 2016 (10 years), for example 2008, 2012 and 2014. So it's more or less equivalent to …
zlqs1985
  • 509
  • 2
  • 8
  • 25
0
votes
1 answer

Use putexcel to insert a string in Excel

I am trying to make an easy to use do file where the user inserts the names of the towns s/he wants to summarize and then Stata: summarizes the towns saves the results in an Excel file exports the names of the towns summarized I am using a list…
0
votes
1 answer

Why do I get an invalid syntax error with a foreach loop?

I want to rename variable names starting with intensity. I received an invalid syntax, r(198) error, with the following code. #delimit; foreach VAR of varlist intensity* {; local NEW = subinstr("`VAR'", "intensity", "int"); rename `VAR'…
Victor Wang
  • 765
  • 12
  • 26
0
votes
1 answer

Match values stored in local macros with those in a variable

In my data, I have a string variable ICD9CODX like the one below: |ICD9CODX| |:-------| |410 | |411 | |398 | |196 | I would like to search/match each observation within some code sets defined as Stata local macros. So if the code…
user159193
  • 21
  • 7
0
votes
1 answer

Save dta file with local and global variables in file name

In Stata, I try to save a dta file using local and global macros together in file names. However, the local macros are not parsed correctly. The global macro result is a folder directory which is: global result "C:\Users\...\....\..." The local…
Gen
  • 441
  • 4
  • 11
0
votes
1 answer

Is there a more efficient way to run three statsby regressions with different reference groups?

I want to run these three regressions. Note that each has a different reference group, which is why I run them separately. statsby _b, by(grp_iden) saving(reg_aaa.dta, replace): reg prezzo ib43.city_str i.marca_str, baselevels statsby _b,…
StatsScared
  • 517
  • 6
  • 20
0
votes
2 answers

Using a growth formula for grouped observations

I have a dataset which is shown below: clear input year price growth id 2008 5 -0.444 1 2009 . . 1 2010 7 -0.222 1 2011 9 0 1 2011 8 -0.111 1 2012 9 0 1 2013 11 0.22 1 2012 10 0 2 2013 12 0.2 2 2013…
Amin Karimi
  • 407
  • 2
  • 16