Announcement

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

  • Generating a binary variable conditional on multiple values from another variable, to create Southern and Northern states.

    I have a set of survey data, where I have the "Statefip code" for the US state an individual lives in. I want to create two binary variables:

    1) NorthernStates: which has a value of 1 if they live in a "northern" state, and 0 otherwise
    2) SouthernStates: which has a value of 1 if they live in a "southern state, and 0 otherwise

    ID Statefip Northern Southern
    234 1 0 1
    546 12 0 1
    876 18 1 0
    432 33 1 0
    156 6 0 0


    The definition I'm using for Southern state is when Statefip equals 1, 5, 12, 13, 22, 28, 37, 45, 47, 48, 51. These numbers correspond to Alabama, Arkansas, Florida, Georgia, Louisiana, Mississippi, North Carolina, South Carolina, Tennessee, Texas, Virginia.


    The definition I'm using for Northern state is when Statefip equals 9, 10, 17, 18, 23, 24, 25, 26, 33, 34, 36, 39, 42, 44, 50, 55. These numbers correspond to delaware, Illinois, Indiana, Maine, Maryland, Massachusetts, Michigan, New Hampshire, New Jersey, New York, Ohio, Pennsylvania, Rhode Island, Vermont, Wisconsin.

    So for a west-coast or midwestern state (for example, California where Statefip==6) would equal 0 for both Southern and Northern variables.


    Does anyone have a more elegant way to create these variables than me manually creating dummies for each state, and then adding them up to create the Southern and Northern states.


  • #2
    Code:
    gen byte southern_state = inlist(Statefip, 1, 5, 12, 13, 22, 28, 37, 45, 47, 48, 51)
    gen byte northern_state = inlist(Statefip, 9, 10, 17, 18, 23, 24, 25, 26, 33, 34, 36, 39, 42, 44, 50, 55)

    Comment


    • #3
      Mr Schechter, thank you so much. The functions "byte" and "inlist" were not ones I was ever taught in class. This is great, as it also allows to me quickly and easily expand my selection criteria--I am currently debating whether to include Kentucky as a Southern or Northern State, as Kentucky is regarded on the map as a "Southern" state (it does border Virginia), yet Kentucky fought for the Union during the Civil War. But with your code I can easily add the state with little fuss to either side.

      Thank you once again for the eloquent coding.

      Comment

      Working...
      X