Announcement

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

  • Recoding variables using commando loops

    Hi Stata Forum,

    I'm trying to recode a list of variables using foreach. The variables have some categories i want to code as missing, some of them, however, have a category the others don't have. What I tried to do was the following:

    foreach X of var_* {
    recode X 98 99 -1 =.
    }


    I'm not sure if the problem is that not all of var_* have the -1 category, or if I'm using foreach wrong.

    Thank you in advance
    Kasper Loven




  • #2
    You don't need a -foreach- loop, as -recode- takes a varlist. (See the syntax diagram in -help recode-.)
    Code:
    recode var_* (98 99 -1 = .) // Note also that standard recode syntax uses ()
    Regarding your -foreach- loop: Uses of the local macro X inside the loop must be dereferenced using quotes, as in `X' (note the first mark must be a "backquote.") See the "Loop over existing variables" example under -help foreach-.

    Comment


    • #3
      Thank you!

      Comment

      Working...
      X