Using local macros in a foreach loop

  Рет қаралды 24,884

Alan Neustadtl

Alan Neustadtl

Күн бұрын

Пікірлер: 41
@mrerickism
@mrerickism 4 жыл бұрын
Thank you for the simplified yet elaborate explanation about macros. Easy to see, easy to follow, easy to understand!
@asharawan7331
@asharawan7331 4 жыл бұрын
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
@jonasjohajohanson9517
@jonasjohajohanson9517 8 жыл бұрын
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.
@smilex3
@smilex3 8 жыл бұрын
+Jonas Joha Johanson I'm glad you found these videos useful. Let me know if you have suggestions about other Stata programming topics.
@jonasjohajohanson9517
@jonasjohajohanson9517 8 жыл бұрын
+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!
@mdhasibreza5161
@mdhasibreza5161 5 жыл бұрын
Very effective tutorial! Thanks a lot for uploading this video!
@julievolk8033
@julievolk8033 7 жыл бұрын
Thank you for a great tutorial! Please keep posting videos
@rupertmajor7032
@rupertmajor7032 7 жыл бұрын
Thanks, good clear example of using loops.
@Garrettthethief
@Garrettthethief 7 жыл бұрын
Thank you again. these examples helped me a lot.
@rezaghaziani9872
@rezaghaziani9872 6 жыл бұрын
Hi Alan, Just to say Thank you for your easy to learn explanation. cheers :)
@tavarez5426
@tavarez5426 8 жыл бұрын
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.
@ericsuan4680
@ericsuan4680 10 жыл бұрын
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.
@pilarzueras4099
@pilarzueras4099 6 жыл бұрын
Thank you so much for your great videos!
@任舒云
@任舒云 4 жыл бұрын
It is an amazing video and helps me a lot! Could you make a video about code program?0
@ilzaninalom
@ilzaninalom 8 жыл бұрын
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 .
@smilex3
@smilex3 8 жыл бұрын
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.
@otgondugee1403
@otgondugee1403 7 жыл бұрын
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]) } } }
@smilex3
@smilex3 7 жыл бұрын
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.
@Yeppi232
@Yeppi232 2 жыл бұрын
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
@smilex3
@smilex3 2 жыл бұрын
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
@Yeppi232
@Yeppi232 2 жыл бұрын
@@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.
@zakku78
@zakku78 10 жыл бұрын
Hi, Thanks a lot. do you have any example on levelsof Regards Zak
@smilex3
@smilex3 10 жыл бұрын
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
@neylatardin7233
@neylatardin7233 9 жыл бұрын
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?
@smilex3
@smilex3 9 жыл бұрын
+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
@rajendrashrestha7466
@rajendrashrestha7466 4 жыл бұрын
Dear Alan, could you provide the data you used in this video demonstration?
@smilex3
@smilex3 4 жыл бұрын
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.
@debit342
@debit342 8 жыл бұрын
Hey Alan, Also a minor error at 5:52 where `var' rev=socrelrev, not socrelvar... ~Arvind
@smilex3
@smilex3 8 жыл бұрын
+arvind sharma Thank you! I added an annotation at that point of the video pointing out the correct Stata code.
@clapton007lp
@clapton007lp 6 жыл бұрын
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
@smilex3
@smilex3 6 жыл бұрын
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
@clapton007lp
@clapton007lp 6 жыл бұрын
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!
@kaminajohnson8543
@kaminajohnson8543 10 жыл бұрын
Very helpful, thank you.
@smilex3
@smilex3 10 жыл бұрын
You're welcome!
@manzooranoori2536
@manzooranoori2536 Жыл бұрын
thank you
@jakeclifton3310
@jakeclifton3310 6 жыл бұрын
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
@smilex3
@smilex3 6 жыл бұрын
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.
@jakeclifton3310
@jakeclifton3310 6 жыл бұрын
Alan Neustadtl awesome, thanks for the prompt response. Keep doing what you're doing, I'm learning a lot from your videos!
@gedeaolocks155
@gedeaolocks155 7 жыл бұрын
Thank you!
@RPDBY
@RPDBY 7 жыл бұрын
Did you just delete my comments where i asked for help?
@smilex3
@smilex3 7 жыл бұрын
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.
Using local macros in Stata
28:37
Alan Neustadtl
Рет қаралды 37 М.
Introduction to Programming Loops in Stata
17:25
Mike Jonas Econometrics
Рет қаралды 19 М.
Try Not To Laugh 😅 the Best of BoxtoxTv 👌
00:18
boxtoxtv
Рет қаралды 7 МЛН
When u fight over the armrest
00:41
Adam W
Рет қаралды 22 МЛН
Perfect Pitch Challenge? Easy! 🎤😎| Free Fire Official
00:13
Garena Free Fire Global
Рет қаралды 69 МЛН
How Stata Treats Missing Values
16:18
Alan Neustadtl
Рет қаралды 43 М.
Stata Tips: Macros
14:24
Ajay Shenoy
Рет қаралды 10 М.
Cleaning Data in Stata
22:42
Alan Neustadtl
Рет қаралды 99 М.
Google Data Center 360° Tour
8:29
Google Cloud Tech
Рет қаралды 5 МЛН
Stata for Undergraduates 14: Combining Data Sets
9:19
Rochester Econometrics
Рет қаралды 62 М.
Fixed Effects in Stata
13:41
SebastianWaiEcon
Рет қаралды 213 М.
Creating New Variables Using Stata
31:24
Alan Neustadtl
Рет қаралды 55 М.
Linear regression using R programming
20:01
R Programming 101
Рет қаралды 108 М.
If __name__ == "__main__" for Python Developers
8:47
Python Simplified
Рет қаралды 412 М.
Tables in Stata with estout
11:40
SebastianWaiEcon
Рет қаралды 129 М.
Try Not To Laugh 😅 the Best of BoxtoxTv 👌
00:18
boxtoxtv
Рет қаралды 7 МЛН