Announcement

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

  • Efficient way to generate binary vars while changing var names

    Let's say I have 3 variables: freq_fight freq_run freq_drink, which contain values 0, 1, 2, 3. I want to create binary 0/1 versions of each of those variables and also have the new binary variables replace 'freq' with 'binary' in the variable name. I could do this very clunkily, but is there a way to do this all in one loop?

  • #2
    Code:
    foreach s in fight run drink {
         gen binary_`s' = cond(missing(freq_`s'), ., freq_`s' > 0)
    }
    replaces 3 command lines with ... 3 command lines.

    Comment


    • #3
      Thanks Nick. I actually have 8 or so variables organized like this, so it saves at least a few lines!

      Comment


      • #4
        Fine!

        Comment

        Working...
        X