Announcement

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

  • Generating a single variables for multiple values

    Hello,

    i am trying to divide the 50 states into 2 categories: "childcare" and "no childcare".

    I am assigning a value of 1 for "childcare" and 2 for "no childcare". Only 4 of the states are "childcare" and the rest are "no childcare." I already generated the variable "childcare" with this line of code:

    gen childcare=.
    replace childcare=1 if state==1 & state==43 & state == 27 & state == 21

    for "no childcare," which is the remaining 46 states, is there an easier way to generate a variable without coding every state by hand?

  • #2
    If I knew that all the other states had no childcare, I wouldn't generate the variable equal to missing.

    Code:
    gen childcare=0
    replace childcare=1 if inlist(state,1,21,27,43)
    Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

    When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

    Comment


    • #3
      Thank you so much!

      Comment


      • #4
        And even shorter:

        Code:
        gen childcare= inlist(State,1,21,27,43)

        Comment


        • #5
          thank you!

          Comment

          Working...
          X