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

Automatic renaming of files

I am trying to batch rename many files within the same folder on my operating system. I have a dataset with two variables: oldname and newname. There are more file names in the dataset then actual files that need to be renamed so I would like to…
JRR
  • 578
  • 5
  • 21
1
vote
2 answers

Saving significance of a regression

I am running several simple regressions and I wish to save the value of the significance (P > |t|) of a regression for a given coefficient in a local macro. For example, I know that: local consCoeff = _b[_cons] will save the coefficient for the…
1
vote
1 answer

Bring R list into Stata as macro?

I wish to run a Lasso model in R from Stata and then bring a resulting character list (the names of the subset coefficients) back into Stata as a macro (for example, a global). At the moment I am aware of two options: I save a dta file and run an…
Leah Bevis
  • 317
  • 1
  • 11
1
vote
1 answer

Using local macros in names of global macros

How can I use local macros in the names of global macros in Stata 14? For example: global test1 = 250 local n = 1 . di $test1 // works 250 . di $test`n' // does not work (should be 250 and not 1) 1
Andreas
  • 33
  • 4
1
vote
2 answers

How do I confirm existence of all respective variables of a variable list?

Similar threads have not led me to solve the following problem. I use a local macro to specify a varlist with multiple variables and want to check whether each of the variables in this varlist exists in the dataset I use. So as to get a quick…
Timo K
  • 29
  • 6
1
vote
1 answer

Importing files with different months in name

I have the following code: local date "September" global dir `c(pwd)' global files "A B C" foreach x of global files { import excel "${path}`x'_`date'.xlsx", sheet("1") cellrange(A3:O21) clear generate Store="`x'" save `x',…
StatsScared
  • 517
  • 6
  • 20
1
vote
2 answers

Automated labeling of logarithmic plots

I would like to automate the graph axis values for a series of plots in Stata 13. In particular, I would like to show axis labels like 10^-1, 0, 10^1, 10^2 etc. by feeding the axis options macros. The solution in the following blog post gives a good…
1
vote
1 answer

Stata macro list uniq extended function (remove duplicates from a macro var list)

This question has been edited to add sample data and clean-up (hopefully) some unnecessary steps per feedback. I am starting with longitudinal data in wide format. I need to subset, reshape, and perform summary steps for multiple different chunks of…
jrc
  • 35
  • 1
  • 7
1
vote
1 answer

Adding time in a file name

Consider the code snippet below: local date: display %td_CCYY_NN_DD date(c(current_date), "DMY") local date_string = subinstr(trim("`date'"), " " , "", .) save "`date_string'_example", replace mkdir "`date_string'_example" This creates output as…
user42459
  • 883
  • 3
  • 12
  • 29
1
vote
1 answer

Does SPSS have functions similar to double/compound quotes in Stata?

Take an example when we have a series of 10 categorical variables var1, var2,..., var10 which take values from 1 to 5. We create 5 dummy variables from each of these variables. For example, from var1 we generate dumvar1_1,..., dumvar1_5. A dummy…
NonSleeper
  • 849
  • 1
  • 6
  • 17
1
vote
1 answer

How do I use a local macro referencing a variable?

I am trying to create a macro in Stata that will set a variable name for me to reference later on in my do file. Essentially, I am trying to replace a variable name with a local macro. An example dataset is the following: ID Indicator1 Indicator2 …
Kany
  • 39
  • 7
1
vote
1 answer

error when looping with macros in the indexing

I am trying to create a dummy variable to identify the next five observations after a selection of cutoffs. The first method in the code below works, but it looks a bit messy and I'd like to be able to adjust the number of observations I'm creating…
Susan
  • 101
  • 8
1
vote
4 answers

Using regular expressions or subinstr() to clean local macros

My aim is to clean a given local from _ and all numbers following the underscore at the end of the words. Assume that I have underscores followed by numbers at the end of the words only. By using subinstr(), I am able to specify that I want to…
Stefano Lombardi
  • 1,581
  • 2
  • 22
  • 48
1
vote
2 answers

How to get index of value in macro

I have a global macro: global global_macro sheep frog dragon I loop over this macro and I want to be able to generate three new variables based on the current index value of global_macro: foreach i in $global_macro { ***define local index*** …
bill999
  • 2,147
  • 8
  • 51
  • 103
1
vote
1 answer

Assigning a range of values to a macro

I am trying to rename a list of variables using a for loop. My code looks like this: local x 99 00 01 02 03 04 05 06 07 08 09 10 11 12 local k 2 4 6 8 10 12 14 16 18 20 22 24 26 28 local n: word count `x' forvalues i = 1 / `n' { local a :…
Rodrigo
  • 69
  • 5
  • 14