Announcement

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

  • check whether variable contains specific realization

    Hi all,

    I would like to check (in an if environment) whether a certain value of var2 appears somewhere in var1 and consequently mark it in var3;
    If possible also using a by operator to check that for specific region within var1.

    Thus if there is an 8 of 5 in var1, set var3 to 1 in the specific row as can be seen below:
    var1 var2 var3
    0
    1
    2
    3 8 1
    4
    5 10
    6
    7 5 1
    8 11
    9















    I don't know if there is a simple expression or if I have to go there "by foot" with some help-variables.

    Thanks
    Tim

  • #2
    Check out countmatch (SSC).

    Comment


    • #3
      Code:
      generate var3=inlist(var2,5,8)

      Comment


      • #4
        @ Nick:
        Thank you. This command only counts though, but maybe I can work with it.

        @ Sergiy
        I don't think that will help. It's not about 5 and 8 but a variable containing specific values.

        Comment


        • #5
          As I understand it, countmatch does more than you want, so you just throw away what you don't want. Otherwise put, you reduce a count to an indicator.

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input byte(var1 var2 var3)
          0  . .
          1  . .
          2  . .
          3  8 1
          4  . .
          5 10 .
          6  . .
          7  5 1
          8 11 .
          9  . .
          end
          
          countmatch var2 var1, gen(wanted)
          
          list 
          
               +-----------------------------+
               | var1   var2   var3   wanted |
               |-----------------------------|
            1. |    0      .      .        . |
            2. |    1      .      .        . |
            3. |    2      .      .        . |
            4. |    3      8      1        1 |
            5. |    4      .      .        . |
               |-----------------------------|
            6. |    5     10      .        0 |
            7. |    6      .      .        . |
            8. |    7      5      1        1 |
            9. |    8     11      .        0 |
           10. |    9      .      .        . |
               +-----------------------------+
          
          replace wanted = cond(wanted > 0, 1, .) 
          
          list 
          
               +-----------------------------+
               | var1   var2   var3   wanted |
               |-----------------------------|
            1. |    0      .      .        . |
            2. |    1      .      .        . |
            3. |    2      .      .        . |
            4. |    3      8      1        1 |
            5. |    4      .      .        . |
               |-----------------------------|
            6. |    5     10      .        . |
            7. |    6      .      .        . |
            8. |    7      5      1        1 |
            9. |    8     11      .        . |
           10. |    9      .      .        . |
               +-----------------------------+

          Comment


          • #6
            Sorry, I read "Thus if there is an 8 of 5 in var1, set var3 to 1 in the specific row" as "Thus if there is an 8 or 5 in var1, set var3 to 1 in the specific row."

            Perhaps there is still a typo in the original description of the task, but with your clarification it is a job for isknown.
            http://www.radyakin.org/stata/isknown/

            Comment


            • #7
              Perhaps there's more educational benefit in showing how to solve this using merge. I tweaked the problem to add a region variable since Tim suggested that the matching could be done by region.

              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input byte(region var1 var2)
              1 0  .
              1 1  .
              1 2  .
              1 3  8
              1 5 10
              1 6  .
              1 7  5
              1 8 11
              1 9  .
              1 .  .
              2 2  .
              2 2  .
              2 3  4
              2 4  .
              2 5  9
              2 6  .
              end
              save "statalist_data.dta", replace
              
              * reduce to one observation per distinct non-missing values of var1
              keep region var1
              bysort region var1: keep if _n == 1 & !mi(var1)
              
              * match distinct values of var1 to values of var2
              rename var1 var2
              merge 1:m region var2 using "statalist_data.dta", keep(match using)
              
              * create an indicator when values matched
              gen wanted = _merge == 3
              
              * clean up and list results
              drop _merge
              sort region var1
              order region var1 var2 wanted
              list, sepby(region)
              and these are the results
              Code:
              . list, sepby(region)
              
                   +-------------------------------+
                   | region   var1   var2   wanted |
                   |-------------------------------|
                1. |      1      0      .        0 |
                2. |      1      1      .        0 |
                3. |      1      2      .        0 |
                4. |      1      3      8        1 |
                5. |      1      5     10        0 |
                6. |      1      6      .        0 |
                7. |      1      7      5        1 |
                8. |      1      8     11        0 |
                9. |      1      9      .        0 |
               10. |      1      .      .        0 |
                   |-------------------------------|
               11. |      2      2      .        0 |
               12. |      2      2      .        0 |
               13. |      2      3      4        1 |
               14. |      2      4      .        0 |
               15. |      2      5      9        0 |
               16. |      2      6      .        0 |
                   +-------------------------------+

              Comment

              Working...
              X