Announcement

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

  • Generating new variable that identifies occurance of multiple integers across variables

    Hello,

    I am trying to figure out how to generate a new variable that will tell me if (e.g.) two values appear in a row, in any two of five different variables. The data are structured like this:

    v1 v2 v3 v4 v5
    1 . 2 . 3 . 8 . 10

    I want to create a new variable where:

    0=1 & 2 occur in row
    1=only 1 appears in row
    2=only 2 appears in row
    3=neither 1 nor 2 appear in row

    So in my above example, newvar=0.

    My assumption was that I would be able to accomplish this using inlist, but it seems like inlist will not work with v* (i.e., it will only let me specify a single variable, not a list of variables). Does anyone have any suggestions?

    Thank you! Stacy

  • #2

    Two ways to do it:

    Code:
    * vital that the next two commands do not include equals signs!
    local cond2 inlist(2, v1, v2, v3, v4, v5)
    local cond1 inlist(1, v1, v2, v3, v4, v5)
    
    gen wanted1 = cond(`cond1' & `cond2', 0, cond(`cond1', 1, cond(`cond2', 2, 3)))
    
    gen wanted2 = 3 - 2 * `cond1' - `cond2'
    The help for inlist() is minimal but using several variables together is tacitly allowed. There are further examples at

    https://www.stata-journal.com/sjpdf....iclenum=dm0026

    and (to the point here)

    https://www.stata-journal.com/sjpdf....iclenum=dm0058
    Last edited by Nick Cox; 19 Jul 2018, 12:52.

    Comment


    • #3
      Worked perfectly--thanks so much Nick!

      Comment

      Working...
      X