Announcement

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

  • Loop only using last var in varlist?

    Hi Statalist. I am running Stata 14.2 on Windows.

    I have a long dataset with vars:
    • Group = observations are associated with one of three groups
    • Post = pre-assessment (0) and post-assessment (1)
    • Outcomes = ordinal ratings across a series of vars that start with the prefix "standund_" (for "understanding of standards")
    I want to create and export a data set containing a table of difference scores by group. To do this, I need to collapse scores by group, reshape wide, then calculate difference = score1-score0. Then I'd just keep the vars GROUP and the DIFF vars and -export excel- the data.

    The code I am using is below. The problem with the code is that Stata is only calculating the difference score for the last variable in the local varlist I'm defining.

    preserve
    keep group post stanund_*
    collapse stanund_*, by(group post)
    format stanund_* %9.2f
    foreach var of varlist stanund_* {
    reshape wide stanund_*, i(group) j(post)
    gen diff_`var'=`var'1-`var'0
    format diff_* %9.2f
    }
    *
    restore

    I tried also creating a local list of the varnames using the following code, but then I get an error complaining that the var stub is too ambiguous, which I'm confused by since it looks like I'm marking them as "diff" "0" or "1". (I got this idea from the thread at http://www.stata.com/statalist/archi.../msg00945.html.)

    local myvars
    foreach var of varlist stanund_* {
    local x `x' `var'
    }
    *Make sure my local list contains the varnames I expect - which it does
    di "`x'"

    keep group post `x'
    collapse stanund_*, by(group post)
    format `x' %9.2f
    reshape wide `x', i(group) j(post)

    foreach var of varlist `x' {
    gen diff_`x'=`x'1-`x'0
    }
    .
    stanund_techprof ambiguous abbreviation
    r(111);

    Any idea on how to get what I want? Thanks in advance.

  • #2
    Hello Brandon,

    I see two issues right off the bat.

    First, for your first chunk of code (the one between the preserve/restore), you've put your reshape inside of the for loop. The for loop will break after one iteration because you can't perform the same reshape again.

    Try:
    Code:
    /*The foreach below is not a valid solution to our problem.
    I'm leaving it in for the record, but adding this comment*/
    
    preserve
    keep group post stanund_*
    collapse stanund_*, by(group post)
    format stanund_* %9.2f
    reshape wide stanund_*, i(group) j(post)
    foreach var of varlist stanund_* {
        gen diff_`var'=`var'-`var' //Don't need 1 and 0 anymore b/c it should be part of the stanund_* varlist
        format diff_* %9.2f
    }
    *
    restore
    The above code may be enough to solve your issue.


    In your second chunk of code, your issue is using the x macro in your for loop. If you had used stanund_* in place of `x' in the foreach, you would be fine.

    Why? Well, your macro x contains (for example) the following variables: stanund_a, stanund_b, stanund_c.

    After you reshape your data, your data now contains the following variables: stanund_a0, stanund_a1, stanund_b0, stanund_b1, stanund_c0, stanund_c1. The 0's and 1's correspond to the post dummy variable you had earlier in your long form.

    So, what the above means is that when you run your foreach after running reshape, the foreach looks for the first variable of your varlist stored in x. The first variable listed in x is stanund_a. But now there is stanund_a0 and stanund_a1. Stata doesn't know which one you mean. Hence, your variable stub is ambiguous.

    If you had used stanund_* in the foreach, Stata would have automatically updated the varlist with the new variable names.


    Hope this helps,
    Roger
    Last edited by Roger Chu; 10 Nov 2016, 09:48. Reason: Commenting out the incorrect line in my code, as noted in #3 by Brandon.

    Comment


    • #3
      Thanks for the quick response, Roger, and for helping me think through the order of operations.

      Unfortunately, neither solution worked. The first simply yielded a set of zeros, since `var'-`var' will = 0 in every case. At least, those are the results I got.

      On the upside, I stumbled across the solution. I was thinking about your comments re an ambiguous stub, and attempted the following. The secret to the solution was in using -in- rather than -of-, and I think I see why...because I'm referring to a generic list rather than a varlist (etc) per se:

      foreach var of varlist stanund_* {
      local x `x' `var'
      }
      *
      di "`x'"
      *Yes, the local `x' is returning the list I expect

      keep group post `x'
      collapse stanund_*, by(group post)
      format `x' %9.2f
      reshape wide `x', i(group) j(post)

      foreach y in `x' {
      gen diff_`y'=`y'1-`y'0
      }

      ...And it worked! Lucky, I know, which is not how I want to find solutions, but it made me think about local lists differently.

      Comment


      • #4
        Oh, whoops! You're absolutely correct about the block of code in #2 not working. I sped through the code too quickly.

        The solution you have should work, and that last foreach in #3 is how you would deal with the issue you brought up in my code.

        Good to see that you've found the solution!

        Comment


        • #5
          Note that this loop

          Code:
          foreach var of varlist stanund_* {
               local x `x' `var'
          }
          can be avoided in favour of

          Code:
          unab x : stanund_*

          Comment

          Working...
          X