Announcement

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

  • Invalid name in loop foreach replace command

    Hello,

    I have a dataset which includes a specific questionnaire (105 variables, all numeric) and I would like to generate a variable data with 0 unless any of the 105 variables has a value than I would like a 1. All 105 variables end with _CLEAN, the dataset contains more variables, but this can be used as an identifier. I thought it would be fasted to use a foreach and replace command.

    Code:
    gen data =0
    
    foreach x of *_CLEAN {
    replace data = 1 if `x' <10
    }
    Stata returns an error : '*_CLEAN invalid name'

    But I don't understand why it's an invalid name as I used the following loop without any problems.

    Code:
     foreach x in *_CLEAN{
     mdesc `x'
    }

    Any guidance would be much appreciated!
    Kind regards,
    Rosa



  • #2
    Thanks for using CODE delimiters. Unfortunately, you're posting in the wrong forum. Use this forum to make test posts, practice with the formatting features of the forum software, etc. Post questions about Stata in the General Forum. Before you do, be sure to read the FAQ,especially FAQ 12 about how to ask a good question.
    Steve Samuels
    Statistical Consulting
    [email protected]

    Stata 14.2

    Comment

    Working...
    X