Using local macros in Stata

Поделиться
HTML-код
  • Опубликовано: 10 окт 2024
  • One of the great features of Stata is the local macro construct that allows macro substitution or expansion. Using macros leads to fewer lines of code, code with more clarity, and an economy of scale. This video provides some examples of how to use local macros in your programs.

Комментарии • 69

  • @drabhishekjaiswal2548
    @drabhishekjaiswal2548 6 лет назад +7

    you are awesome sir,
    i was struggling with the stata macros.... you explained everything so beautifully
    thanks a lot

  • @rugaziagonzaganyombi4829
    @rugaziagonzaganyombi4829 5 лет назад +1

    Alan, u made hard stuff look simple.
    Thanks for this insightful presentation

    • @smilex3
      @smilex3  5 лет назад

      I'm glad you found it useful. Macros are used everywhere in Stata and are very powerful.

  • @SudheerShuklaArjun
    @SudheerShuklaArjun 2 года назад

    Thank you for these important macros.

  • @manzooranoori2536
    @manzooranoori2536 Год назад

    Thank you so much. It was so clear and helpful.

  • @鄭達翼
    @鄭達翼 6 лет назад

    Wow! Amazing, this is the most detailed video I saw on youtube about local macros, thxs

  • @kimberlyfabian1013
    @kimberlyfabian1013 4 года назад

    awesome video! Struggling so much until I watched this

  • @pravin8419
    @pravin8419 2 года назад

    Thank you for such a wonderful tutorial.

  • @farahpangestuty3929
    @farahpangestuty3929 7 лет назад

    Really miss your new videos. It is so incredible

  • @weijiawang1571
    @weijiawang1571 7 лет назад

    Nice video. Very helpful. Thanks!

  • @katie6482
    @katie6482 9 лет назад

    Great video! Many thanks!

  • @debit342
    @debit342 8 лет назад +2

    Around 22:52 where you provide the explanation of the macro, please note that loop 3 should have hrs1 instead of wordsum...

    • @smilex3
      @smilex3  8 лет назад +1

      +arvind sharma Thank you for finding this. I have not had a chance to look at the issue, but I will.
      Best,
      Alan

  • @anasiqueira5920
    @anasiqueira5920 7 лет назад

    Hi, Alan. Thanks for these videos! Can I use macro to regress in loop of firm and save the residuals of the regression as a variable using the minimum of 8 firms? What is the stata command for that? Thanks! Ana

  • @Mgilsy
    @Mgilsy 6 лет назад

    Dear Alan. Thank you for a great video. I have tried to replicate the commands you use in your video with regression models. In my own dataset I have created a list of independent vars and stored them in a local macro that I include in several logistic regression models that
    each include different dependent variables. What I realized (at least I think I did) is that you have to create the local macro as well as run all the models including the local macro at once, i.e. you have to run several lines from your do-file in one go (as you do as well in your video). Is it possible to create a macro, which does not require that you run all of these commands at once? E.g. you may first create the local macro of independent variables, then spend some time doing some summary statistics, inspecting your data etc. and then, later in the stata session, run a model using the specified local macro of the independent variables. I hope this makes sense. Thank you very much. Kind regards, Martin (Denmark).

  • @otgondugee1403
    @otgondugee1403 7 лет назад

    I'm trying to run below matrix of concentration index (ci). However, following error came up. Could you advise what's wrong with it.
    type mismatch
    r(109);
    end of do-file
    r(109);
    sum r [aw=hhweight]
    sca v_rank=r(Var)
    foreach var of varlist count* {
    sum `var' [aw=hhweight]
    sca m_`var'=r(mean)
    gen d_`var'=(2*v_rank)*(`var'/m_`var')
    quietly {
    regr d_`var' rank
    matrix coefs=get(_b)
    gen ci_`var'=coefs [1,1]
    If "`var'"=="count5" {
    matrix ci=coefs [1, 1]
    }
    if "`var'"~=̏count5̋ {
    matrix ci=(ci, coefs[1,1])
    }
    }
    }

    • @smilex3
      @smilex3  7 лет назад

      Otgon,
      This error message implies that either you are trying to numerically manipulate a string variable or apply a string operation to a numeric variable. The following Stata code demonstrates this idea. So, you will need to look at your variables and decide if you need to convert some or all to numerics or to strings. Here is the example code. Run one line at a time since the second generate will fail with an error.
      Best,
      Alan
      /* Stata Code Follows */
      sysuse auto, clear
      describe mpg make
      generate log_mpg=log(mpg) /* This works */
      generate log_make=log(make) /* Data mismatch */
      generate mpg_len=strlen(mpg) /* Data mismatch */

  • @shamsaditi5923
    @shamsaditi5923 7 лет назад

    Hi Alan, I followed your instruction for constructing macro ( referring to your do file 27-30) but it's not working ( I am using STATA 14). Can you please suggest . I used the following command for my variables :
    ** hofstede to refer to the following three variables **
    local hofstede Power_Distance Individualism Masculinity
    di `hofstede'
    reg WoB_Proportion `hofstede'
    However the regression result is not showing coefficients for the three independent variables.
    Please suggest. Thanks in advance.

    • @smilex3
      @smilex3  7 лет назад +1

      Shams, without seeing the error from your program I can't be very specific about how to fix the issue. That said, the problem I see most often is the incorrect use of quoting in local macros. The left quote is not really a quote. It is the symbol underneath the tilde key. This is the tilde "~" and this is the quote on the same key "`". The right quote is " ' ". Here is some reproducible Stata code that works:
      sysuse auto, clear
      local ivs weight displacement
      regress mpg `ivs'
      Best,
      Alan

    • @shamsaditi5923
      @shamsaditi5923 7 лет назад

      Thank you Alan. I tried the same quote you mentioned. I cannot upload the error msg in the youtube comment box, is there any other way I can send you the snapshot of the error?

  • @hadhadaa6947
    @hadhadaa6947 9 лет назад

    Thank you for your video.
    I would like to ask about bootstrapping. Could you give me an example that how to bootstrap with the foreach loop. For instance, to get standard error for elasticities from multiple regression.
    Thank you.

  • @JC-dl1qr
    @JC-dl1qr 8 лет назад

    Thank you for sharing this.

    • @smilex3
      @smilex3  8 лет назад

      I'm glad you found this video helpful!

  • @carolinavargas1852
    @carolinavargas1852 9 лет назад

    +Alan Neustadtl I really enjoy your videos... though I was wondering if you have any that explainas how forvalue works in stata!

  • @aloksaurav5725
    @aloksaurav5725 8 лет назад

    Hi Alan, Seeking your help in constructing a foreach loop. I have a pair of datasets (A & B) for each year from 2001 through 2010. I want to run a set of commands for database A for a given year first, then merge it with dataset B of same year. I want to repeat this for each year from 2001 through 2010. Appreciate your help. Thanks.. Alok

  • @zumasuma5489
    @zumasuma5489 6 лет назад

    Great help!

  • @fenixrock
    @fenixrock 7 лет назад

    Greatings Professor, thank you so much for your videos. They are really clarifying to me.
    I was wondering, why in the 41th line, don't you have to use the "`'" quotes to use the e() macros?.

    • @smilex3
      @smilex3  7 лет назад +1

      julián, for reasons unknown to me, you do not need the special quotes on these Stata generated local macros. For example, the following all produce the same result with the exception that the last example is a string while the first two are numbers:
      sysuse auto, clear
      regress mpg weight
      di e(mss)
      di `e(mss)'
      di "`e(mss)'"
      Best,
      Alan'

    • @fenixrock
      @fenixrock 7 лет назад

      Thank you!

  • @colmduffy2272
    @colmduffy2272 6 лет назад

    this is particularly useful

  • @TalzBlaze
    @TalzBlaze 10 лет назад

    Is it possible that you can post some video about example how to do 30 regression estimate for the Dowjones stocks in just a short time rather than doing it by manually loading each firms data repeatedly over and over. and doing each seperate regression and then pile it in one file. have you some pointers on how to do such sort of a thing,

  • @carolinavargas1852
    @carolinavargas1852 9 лет назад

    I really enjoy your videos... though I was wondering if you have any that explainas how forvalue works in stata!

    • @smilex3
      @smilex3  9 лет назад

      +Carolina Vargas I don't have a video explicitly about this but if you type "help forvalues" in the command window you can get a brief introduction. Do you have a specific problem in mind that we may be able to teas out together?

    • @carolinavargas1852
      @carolinavargas1852 9 лет назад

      +Alan Neustadtl Im currently in a stata class and have to do some work. I have a data base(colombian) that is showing for each region the types of taxes and how much was recieved for each type of tax each year by region. So the variable nabes are something like this:
      region | type_ of_ tax | 2000 | 2001 | 2002 | 2003 | 2004 |
      there are 33 types of tax. they ask me to create a loop using "forvalues" with a sequence for each type of tax that has the following:
      1) Number of iterations as big as the number of types of taxes (so 33)
      2). Use command "summ"
      3) extract the descriptive stats from each and save them in a matrix
      **the matrix was created before and on the rows names were the types of tax and columns are mean sd min max sum. Right now the matrix is composed of "."
      I dont know if this is very confusing

    • @smilex3
      @smilex3  9 лет назад

      +Carolina Vargas So, this is a little confusing to me, but here is a try at using forvalues to produce descriptive statistics for different groups using the built-in auto.dta dataset:
      sysuse auto, clear
      forvalues i=1(1)5 {
      summarize mpg if rep78==`i'
      }
      This ignores putting the results into a matrix which is more complicated. I know that the variable rep78 ranges from 1 to 5 so I can summarize another variable, mpg in this example, when rep78=1, then when rep78=2, and so forth.
      But, in Stata this kind of operation can often be accomplished more simply using other means. This is probably not within the scope of your assignment, but the same out put can be achieved with just a single line of Stata code:
      bysort rep78: summarize mpg if rep78!=.
      And, this has the advantage of further grouping your summarized statistics. Here you can see the descriptive statistics of mpg broken down by foreign and domestic and within those categories by the five categories of rep78:
      bysort foreign rep78: summarize mpg if rep78!=.
      Is the first example what you needed?

  • @313kash
    @313kash 7 лет назад

    Initial tune is dope

    • @smilex3
      @smilex3  7 лет назад +1

      A great Canadian singer-songwriter named Stan Rogers. One of my favorite songs of his is called "Northwest Passage". Prime Minister Stephen Harper called it the unofficial national anthem of Canada. You can listen to it here: ruclips.net/video/Fk2cNWW5FU0/видео.html

  • @showyijiang6260
    @showyijiang6260 10 лет назад +1

    thanks, its so helpful

    • @smilex3
      @smilex3  10 лет назад +1

      I'm glad you found the video useful!

  • @javierbeltran7623
    @javierbeltran7623 7 лет назад

    Dear Alan. First, thanks for your videos!!!... I am trying to create a macro and foreach loop with the command spmap, but always STATA send the message "too many variables specified". This is the code:
    local mld mldc_ilpc mldc_iapc mldc_impc
    format `mld' %12.2f
    foreach var of varlist `mld' {
    spmap `mld' using comunascoord, id(id) clmethod(q) legend(size(medium) position(7)) fcolor(Blues2)
    graph save `mld'
    }

    • @smilex3
      @smilex3  7 лет назад

      Javier, I cannot replicate your error. I adapted your code to run with the auto.dta dataset and it runs fine. Can you use that dataset and replicate your error? Then I might be able to dig a little deeper. I also wonder why you are using macros at all in your code with so few variables you could just use the format command. See the second to last line of the code below for an example.
      Best,
      Alan
      sysuse auto, clear
      desc length turn displacement
      local mld length turn displacement
      format `mld' %12.2f
      desc length turn displacement
      format length turn displacement %8.0g
      desc length turn displacement

  • @docteurorl
    @docteurorl 9 лет назад

    Thanks a lot!

    • @smilex3
      @smilex3  9 лет назад

      Valérie Julie Brousseau Thank you for your comment. I'm glad you found this video useful.

  • @bugzdesilva8216
    @bugzdesilva8216 Год назад

    thank you so much, can you please show how to rename a set of variables using loop and also to generate dummy variables using loop command

    • @smilex3
      @smilex3  Год назад

      Hi Bugz, Stata has a command that allows you to rename a group of variables. I would rename variables using this command and not use any looping constructs. Here is an example:
      sysuse auto, clear
      rename (make price mpg rep78 headroom trunk ///
      weight length turn displacement gear_ratio foreign) ///
      (v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12)
      You can read about the command by entering help rename group in the command window.
      You don't say much about how you want to construct your dummy variables but I can give you two examples. The first uses numeric variables as inputs and divides them at their means, assigning 0 if a value is less than or equal to the mean and a 1 if it is greater than the mean and not missing. I put the variable generation in a loop. Here is the example:
      sysuse auto, clear
      foreach var of varlist price mpg weight {
      summarize `var', meanonly
      generate `var'dum = `var' >= r(mean) & `var'

    • @bugzdesilva8216
      @bugzdesilva8216 Год назад

      🤩thank you so much for the very clear and extensive answer. I have several categories in the dummy. So as your suggestion it is the tabulate command I used.
      gen v114=waters_dummyd
      tabulate waters_dummyd, gen(water_srca)
      gen imp_water_dd=1 if water_srca1==1
      replace imp_water_dd=1 if water_srca2==0
      For this, if there are categories from 1 - 20 I need to retype it 20 times and change the 1 or 0. I was wondering if I should do this way manually or if is there a macro to make this in a few lines.

    • @smilex3
      @smilex3  Год назад +1

      ​@@bugzdesilva8216
      I am confused by your code. I belive that your first two lines of code can be replaced by a single command like this:
      tabulate v114, gen(water_srca)
      You do not have to create a new variable to tabulate. Here is how this works with the auto.dta dataset:
      sysuse auto, clear
      tab rep78, generate(rep78dum_)
      Because the variable rep78 has 5 categories, a total of 5 dummy variables are created. The variables form a linear combination so something like a regression model you would need to decide on a category to exclude. Nota bene: if using indicator variables in some model, you really should use Stata's factor notation (see help fvvarlist).
      Your next two commands are confusing to me because I do not know how the original variable, v114, was coded. It looks like you are trying to take two conditions and code them equal to 1 and all other equal to zero. Here is an example, again, using rep78:
      sort rep78
      generate rep78dum = cond(1, rep78==1 | rep78==5, 0) if rep78

    • @bugzdesilva8216
      @bugzdesilva8216 Год назад

      @@smilex3 Thank you so much !!! The last example you provided was really helpful to me. It solved the problem I faced. I was generating a binary variable assigning 1 or 0 per category to the main variable (ie: rep78). Your command made my dofile clear and smooth. Glad I'm following your channel !!

    • @bugzdesilva8216
      @bugzdesilva8216 Год назад

      @smilex3 And one more clarification please.
      if we want to generate a categorical variable by using the rep78 variable,
      giving 1 if rep78==1 and 2 if rep78==2 and 3 if rep78==3 and 0 for both rep78==4 and rep78==5
      how could we generate the above categories using the same command. Is it possible..
      *sort rep78 generate rep78dum = cond(1, rep78==1 | rep78==5, 0) if rep78

  • @statdata3973
    @statdata3973 7 лет назад

    Alan Neustadtl, you could try "Alt+96" and/or "Alt+39" for the apostraphes. For even smoother workflow with your surface pro2, you could assign those to an autohotkey.
    Having said that, I am pretty sure you can find the apostraphes even without using the alt-combinations. It will probably just take some trial and error before you find them.
    usefulshortcuts.com/alt-codes/punctuation-alt-codes.php
    autohotkey.com/

  • @Canuckish
    @Canuckish 9 лет назад

    the `r(N)' and other scalars seems like a fantastically silly way for STATA to store these data. How can I look at the objects stored within r(), e(), etc? It seem like there should be a useful way of looking at what is stored in these lists before I'd use them...?
    For example, I have a "flexible" stat summary table with rows, columns, and super columns. When I type "return list" after this table the only piece it shows is `r(N)' - where's the rest?

    • @smilex3
      @smilex3  9 лет назад +1

      Jeff van Geete Unfortunately, there is no rest. When Stata commands are written, the developer decides what, if any, results are stored as local macros. In the case of the table command (I think this is what you are referring to) only one result, the sample size, is saved as a scalar.
      I am not certain if this is silly (fantastically) or not, but saved results in macros are often very useful if you want to use these results in your own programming to calculate something outside of the command, proper.
      Best,
      Alan

    • @Canuckish
      @Canuckish 9 лет назад

      Alan Neustadtl As in most other stat programs / packages, I agree that having the results is useful for later calculation. For example, all (lm) linear models in R contain these results in their structure, without having to issue an extra command. I quite like Stata, but on this one issue look no further than the curious assignment of ` and ' (I can think of no other language that demands dissimilar quotes). Truly head-scratching.

    • @smilex3
      @smilex3  9 лет назад

      Jeff van Geete I am not sure why you find using `' to delimit local macros. All programming languages have these kind of programming syntax. Suppose you could define a local macro as "local #newmacro r(N)" and then refer to it as "#newmacro". How is the decision to use a pound symbol to refer to macros any less curious than using ` and ' delimiters?
      Unfortunately, all programming languages do what they do regardless of our dreams, wishes, and desires! So, for many of us that means learning the ins and outs and curiosities of many programming languages.

    • @Canuckish
      @Canuckish 9 лет назад

      Alan Neustadtl if a language wanted you to use ( and ] to open and close arguments, would you think that's odd? why use mismatched quotes? just to be different? programming languages are supposed to follow a discernible (and yes, learnable) logic, almost rule #1 - or? i use `language function()` all the time in markdown (as in `r nrow(data)`), as has been standard since probably before my time. matching these opening and closing symbols is standard - or should be.

    • @smilex3
      @smilex3  9 лет назад

      No more odd than many, many other programming conventions which is exactly my point. What is odd in R to me is not to an R programmer. What is odd to the R programmer in Stata is not to me. And no, using ( and ] is no more odd than using ` and ' or #macroname . I suppose that using the accent grave and a quote may have been selected as delimiters since they are similar, but not equal to single and double quote pairs. But I speculate, and my wishes and desires for what Stata "should do" are of no consequence--Stata does what it does (same for all other programming languages.

  • @RPDBY
    @RPDBY 7 лет назад

    ive been trying to use local macros, but ` and ' do not do the job, i can assign macros, but cannot use it cause `local-name' does not do anything, global works just fine

    • @smilex3
      @smilex3  7 лет назад

      Can you share a reproducible example that fails? Without an example, it is difficult to comment on how to address your problem.

    • @RPDBY
      @RPDBY 7 лет назад

      here is my code for three dummies mw ne sou
      local region "mw ne sou"
      display "The local macro region contains: `region' "
      the display command returns "The local macro region contains: ", in other words it returns nothing, if i include `region' in regression model, none of the variables shows up in the model as if i did not include the local macro at all... hope that clarifies the problem

    • @smilex3
      @smilex3  7 лет назад

      Well, that worked perfectly for me. It produced the following output: The local macro region contains: mw ne sou
      Can you explain how this does not work for you?

    • @RPDBY
      @RPDBY 7 лет назад

      well, im not sure what else to explain beyond saying that this trivial command does not work on my PC for an unknown reason, i do exactly the same thing as i posted earlier and i get empty output, which is "The local macro region contains:" without the dummies mw ne sou, same happens when i include `region' into regression model, the dummies just not show up in the model. I tried it a month ago and it worked fine back than... i am using stata 13.1

    • @RPDBY
      @RPDBY 7 лет назад

      when i type su `region' i get a summary for all variables in the dataset...