Announcement

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

  • creating a variable that=1 if another variable equals any of many things

    Hi,

    I am trying to create a variable in the following way:
    gen x=(y==1|y==2|y==3)

    However, there are many different things that y can equal where x should =1. Is there a more efficient way to do this? Something like gen x=1 if y in the list (1,2,3,4,5,6,7,....) etc? Thanks.

    Code:
    gen nonhosp_detox_rehab=(service_code=="H2034" | service_code=="H0013" | service_code=="H0012" | service_code=="H0012-TG" | service_code=="H2036")

  • #2
    Code:
    gen nonhosp_detox_rehab= inlist(service_code,"H2034" , "H0013", "H0012","H0012-TG" ,"H2036")
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3

      This might get you started:
      Code:
      gen nonhosp_detox_rehab= 1 if inlist(service_code, "H2034","H0013","H0012")

      Comment


      • #4
        See also https://journals.sagepub.com/doi/pdf...36867X19830921 for an overview of indicator variable creation, including the standard advice that (1, 0) indicators are usually much more useful than (1, .) indicators.

        Comment


        • #5
          Thank you all!

          Comment

          Working...
          X