Announcement

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

  • Adding missing values in a varlist

    Hi,

    In my data set answers below zero each show a different type of missing value (e.g. not answered or not asked etc. from -1 to -10) and I want to replace them all with missing values.

    I have made a global list named alist like this:

    global alist Date Age Education Employment1 Employment2 Sex Stress1 Stress2 ///
    JobControl1 JobControl2 JobControl3 JobDemand1 JobDemand2 JobDemand3 ///
    Depression1 Depression2 Depression3 Depression4 Depression5 SocialSupport1 ///
    SocialSupport2 SocialSupport3 SocialSupport4 SocialSupport5 SocialSupport6 ///
    SocialSupport7 SocialSupport8 SocialSupport9 SocialSupport10
    and then called them to be replaced like this:

    foreach var of $alist {replace `var' =. if (`var'<=0)}
    but I am getting this error that tell me :
    program error: code follows on the same line as open brace
    can any one tell me what should I change? I've been googling but could not find any way to do it.

    Many thanks and appreciation.


  • #2
    you can't, as Stata is telling, you place all that on one line; try this:
    Code:
    foreach var of $alist {
    replace `var' =. if (`var'<=0)
    }
    in general, using globals, rather than locals, is not a good idea as discussed many time in this forum

    note that there is no reason for a loop here anyway; see
    Code:
    help mvdecode

    Comment


    • #3
      Thanks a lot but when I put it as you suggested it says invalid syntax:

      foreach var of $alist {
      2. replace `var' =. if (`var'<=0)
      3. }
      invalid syntax
      r(198);
      and
      mvdecode
      is about changing missing values to numeric right? how can it be used to change numeric to missing values?

      Comment


      • #4
        Typos there.

        You need

        Code:
        foreach var of var $alist {
        or

        Code:
        foreach var of global alist {
        Looking at the help for foreach makes that clear.

        Further, Rich did mean that mvdecode might help: it's all in the same help file.
        Last edited by Nick Cox; 01 Mar 2019, 03:07.

        Comment

        Working...
        X