Announcement

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

  • Create binary variable

    Hi Statalist,

    I have question regarding the binary variable. This is the example of my data.
    Code:

    Code:
    input int str4 str4 str4 str4 str4 (ID grp1 grp2 grp3 grp4 grp5)
    1 L30D I31A G10H J14I B25D
    2 I31A V67Z L30D K25M O29H
    3 F13W H76E P66Q M43F A12D
    4 P14F V20K I36M O16F M35I
    5 Q24R E38V H84A L30D W59M
    6 I46R L30D M36T H99R L30D
    7 L30D B36T L30D L30D N78Z
    end
    How can I create a binary variable for each ID that takes the value of "1" if the "grp*" variables have the "L30D" value, and "0" otherwise? And how can I do it if i have 1000 "grp*" variables in stead of just 5 in this example?

    Thank you so much for your help

  • #2
    Code:
    reshape long grp, i(ID)
    by ID, sort: egen wanted = max(grp == "L30D")
    reshape wide
    Consider omitting the final -reshape wide-, since most data management and analysis in Stata are easier with a long data layout. But if you have good reasons to return to a wide layout, there it is.

    Comment


    • #3
      Thanks Clyde!

      Comment

      Working...
      X