Basic Loops in Stata

Поділитися
Вставка
  • Опубліковано 26 жов 2024

КОМЕНТАРІ • 45

  • @blink2nap
    @blink2nap Рік тому

    Stata isn't taught properly in our college. So I'm very grateful for such clear videos.

  • @shalinparekh1920
    @shalinparekh1920 2 роки тому +1

    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 років тому +2

    thanks for the video, very clear explanation, very helpful. do more!

  • @monika1883
    @monika1883 7 років тому +1

    Thanks for sharing! Very understable video.

  • @claire2247
    @claire2247 3 роки тому

    Thank you Nick!

  • @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

  • @kimmyberly
    @kimmyberly 7 років тому +1

    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.

  • @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 років тому +1

      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'"

  • @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')
      }

  • @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

  • @____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.

  • @oscarfranyutti9352
    @oscarfranyutti9352 6 років тому

    at 6:05 when you included outreg2 What does that command ? or what is the difference with the previous loop?

  • @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

  • @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 UA-cam 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.

  • @raffaelapalomba7276
    @raffaelapalomba7276 4 роки тому

    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  4 роки тому +1

      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' "

  • @josearturogf
    @josearturogf 6 років тому

    Thanks!

  • @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!