Announcement

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

  • Help generating region variable

    Dear all,

    I am using Stata 16, on mac. I want to create a region variable based on my dummy variables west, ne, midwest, and south, but I am not sure how to do so. south is my reference category. I included the commands I used to construct my dummy variables along with a snapshot of my dataset.

    generate west = 0

    replace west =1 if 2.state | 3.state | 5.state | 6.state | 12.state | 13.state | 27.state | 29.state | 32.state | 38.state | 45.state | 48.state | 51.state

    generate ne = 0

    replace ne =1 if 7.state | 20.state | 30.state | 22.state | 31.state | 33.state | 39.state | 40.state | 46.state

    generate midwest = 0

    replace midwest =1 if 14.state | 15.state | 16.state | 17.state | 23.state | 24.state | 26.state | 28.state | 35.state | 36.state | 42.state | 50.state

    generate south = 0

    replace south =1 if 1.state | 4.state | 8.state | 10.state | 11.state | 18.state | 19.state |21.state | 25.state | 37.state | 34.state | 41.state | 43.state | 44.state | 47.state | 49.state | 48.state | 9.state

    Click image for larger version

Name:	Screen Shot 2019-12-01 at 11.03.21 AM.png
Views:	1
Size:	22.3 KB
ID:	1527059
    Click image for larger version

Name:	Screen Shot 2019-12-01 at 11.05.23 AM.png
Views:	1
Size:	11.2 KB
ID:	1527060



    Thank you in advance for your help



    Jason Browen
    Attached Files

  • #2
    Already answered in your earlier thread,

    Please don’t ask the same question in different threads.

    Please do read the FAQ Advice to see advice on screenshots.

    Comment


    • #3
      Sorry about that I did not know

      Comment


      • #4
        Instead of generating four dummy variables, generate your region variable directly by modifying your existing code, and you won't need to generate dummy variables because you can use factor variable notation in your models. Note the use of the inlist() function to simplify the if clause.
        Code:
        generate region = .
        // west
        replace region = 1 if inlist(state,2,3,5,6,12,13,27,29,32,38,45,48,51)
        // northeast
        replace region = 2 if inlist(state,7,20,30,22,31,33,39,40,46)
        // midwest
        replace region = 3 if inlist(state,14,15,16,17,23,24,26,28,35,36,42,50)
        // south 
        replace region = 4 if inlist(state,1,4,8,10,11,18,19,21,25,37,34,41,43,44,47,49,48,9)
        label define REGION 1 "West" 2 "Northeast" 3 "Midwest" 4 "South"
        label values region REGION
        // make sure nothing was missed
        assert region<.
        Untested code. Please do read the advice on giving usable sample data with the dataex command rather than pictures of data.

        Comment

        Working...
        X