Basic Loops in Stata

  Рет қаралды 63,370

Econometrics, Causality, and Coding with Dr. HK

Econometrics, Causality, and Coding with Dr. HK

Күн бұрын

Пікірлер: 45
@blink2nap
@blink2nap Жыл бұрын
Stata isn't taught properly in our college. So I'm very grateful for such clear videos.
@shalinparekh1920
@shalinparekh1920 2 жыл бұрын
This is an awesome video just what I was looking for to help in my data analysis Thank you so much!
@Pietro-lp5ki
@Pietro-lp5ki 5 жыл бұрын
thanks for the video, very clear explanation, very helpful. do more!
@monika1883
@monika1883 7 жыл бұрын
Thanks for sharing! Very understable video.
@girlthatcooks4079
@girlthatcooks4079 2 жыл бұрын
Hello! Thankyou amazing! How do we generate a stata program (that we name) but has a loop in it, i am not able to know the order to the steps:(
@NickHuntingtonKlein
@NickHuntingtonKlein 2 жыл бұрын
You can define a program called "programname" in stata with "prog def programname". Then you can include a loop inside of that code as normal.
@girlthatcooks4079
@girlthatcooks4079 2 жыл бұрын
@@NickHuntingtonKlein Thankyou so much! Do i put thr forval command before or after the agrs comman while defining the program?
@NickHuntingtonKlein
@NickHuntingtonKlein 2 жыл бұрын
@@girlthatcooks4079 depends what you're doing but probably after
@Azam_Pakistan
@Azam_Pakistan 2 жыл бұрын
Is there a way to display just the single value of each statistic e.g I run ARDL and then go for LM, B.Pagan,Godfrey etc etc tests, the results of these tests show too many tables and details with each test that I dont need (atleast for now), can I just get the values (Chi Sq here )of the statistic for each?
@NickHuntingtonKlein
@NickHuntingtonKlein 2 жыл бұрын
Sure, something like foreach stat in statname1 statname2 { display r(`stat') }
@GebretsadikShibre
@GebretsadikShibre Жыл бұрын
Thank you for the video. I want to kindly ask you on how do I export bivariate regression output ( odds ratio and its 95% VI) done in foreach and forvalues to word. I have many exposure variables and one outcome variable. I want to do regression of my outcome variable with each variable separately and looping this over another variable, survey time ( 1 to 4). The loop looks like this: forvalues i=1/4 { foreach var of varlist v106_new v025 adolescent_age_F media_new husband_edu_new v190 region_f religion_f women_occupation husband_occupation wantedness_child birthor swpatt3gr swpsoc3gr { svy:logit skilled_delivery i.`var' if survey==`i', or } } Thanks in advance
@NickHuntingtonKlein
@NickHuntingtonKlein Жыл бұрын
Thanks! Check out this video on outreg2 to output results
@joesr31
@joesr31 5 жыл бұрын
Is there a way to loop a regression on the "title bar" like the first row with the names the default is A, B, C, D. I have 400 of those and do not know how to loop them.
@NickHuntingtonKlein
@NickHuntingtonKlein 5 жыл бұрын
I'm not sure I quite follow what you're taking about but yes you can loop over it. If you mean looping over variable names, use varnames
@oscarfranyutti9352
@oscarfranyutti9352 6 жыл бұрын
at 6:05 when you included outreg2 What does that command ? or what is the difference with the previous loop?
@NickHuntingtonKlein
@NickHuntingtonKlein 6 жыл бұрын
Outreg2 writes the results of the regression to a file.
@claire2247
@claire2247 3 жыл бұрын
Thank you Nick!
@____darrah____
@____darrah____ 5 жыл бұрын
Is there a way to set the number of iterations equal to a specific cell with a numerical value?
@NickHuntingtonKlein
@NickHuntingtonKlein 5 жыл бұрын
If you mean pulling a single number out of a cell and then looping that many times, sure. If you want to loop a number of times equal to the value of variable X on row R, then local loopnumber = X[R] forvalues i = 1(1)`loopnumber' If you mean you want the loop to go a different number of times for each observation based on that observation's value of a particular variable, that's different. I'd recommend looping over the largest possible value, and then making whatever changes you're making in the loop only if `i' is below the value you're interested in.
@pragjn
@pragjn 5 жыл бұрын
Thanks Nick! Great Video. I have a doubt in the last syntax that you have shown. The levelsof syntax works both for string as well as categorical variable? I was trying to use it on auto.dta and I couldn't run the syntax. Please help :- . levelsof foreign, l(levels) 0 1 . foreach x in "levels"{ 2. summ rep78 if levels == `x' 3. } levels not found r(111); OR levelsof make, l(level) `"AMC Concord"' `"AMC Pacer"' `"AMC Spir ...............(This is a long list) foreach x in "levelcaste"{ 2. summ rep78 if levelcaste == `x' 3. } level not found r(111);
@NickHuntingtonKlein
@NickHuntingtonKlein 5 жыл бұрын
Four things: 1) you have these if levels == statements, but you actually want to check if foreign and make take those values. So it should be if foreign == and if make ==, respectively. 2) when referring to a local variable like levels you have to quote it as a local variable, not a string, like `levels', which gives you the levelsof list, whereas "levels" just gives you a string containing the letters "levels". So it should be foreach x in `levels' 3) in your second levelsof you create the local variable levels but then loop over levelscaste which I think is undefined 4) this works for both string and numeric variables. But when checking equality of a string you have to quote it, or else it will think you're trying to refer to a variable by that name. So it should be summ rep78 if make == "`x'"
@kimmyberly
@kimmyberly 7 жыл бұрын
Thanks for sharing this Nick! Your piece on looping foreach code over all the values of a variable was exactly what I was looking for! Unfortunately, I can't seem to get it to work for my own string variables -- when I run it from my do file, the only output I'm given is the location of the temporary do file code. Do you have any suggestions on how to troubleshoot?
@NickHuntingtonKlein
@NickHuntingtonKlein 7 жыл бұрын
Can you post your code?
@NickHuntingtonKlein
@NickHuntingtonKlein 7 жыл бұрын
And thanks, by the way!
@kimmyberly
@kimmyberly 7 жыл бұрын
Thanks Nick! I tried to stick to something basic in case it was my tabout command that was giving me issues, but even trying this wasn't working. levelsof agency, l(org) foreach x in `org' { summ age if agency == `x' }
@NickHuntingtonKlein
@NickHuntingtonKlein 7 жыл бұрын
Is agency a numerical variable or a string? If it's a string, you'll need summ age if agency == "`x'" with the quotes. If that's not it, I'm not sure what's going on, that syntax looks correct to me.
@kimmyberly
@kimmyberly 7 жыл бұрын
Does the foreach x also need to be "x" as well? It is a string and I've put in quotations, but I'm still getting the the temp do file location.
@raffaelapalomba7276
@raffaelapalomba7276 5 жыл бұрын
Hi, I am a very beginner and I have a probably stupid problem: when I give the command of the basic loop " forvalues i=1(1)10 { display "i" } stata gives me 10 times i (like i i i i i i i i i i in a vertical line) . Why is this?
@NickHuntingtonKlein
@NickHuntingtonKlein 5 жыл бұрын
If you want i to be replaced with its value you need to refer to it as a variable explicitly by using `i' instead of just i in those quotes.
@johnvargas3006
@johnvargas3006 4 жыл бұрын
Your wrong is: display "i", because when you use the command forvalues i ... you've generated a macro called i, then if you want to use it, you should call it as a macro. It is: display " 'i' "
@anhTuan-eu8fr
@anhTuan-eu8fr 5 жыл бұрын
Hi Nick. Thanks really liked your video. Though I need more help if possible. (fyi, I'm pretty new to Stata). I am working with panel data over numerous waves and in cleaning up my data, I'd like an efficient way to create new variables to replace existing variables that differ by a letter subscript prior to the variable name to denote the wave number. Let's say I want to generate age At the moment age is denoted by _hgage so for the variable is denoted by ahgage (1st wave), bhgage (2nd wave), etc. Can you help with the code to make a loop to replace all _hgage with 'age'? I will have to do the same with a few other variables so it would be a big help.
@NickHuntingtonKlein
@NickHuntingtonKlein 5 жыл бұрын
You can't have multiple variables all named age, so I'm guessing you're trying to reshape the data to long? I'd recommend doing a foreach var of varlist *age and in the loop, get the wave letter with local waveletter = substr("`var'",1,1) Then, rename the variable so the wave is at the end with rename `var' age`waveletter' Then your variable names will be agea ageb agec, etc. Now you're in a format where you can run a reshape with the s option (so it allows string identifiers: a, b, c...). After the reshape you'll have one variable indicating what wave it is, and another variable, age, which shows the age in that wave.
@anhTuan-eu8fr
@anhTuan-eu8fr 5 жыл бұрын
@@NickHuntingtonKlein Yes this is correct. One issue I have is that I want to merge/append all of the waves to a single file, however, because there are so many variables/observations, the process does not get beyond wave 7 so I thought of dropping all variables i don't need. Here's the code I've been working on so far, but it doesn't work (says "no variables defined"). (1) foreach var of varlist wave_a160.dta-wave_p160c.dta { (this is the file names within the folder) (2) use `var' in "C:\data", clear (not sure if the way I've tried to put the location of the file is correct or if I open the data file prior so it is in memory?). (3) keep id wave *sex *age *edhigh1 *wsfei *mrcurr (these variables occur in each wave) *relt *reli *rela (these variables only occur every 3-4 years). One issue is each variable name is preceded by the wave prescript (wave 1=a) so in wave 1, the variable sex is asex, etc). I tried the * or _ prior to each but that didn't work. (4) save "C:\data Wave\Wave1.dta", replace I appreciate your help. Cheers.
@NickHuntingtonKlein
@NickHuntingtonKlein 5 жыл бұрын
@@anhTuan-eu8fr I think this is probably too complex to really help through the medium of KZbin comments. You may want to check out upWork or something, I'm sure you can find someone who can sort this all out for you. I can say that off the bat, line 1 won't work. varlist is for a list of variables in your data, not a list of filenames. Use the dir command to create a local or global containing the filenames (see examples online as to the syntax, just google "stata list of files in folder"), then foreach to loop over the elements of that local.
@anhTuan-eu8fr
@anhTuan-eu8fr 5 жыл бұрын
@@NickHuntingtonKlein Yes of course. Thanks for all of your help.
@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
@NickHuntingtonKlein
@NickHuntingtonKlein 6 жыл бұрын
If k is small you can just write out all the model variants you want and loop over them, e.g. foreach indvars in "" "x1" "x2" "x1 x2" { then run the regression of reg y `indvars' inside the loop, store all the stuff you want (AIC etc), and print it out or output it with an outreg command. If k is large you may have to write k nested loops, for example foreach i1 in "" "x1" { foreach i2 in "" "x2" { then run the regression of reg y `i1' `i2', store all the stuff, etc. There are ways to do this with a single loop without having to write out all the variants, but honestly it's more difficult than just writing them all out.
@clapton007lp
@clapton007lp 6 жыл бұрын
Hello, Thank you very much for your contribution and your advice, it helped me to assemble the loops that were prepared. Thank you again!
@josearturogf
@josearturogf 6 жыл бұрын
Thanks!
Introduction to Programming Loops in Stata
17:25
Mike Jonas Econometrics
Рет қаралды 19 М.
Using local macros in Stata
28:37
Alan Neustadtl
Рет қаралды 37 М.
Family Love #funny #sigma
00:16
CRAZY GREAPA
Рет қаралды 37 МЛН
ЗНАЛИ? ТОЛЬКО ОАЭ 🤫
00:13
Сам себе сушист
Рет қаралды 4 МЛН
КОГДА К БАТЕ ПРИШЕЛ ДРУГ😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 7 МЛН
Basics of Loop in Stata | forvalues loop | foreach loop
10:01
The Data Hall
Рет қаралды 2,9 М.
Bootstrap in Stata
12:21
Econometrics, Causality, and Coding with Dr. HK
Рет қаралды 33 М.
Stata - How to Make Double/Nested Loops
11:09
Steffen's Classroom
Рет қаралды 569
Stata Lecture 13: Understanding Foreach, Forvalues, and Levelsof Loop
8:51
Stata - How to use groups and loops
8:28
Steffen's Classroom
Рет қаралды 6 М.
Local and Global Macros in Stata
15:35
The Data Hall
Рет қаралды 7 М.
Stata - Keep/Drop and Missing values
5:13
Steffen's Classroom
Рет қаралды 28 М.
5. How to create a for (foreach) loop in Stata with the help of numlist and varlist
23:12
Learn data analytics with us
Рет қаралды 16 М.
Binary logistic regression using Stata (2018)
28:11
Mike Crowson
Рет қаралды 135 М.
Family Love #funny #sigma
00:16
CRAZY GREAPA
Рет қаралды 37 МЛН