Using local macros in a foreach loop
HTML-код
- Опубликовано: 11 сен 2024
- One of the great features of Stata is the local macro construct that allows macro substitution or expansion. This video walks through an example of using local macros in the context of foreach loops in a regression model.
My thanks to Arvind Sharma for pointing out an error at around 5:50 in the video. I have added an annotation at that point with the correction.
Thank you for the simplified yet elaborate explanation about macros. Easy to see, easy to follow, easy to understand!
Thank you so much, this is my second video which i watched to learn basics about macros, i think i dont need any third one now after watching this video. Hats off to you Alan
Hi Alan! Thank's a lot for all your great videos - especially this one here is awesome!
I know there are tons of other stata videos out there, but I like the way you explain your syntax, the way you solve problems, and how you give general informations about the program. Thank you for your work and all the effort you put in, I really really appreciate it.
+Jonas Joha Johanson I'm glad you found these videos useful. Let me know if you have suggestions about other Stata programming topics.
+Alan Neustadtl Well, I'm curently trying to learn all the fundamentals for a regular stata use by watching all your videos. My old university used another statistic program than my new one, so I have to keep up with the rest by learning by myself. I'm still not there, but later on I would enjoy some videos about time history analysis and multilevel analysis with stata ;). I know the theory, but to understand and use all the stata comands is quite a different story. It's the same with your regression videos. To do a simple bivariate, logistic or multiple regression is one thing, but to go further, and use all the capabilities that stata offers is somethin different. At least if you try to do it without "cheating" or a crib. Anyway, your videos are helping me a lot!
Thank you for a great tutorial! Please keep posting videos
Thanks, good clear example of using loops.
Very effective tutorial! Thanks a lot for uploading this video!
Hi Alan,
Just to say Thank you for your easy to learn explanation.
cheers :)
Thank you again. these examples helped me a lot.
Thank you so much for your great videos!
thank you
Hello Alan, I found your video very useful. Thanks for posting. I have a quick question. I would like to subtract all elements (observations) of var1 from all elements from var2. So, If both variables have 10 observations, I will have a new vector (var3) with 100 elements. Do you know how to develop a loop for this?
I will appreciate any help you can provide.
I wanted to create a new file from my main file. Basically, after keeping the variables and saved it as a subfile it would go to open again the main file and get another variable and so on. The tricky part is that each subfile contain variables that I would like to reshape to long. How am I going to keep and reshape in a loop so I don’t have to keep on opening the main file and keep the variables and save it into a subfile and goes on. Please help me….
Another problem I have is I want to merge all the subfile which I have created to come up with a single file. I am doing this because the main file is “wide file” and would like to create a “long file”. Or is there a way to simply transform the wide file with several variable say age1 age2 …age35, and other variable of the same sequence to a long file. Thanks so much for your help.
Hi Alan! I wonder if you can help me with event studies in Stata. I have 36,000 events in my sample and I want to calculate the returns in a five day window (-2,1,0,1,2) around the event. But I am suppose to use the predicted returns estimated in a 252 trading days before the announcement date (event date). Since I have several events, I have an overlap in the dates and my code does not work. Can you help me?
+Neyla tardin Neyla, I am unclear about the problem you re trying to solve. Part seems complicated but doable, but the other requires more information.
It sounds like you are trying to calculate a 5-period average (maybe a moving average). Here is some code that demonstrates how to do this:
sysuse auto, clear
generate timevar=_n
tsset timevar
generate moveavgmpg=(mpg+F1.mpg+F2.mpg+mpg+L1.mpg+l2.mpg)/5
list mpg L2.mpg L1.mpg mpg F1.mpg F2.mpg moveavgmpg
This method requires time series data but I fooled data by using the automatically generated _n variable as a time variable.
My guess is you need to create a dataset of predicted values based on data from 252 days prior. That is the part I am unclear about.
Does the moving average code make sense?
Best,
Alan
Dear Alan, could you provide the data you used in this video demonstration?
Hi Rajendra, You can download the data at gss.norc.org/get-the-data. I am not certain what year of the data I used in this video, but you could download the cumulative dataset and check the frequencies of some variables or models to find the exact year if that matters to you.
Hey Alan,
Also a minor error at 5:52 where `var' rev=socrelrev, not socrelvar...
~Arvind
+arvind sharma Thank you! I added an annotation at that point of the video pointing out the correct Stata code.
Hello @Alan
Can you show macros to edit tabulate command?
In stata when we use tab it shows output in a fixed manner, I want to edit that. For example, I want instead of showing p value under the table, it will show within the output table cells beside 'frequency' column
I don't think that this can be done without writing your own command to create a contingency table and the associate statistics that you are interested in displaying (e.g. chi2, p-value, etc.). There may be a way to do this using the new table command in Stata V17, but I don't see it after a quick look. While not what you are looking for, the following example use the (new) table command with the tabulate command to produce a table that is maybe a little better looking than the default one in tabulate. Not what you wanted, but maybe you will find it interesting:
sysuse nlsw88.dta, clear
quietly {
table (collgrad) (race) () if race
@@smilex3 thank you for your prompt response, I have stata 14.2 .
I tried to run this code
Its saying
option nototals not allowed,
Then I tried without nototals.
Now it is saying
option statistic() is not allowed.
I want to learn writing own command. If you can suggest me any link or book, that will be helpful.
Very helpful, thank you.
You're welcome!
Maybe stupid question, but what does the addition of “_n” do in your inside foreach loop in the first line when you’re simply displaying the dependent variable
Hi Jake, that is shorthand for "display _newline". ll it does is add a line to the output screen to make the results a bit easier to read. You can find other display directives by entering "help display" in the command window.
Alan Neustadtl awesome, thanks for the prompt response. Keep doing what you're doing, I'm learning a lot from your videos!
Thank you!
It is an amazing video and helps me a lot! Could you make a video about code program?0
Hi Alan,
Otgon again, I've changed the weight variable as integer; however, now the error is
"invalid syntax"
r(198);
sum r [aw=new_hhweight]
sca var_rank=r(Var)
foreach var of varlist over* {
sum `var' [aw=new_hhweight]
sca m_`var'=r(mean)
gen d_`var'=(2*var_rank)*(`var'/m_`var')
{
regr d_`var' rank
matrix coefs=get(_b)
gen ci_`var'= coefs[1,1]
if `var'==over5 {
matrix ci=coefs [1, 1]
}
if `var'~=over5 {
matrix ci=(ci, coefs[1,1])
}
}
}
Otgon,
Can you use a Stata supplied dataset (e.g. auto.dta) to replicate the problem. Without access to your data, it is difficult to debug your program.
But, I can make some suggestions. First, I would strip out everything in your foreach loop and add them back one line at a time, using different commands (e.g. display, list , etc.) to see what is working and what is produced. Second, I am uncertain why you are using brackets { to block your code. The brackets are typically fine if they are on their own lines (as you show in your code), but, it is possible they are creating a problem.
Hi, Thanks a lot. do you have any example on levelsof
Regards
Zak
Zak,
I don't have anything on video right now, but the following example may give you enough to go on. Basically, you can use -levelsof- to define a local macro that contains the unique values of a variable, typically an ordinal or nominal variable.
/* Example begins */
sysuse auto, clear
levelsof rep78, local(category)
foreach num of numlist `category' {
count if rep78==`num'
}
tab rep78
/* Example ends */
This is a silly program to demonstrate -levelsof-. First, I use -levelsof- to create a local macro called "category". This macro will contain the values of each unique category of the variable "rep78". In this data set the values are 1, 2, 34, & 5.
Then I use that macro with the count comman in a -foreach- loop to cound the number of cases for each level of rep78. Remember, local macros need to be surrounded with ` and ' . The ` charcter is called "grave accent" or some call it a backtick. It is not the same as a single quote mark (which is used to close the macro name).
Finally, I verify these results by using the -tabulate- command.
Hope that helps!
Best,
Alan
Thank you for your video. I just started with STATA. I have a directory with almost 100 dta files in it. they are daily reported data and I need a code that loop over the each dta file and select the variables for making a bar graph. It should create 100 bar graphs in total. Would you please five me some tips.
Appreciate your help in advanced .
There are different ways to approach this problem. One way is to write a short Stata program (see "help program" for an introduction) that take one passed parameter (the file name) and reads the file and creates and saves the graph you want. You would then execute this program 100 times, once for each data file. You could also put this in a loop where you have a macro containing 100 data file names.
This solution assumes that the variables have the same names across datasets. Otherwise, you would need to add the variables names as passable parameters.
There are ways to automate this problem, but they may take longer to code if you are only going to do this once. But, as a learning exercise, this is a good problem.
Hello! good video thank you!
I need more help please,
I want to estimate a dependent variable on the independent variables. In my case, I want to run a regression of y on k number of regressors giving 2^k models. for example, we have y and x1, x2, x3. I want to create a loop where I can estimate:
for example,
Y= C(constant)
Y= C + X1
Y= C+ X1+ X2
Y= C+ X1+X2 +X3
and store the R^2, AIC, and SIC for each model and specification. Please, I would be grateful if I can be directed on how to go about this on Stata
thanks in advance
Hi Marcelo, Generally, this is a (very) bad idea (see www.stata.com/support/faqs/statistics/stepwise-regression-problems/ for some details). Optimizing on chance without human intervention and interpretation can lead to misinterpretation and is generally frowned upon. But, that was not your question. There is a user-written program called "allpossible". From the command line enter "findit allpossible" and install the program. Here is some sample code that uses this program to assess all possible models:
/* Use the auto dataset */
sysuse auto, clear
/* Small program to capture AIC to estimate the quality of each model */
capture program drop myaic
program myaic, rclass
version 9
estat ic
tempname foo
mat `foo' = r(S)
return scalar AIC = `foo'[1,5]
end
/* Run all possible regressions with 6 independent variables */
allpossible regress mpg headroom trunk weight length turn displacement, eclass(rss r2) rclass(myaic AIC)
Best,
Alan
Hello,
Thank you very much for your input and your advice, certainly using a model of this type without knowing what is done is not recommended, but in my case I know my database and the models that I have to test, so it is very useful as a tool of classification. Thank you again!
Did you just delete my comments where i asked for help?
RPDBY, I do not delete or otherwise moderate comments on this channel. I looked through the history of comments and do not see any posts from you.