Announcement

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

  • How to group values within a variable?

    Hi all,

    I'm currently doing a project on the 50 US states and want to group them according to region, making it easier to view them in a graph etc. My variable state is a string variable with each state as an observation. Would anyone be able to help?

    I want to keep the original variable the same with all the states as individual observations.

    Thanks!

  • #2
    Do region and state refer to the same entity? Or, do you want to group the states based on their geography etc?

    Without more details, it is hard to suggest a command. But, group() function is often helpful for grouping.

    Comment


    • #3
      I agree with Navid Asgari More detail is needed, especially for people outside the US (many people here) or who can't see your data (all people here).

      The slow way is likely to be as good as any. At worst it's four lines with some flavour like this

      Code:
      gen region = 1 if inlist(state, "ME", "VT", "NH", "MA", "CT") | inlist(state, "NY", "NJ") 
      ...
      replace region = 4 if inlist(state, "CA", "OR"", "WA") 
      See

      Code:
      help inlist() 

      which can be used for numeric arguments too, but note the limit on the number of string arguments in any one call.

      Comment


      • #4
        While this is not my post - i do have a similar question:

        I want to group states by whether they expanded Medicaid during the 2010 health care reform or did not.

        I changed statenameabr to a numeric variable. I thought this would be helpful since the -inlist- command can 2 and 255 arguments for reals but limited for string var.

        Code:
        encode statenameabr, gen (statenameabr1)
        I am proposing the following to generate a 0/1 dummy for statemcaidexp where 1 = 37 states that expanded medicaid and 0 = 14 states that did not expand.

        Code:
        gen statemedicaid = 1 if inlist (statenameabr1, "AL", "AS", "AZ", "CA",....)
        My end goal is that I can look at the distribution of education, income, insurance status of people in states that have expanded medicaid compare to those that did not. I am thinking once I create the statemcaidexp variable i should be able to do that.

        Comment

        Working...
        X