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

Local macro is "disappearing" when used in a loop

I have a small code snippet where I have defined a local macro i to be used in a forvalues loop: matrix I=I(4) scalar x1=b[1,1] scalar sum1=0 local i = 2 forvalues i = 2/4 { scalar chk=i-1 forvalues j = 1/`=chk' { scalar…
PGupta
  • 183
  • 1
  • 7
0
votes
1 answer

Using cell value in formula

If I have a variable in Stata that has a number, can I use that value embedded in a formula? For example, I variable days (days) and for each individual I want to use that number in a second formula: ID Days var_x_1 var_x_2 var_x_3 …
CJ12
  • 487
  • 2
  • 10
  • 28
0
votes
1 answer

Including a variable local macro inside a loop

Using a forvalues loop, I am merging a list of 400 individual datasets. These datasets can be one of 10 distinct values (defined by a variable in the dataset): depending on the dataset, I would merge with a different dataset. For example, if Player…
0
votes
1 answer

How to carriage return a long local list and how to define list only once

My first question is simple, but cannot find any answer anywhere and it's driving me crazy: When defining a local list in Stata how do I do a carriage return if the list is really long? The usual /// doesn't work when inside double quotations…
ben
  • 787
  • 3
  • 16
  • 32
-1
votes
1 answer

Conditional Division in Stata

I have a dataset that looks like the following: year Gender VP 2023 Total 3 2023 F 2 2023 M 1 2022 Total …
Shubhi
  • 1
  • 1
-1
votes
1 answer

How to subset data using local macros

I have some code that uses local macros in Stata to subset an aggregate data set into quantiles by asset size. That code looks like this: local quantile 0 25 50 75 99 foreach quantile in `quantile' { preserve …
gobygoul
  • 5
  • 3
-1
votes
1 answer

I'm trying to convert birth year written as '11jun1965' to age variable in Stata. With the following code, I could not generate the current age

I used the following code but all I got was '.' for all age categories gen agenew=int((birthyear)/365.25) gen agenew = date("7/16/2020", "MDY") - date("birthyear", "MDY") replace agenew=agenew/365.25 replace agenew=floor(agenew)```
san
  • 1
  • 2
-1
votes
1 answer

Error merging files: . Error says that variable CLAIM_NUMBER does not uniquely identify observations in the using data. How do I fix that?

In Stata, the error says that variable CLAIM_NUMBER does not uniquely identify observations in the using data. How do I fix that? My code: cd"abcd" use NY2019_2021 merge m:1 CLAIM_NUMBER using FULLNY keep if _merge==3 drop _merge```
san
  • 1
  • 2
-1
votes
1 answer

How can I generate AGE categorical/binary variable?

I tried using the below code but it throws an error in Stata. gen agecat=. replace agecat=1 if age<=20 replace agecat=2 if age==21-30 replace agecat=2 if age==31-40 replace agecat=2 if age>=64
san
  • 1
  • 2
-1
votes
1 answer

Random Assigning Treatment in Stata

I need help with random assigning treatments in Stata, I am lost. The variable is assigned to be treated with at 45% probability. After the variable has been assigned to be treated, I have to pick the treatment date by randomly drawing a specific…
ll54
  • 3
  • 3
-1
votes
1 answer

Referring to macros within a loop in Stata

I am trying to refer to a macro inside a loop in Stata. A simple example below. local x1 age race gender local x2 age race gender city local nums 1 2 foreach i of local nums{ reg y $x`i' } I'd like the loop to go through two calues of…
Econ_183
  • 1
  • 1
-1
votes
1 answer

Listing variable name, label, and all value labels in one line

I want to list a variable's name, label, as well as all values and value labels on one line, for each variable in the dataset. For example: foreign, Car type, 0 Domestic 1 Foreign I managed to write some code to do this, but it only shows value…
Stalfos
  • 1
  • 2
-1
votes
1 answer

Loop for recoding years

I would like to recode my variable named as Year using a for a loop and create a new variable named as year in Stata. What I am looking for should shorten below-mentioned code: recode Year (5 = 1960) (6 = 1961)(7 = 1962)(8 = 1963)(9 = 1964) (10 =…
Cenk
  • 325
  • 4
  • 16
-1
votes
2 answers

Inheriting looping variable or local, global macros

main.do is: foreach mode in mode1 mode2 { do run.do } and run.do is: foreach y in y1 y2{ reg `y' x outreg2 using `y'.xls, append ctitle(`mode') } It has outreg2, so it produced a txt output. But I found that the column title is empty meaning…
user42459
  • 883
  • 3
  • 12
  • 29
-1
votes
1 answer

Loop with output save for levels of factor variable

I'm working with a large dataset (18 million observations). I have one continuous variable X in particular that I need to analyze, by a factor variable with about 6k levels. I'd like help in creating a loop function that for each level of the factor…
1 2 3
11
12