Announcement

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

  • How to generate variable if any variable in list is certain value

    Hi, I want to create a dummy variable (0/1) if any variable from a list (ever10-ever50) is equal to 1. What would be the best way to approach this? Thank you!

  • #2
    Code:
    egen byte wanted = anymatch(ever10-ever50), values(1)

    Comment


    • #3
      Thank you, Clyde!

      Comment


      • #4
        One more question: This command codes the var "wanted" as 0 if any of the variables ever10-ever50 were missing. How would I be able to still have wanted=1 if any var in ever10-ever50 =1 but wanted=. if any var in ever10-50=.? Thanks again

        Comment


        • #5
          Code:
          egen byte wanted = anymatch(ever10-ever50), values(1)
          egen mcount = rowmiss(ever10-ever50)
          replace wanted = . if mcount > 0
          will do what you ask. I find it an odd condition to impose, and wonder why you want to do this. I can see that if an observation has no values of ever10-ever50 matching 1, and also has some missing values among those variables, one might want to set wanted to missing, as perhaps some of the missing values of the ever* variables would be 1 if we knew what they are. But to set wanted to missing across the board when any of the variables ever10-ever50 as missing seems peculiar, because if, even in the presence of some missing values of ever*, there is one or more of them that matches 1, I would think you would still want to have wanted = 1. If you find this reasoning persuasive, then the code would be:

          Code:
          egen byte wanted = anymatch(ever10-ever50), values(1)
          egen mcount = rowmiss(ever10-ever50)
          replace wanted = . if mcount > 0 & wanted == 0

          Comment

          Working...
          X