Announcement

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

  • Changing many variables with wildcard *

    Hello all,

    I am trying to change many variables in a time series data set, and would ideally like to do it in a manner such as "replace var* = var*[_n-1] if..." where the variables to be changed are var1 var2 var3 etc.

    But Stata does not allow such use of the wildcard * in this context, and I understand why.

    Is there a workaround? Perhaps using language such as foreach or forvalues, but I haven't found a way to do it with those commands yet.

    For what it's worth, a manual version of what I am trying to do, that I would need to do for each of the hundreds of variables (intervention1, intervention2, etc.), looks like this:

    by ccode: replace intervention6 = intervention6[_n-1] if intervention6[_n-1]!=0 & intervention6[_n-1]!=.
    by ccode: replace intervention6 = 0 if startyear>intervention6
    replace intervention6 = 1 if intervention6!=0


    Where the unit of analysis is country-year, ccode=country code, and each variable indicates an intervention involving the country.

    Thanks in advance.

  • #2
    Perhaps using language such as foreach or forvalues, but I haven't found a way to do it with those commands yet.
    You need to read those sections of the user manual more carefully . That precisely what they do.

    Code:
    foreach v of varlist var* {
        replace `v' = `v'[_n-1] if ...
    }
    is exactly what you are asking for.

    Comment


    • #3
      In fact, [P] on page 228 has examples like this, and the section is, in my view, very clearly written.

      Comment


      • #4
        Thank you. I will look into that in more detail.

        Comment

        Working...
        X