Announcement

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

  • How to generate a new variable coded 1 if a certain number is included in other variables

    Hi

    I am trying to add a variable to the below data by coding 1 if it includes "7011" between variables code1 and code8. It is coded 0 if it does not include "7011" or missing. Do you know any relevant Stata command for this? I thought the egen command with an if statement.

    Click image for larger version

Name:	code.JPG
Views:	1
Size:	37.3 KB
ID:	1659715


  • #2
    Code:
    gen wanted = 0
    foreach v of var code1-code8 {
    replace wanted = 1 if `v' == 7011
    }
    Code:
    reshape long code, i(id)
    egen wanted = max(code == 7011), by(id)
    reshape wide
    Last edited by Øyvind Snilsberg; 15 Apr 2022, 02:13.

    Comment


    • #3
      Øyvind Snilsberg Thank you very much.

      Comment

      Working...
      X