Announcement

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

  • keep if function

    Hi

    I would like to keep multiple VarList if the content is >0 or < 366, so I have eg DaysSinceCough and various numbers denoting the days between the symptom and diagnosis, and the next column is DaysSinceAnaemia and would like to keep across the columns those values

    I have tried

    keep if (days_since_CP, days_since_Fatigue, days_since_HV, days_since_SVCO, days_since_WL, days_since_anaemia, days_since_cough) > 0

    but obviously this doesn't work where it would work for a single variable. I am sure this is very easy but I am a novice so would appreciate if you can help. Ideally I would like a syntax that allows 0< varlist1, varlist2, varlist...<366

    Thanks!

  • #2
    No such syntax exists for what you are specifically trying to do.

    I'm a little unclear on whether you are trying to keep observations in which every variable is in the range from 0 to 366, or just those in which any variable (at least one) meets that criteria. I will assume it is the latter, but if it is not, you can modify the code accordingly. You may need to pay special attention to missing values.

    The code shows both methods, to keep if any or all variables meet the range criteria. It defines a loop that iterates over each variable name supplied in the local -myvars-. For each variable, evaluates a crterion, and updates a flag to keep the observation accordingly. At the end, you have the option to inspect the defined flag, and then use -keep- to keep those observations.

    Code:
    clear *
    cls
    
    input int(a b c)
    1 1 0
    2 1 .
    6 1 0
    3 1 2
    0 9 6
    end
    
    local myvars a b c
    quietly gen byte keep_any = 0
    quietly gen byte keep_all = 1
    quietly foreach v of varlist `myvars' {
      replace keep_any = 1 if inrange(`v', 1, 3)   // keep if any vars meet range criteria
      replace keep_all = 0 if !inrange(`v', 1, 3)  // keep only if all vars meet range criteria and none are missing
    }
    list
    
    keep if keep_any // or keep_all
    Result

    Code:
    . list
    
         +---------------------------------+
         | a   b   c   keep_any   keep_all |
         |---------------------------------|
      1. | 1   1   0          1          0 |
      2. | 2   1   .          1          0 |
      3. | 6   1   0          1          0 |
      4. | 3   1   2          1          1 |
      5. | 0   9   6          0          0 |
         +---------------------------------+

    Comment


    • #3
      Alternatively, if you want all variables in a list to be positive it is necessary and sufficient that their minimum is also positive, and min() or egen, rowmin() will get for you.

      If you want any variable in a list to be positive it is sufficient that their maximum is positive.

      I agree with Leonardo Guizzetti in being unclear how 366 fits in here, although naturally I recognise the number of days in a leap year..

      Detail: keep is a command, not a function. min() is a function. egen is a command.

      In Stata, function is not another name for command. You can regard this as pedantic if you wish but Stata terminology is pertinent on Statalist;

      Comment

      Working...
      X