Announcement

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

  • Generate copies of all variables with foreach

    Hello all,

    Here's an example code that reflects what my data looks like:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(v1 v2) float v3 int year
    1 1 1 2000
    2 2 2 2000
    3 3 3 2001
    4 4 4 2001
    end
    I am trying to generate 2 copies of all of my variables with foreach like this:
    Code:
    foreach var of varlist_all {
    gen `var[sd]' = `var'
    gen `var[se]'= `var'
    }
    When I run this, I get error message invalid syntax r(198);

    The idea is to then collapse the variables like this:
    Code:
    collapse (mean) v1 v2 v3 (semean) v1se v2se v3se (sd) v1sd v2sd v3sd , by (year)
    How can I resolve the error message above? Or is there a smarter way to go about this to end up with the mean, standard error, and standard deviation of each variable per year?

    Many thanks in advance!

  • #2
    Wait, I have no idea why you'd want to do this to begin with.

    If you wanna make a copy of a variable, then clonevar is the way to go, but I'm confused about the goal, like the real goal.

    Variables can't have standard errors, they can have standard deviations. And anyways if THAT'S the goal, egen will readily give you the mean and standard deviation.


    So before we agree to a solution, tell me the problem your want to solve and what the goal is here, please

    Comment


    • #3
      By standard error I meant standard error of the mean.

      But clonevar solves all my problems, not sure why I didn't find that command earlier, thank you for your help!

      Comment


      • #4
        Actually it doesn't yet. My end goal is to have the mean, standard error of the mean and the standard deviation of each variable per year. So, I want to collapse the multiple observations per year to end up with one row for each year. In order to collapse the data in three different ways my understanding is that I need three copies of the same variable.
        As in my actual data set I have over 50 variables I don't want to generate the duplicates individually. How can I clone all of my variables at once?

        Code:
        foreach var of varlist_all {
        clonevar `var[sd]' = `var'
        clonevar `var[se]'= `var'
        }
        The above code again returns invalid syntax r(198);

        Comment


        • #5
          This doesn't make sense for a few reasons. Unless varlust_all is a variable, the loop won't work up front because you're looping over a variable list.

          Also, `var[sd]' isn't legal because `var[sd]' isn't a local macro you've defined. And even if you did `var'[sd], that doesn't return the standard deviation of a variable (does it??????, never attempted this before).

          Also, the collapse command undoes all of this anyways. I just looked at the help file for collapse and it explains that you can just as easily do
          Code:
          collapse (mean) mean1=v1 (sd) sd1=v1 (semean) sem1=v1
          and for a varlist you could just do
          Code:
          collapse (mean) x1-x50 ///
          (sd) x1-x50 ///
          (semean) x1-x50, ///
          by(year)
          so a loop isn't needed here.

          Comment


          • #6
            Thank you very much for your help, I wasn't familiar with the varlist syntax!

            Comment


            • #7
              Yep when commands call for variable lists, you can just do x1-x1000. In fact, you could do
              Code:
              foreach v of var x1-x3 {
              
              su `v'
              }
              just to illustrate this.

              Comment

              Working...
              X