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

Is there a way to extract year range from wide data?

I have a series of wide panel datasets. In each of these, I want to generate a series of new variables. E.g., in Dataset1, I have variables Car2009 Car2010 Car2011 in a dataset. Using this, I want to create a variable HadCar2009, which is 1 if…
0
votes
0 answers

Stata Code related to the loop with bankscope

the code was ok, but in the second time is not working even though the variable is named differently[][Here is a picture of the error that I'm receiving]I'm working on bankscope database and there are some duplicates. Hence, I'm trying to overcome…
0
votes
2 answers

Why does Stata return “unknown function +inrange()”?

I am studying Stata programming with the book An Introduction to Stata Programming, Second Edition. In chapter 4 there is code to generate a variable that tests whether some other variables satisfy a logical condition, the code is like: foreach v of…
0
votes
1 answer

Storing a numerical variable in a local macro?

I have a variable called "count," which contains the number of subjects who attend each of 1300 study visits. I would like to store these values in a local macro and display them one by one using a for loop. E.g., local count_disp = count …
jos0909
  • 55
  • 5
0
votes
1 answer

Looping over macro of macros

I've defined a macro of macros: local my_macros "`macro1' `macro2' `macro3'" Each of the individual macros has a list of covariates, e.g. local macro1 "cov1 cov2 cov3" local macro2 "cov4 cov5 cov6" local macro3 "cov7 cov8 cov9" When I loop over…
0
votes
1 answer

How do you call an element in a local macro by index?

This might seem painfully easy, but say I create a Stata local macro called example: local example "blah1 blah2 blah3" I want to get just blah2 using the numerical index, in a way that might look like example[2] in another language. How does one…
0
votes
1 answer

How to reshape a specific dataset from long to wide without a J variable in Stata?

My dataset looks like the following: identification…
0
votes
2 answers

Invalid syntax loop in Stata

I'm trying to run a for loop to make a balance table in Stata (comparing the demographics of my dataset with national-level statistics) For this, I'm prepping my dataset and attempting to calculate the percentages/averages for some key…
Tara
  • 23
  • 3
0
votes
1 answer

How to hold estimations in memory and write to file in bulk

to speed up the code, I would like to export results to a file for every 10 regressions. Is it possible to do something like the following? forvalues i = 1(1)20{ ppmlhdfe y ${varlist`i'}, absorb(year) cluster(year) estimates store result`i' if…
Angel Chen
  • 67
  • 1
  • 2
  • 10
0
votes
0 answers

How to read json and csv data using Stata such that it can be treated similar to a sav file

I have data in the form of json and csv, and sometimes need to provide this to external analysts. They're used to working with sav files though, so I would like to give them some utility function which will enable them to load the data and work with…
baxx
  • 3,956
  • 6
  • 37
  • 75
0
votes
1 answer

create a variable list from local macro list (findname)

I have lists of variables in local macros that I would like to use in a command, e.g.: local a int1 int2 num1 num2 bin1 bin2 ... local b int3 int4 bin3 num3 ... Here variable num* int* and bin* are numeric, integer and binary…
Makiko
  • 27
  • 9
0
votes
1 answer

Capturing non-missing values row wise and storing it in new variables

My dataset contains multiple variables called avar_1 to bvar_10 referring to the history of an individual. For some reasons, the history is not always complete and there are some "gaps" (e.g. avar_1 and avar_4 are non-missing, but avar_2 and avar_3…
Matthuit
  • 23
  • 3
0
votes
1 answer

invalid 'Component' error when looping over files in a folder in stata

I want to append multiple daily market indices data which I downloaded separately and all stored as csv in the same folder. As the first step, I cleaned the data and saved as dta file for each csv file. However, an r(198) error named "invalid…
IAN CHAN
  • 3
  • 1
0
votes
1 answer

Stata loop to retrieve data from Excel file

My Excel file has many sheets. I am trying to create a loop to retrieve every sheet of data to be able to save it as .dta. This is my code: local sheet = `" "Data dictionary" "Wave 1" "Wave 2" "Wave 3" "Wave 4" "' foreach x of local sheets{ …
A01066656
  • 25
  • 4
0
votes
1 answer

Can we run stpm2 in Stata 15.1?

I have tried to run a flexible parametric survival model using stpm2 in Stata 15.1, and I'm getting an error code of '3499 stpm2_setup() not found'. How we can resolve this issue?
atn
  • 11
  • 2