Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • loop with egen for standardising variables

    Hi, Nick Cox (no relation - or i might be better at stats!) helped someone else with a similar query but i could not adapt his code to work.

    I write in STATA 13:

    local varlist "hypro-arg"
    foreach var in `varlist' {
    sum `var'
    }

    Above works all fine.

    Then i want to generate a set of standardised variables from the same varlist and i write:

    local varlist "hypro-arg"
    foreach var in `varlist' {
    egen z_`var' =std(`var')
    }


    but it does not work .. Why??

    Nick wrote a solution to a similar problem that looked like this:
    foreach v of var <varlist> { > egen mean_`v' = mean(`v') > egen sd_`v' = sd(`v') > } Thanks anyone for helping - i am sure it is annoyingly obvious!

  • #2
    Hi Sharon,

    please read the FAQs on terms like "does not work". Please also use the code-environment to present code and/or output.

    I doubt that your first loop "works". Try

    Code:
    foreach v of var <myvarlist> {
        qui su `v'
        g double z_`v' = (`v' - r(mean))/r(sd)
    }
    Make sure you to use the same sample for standardization that you are going to use in your analysis. Also see center (Jann, SSC).

    Best
    Daniel
    Last edited by daniel klein; 29 Oct 2014, 09:00. Reason: parentheses in standardization

    Comment


    • #3
      Daniel's suggested code is entirely along the right lines, but Sharon's post deserves more detailed comment.

      Sharon's first code segment does make perfect sense, but it works because small misconceptions cancel. In this segment

      Code:
       
      local varlist "hypro-arg"
      foreach var in `varlist' {
            sum `var'
      }
      the varlist reference is evaluated before foreach gets to work. So foreach sees

      Code:
      foreach var in hypro-arg {
            sum `var'
      }
      That is a list with precisely one item -- namely hypro-arg -- which to Stata is one "word". Words are separated by spaces. Note that foreach has not been told that the word is a varlist. The macro name is not visible any more and in any case the macro name would be irrelevant. Any way, given one item in the list, the loop is executed just once, and is equivalent therefore to a single command

      Code:
      su hypro-arg
      which is perfectly legal and works, because summarize is happy to receive a varlist as argument. But this explanation explains why the same kind of code doesn't work in Sharon's other code. For those reasons,

      Code:
      egen z_`var' =std(`var')
      is interpreted as

      Code:
      egen z_hypro-arg = std(hypro-arg)
      which fails because what is offered as a new variable name is illegal (hyphens can't appear in Stata variable names). However, a little bizarrely,

      Code:
      std(hypro-arg)
      is perfectly legal, because in this context hypro-arg is interpreted as an expression, namely hypro MINUS arg -- which as said is legal, just not what is wanted.





      Comment


      • #4
        Thanks for taking the time for explaning in more detail, Nick!

        Best
        Daniel

        Comment


        • #5
          Thank you both very much for taking the time to answer. I understand!! Will take on board comments about how to write posts properly. Asante sana! (kiswahil for thanks)

          Comment

          Working...
          X