Announcement

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

  • Help Generating Dummy Variable WestCoast

    Dear all,

    I am using Stata 16, on mac. Here is a snapshot of my dataset which consists of population and GDP for all 50 states over a the period of three years 2016-2018. I want to create a dummy variable called WestCoast = 1 if the State is California, Oregon, or Washington and 0 if otherwise but I do not know how to do so.

    Click image for larger version

Name:	Screen Shot 2019-11-23 at 3.29.06 PM.png
Views:	1
Size:	80.2 KB
ID:	1526135



    Thank you in advance for your help

    Jason Browen

  • #2
    This should work, but be sure to match exactly what is inside the quotes to the state names you have.

    Code:
    gen WestCoast = inlist(state, "California", "Oregon", "Washington")

    Comment


    • #3
      I copied and pasted the code you mentioned into Stata. Unfortunately it does not work

      Comment


      • #4
        Hi Jason, when you say it didn't work, what do you mean and what, exactly, happened? I suspect it's either a misspelling somewhere or there were extra spaces in the state.

        Code:
        dataex state
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str11 state
        "Alabama"    
        "Alaska"    
        "Arizona"    
        "California"
        "Colorado"  
        "Connecticut"
        "Delaware"  
        "Florida"    
        "Georgia"    
        "Hawaii"    
        "Idaho"      
        "Indiana"    
        "Maine"      
        "Michigan"  
        "Ohio"      
        "Oregon"    
        "Washington"
        end
        
        replace state = trim(itrim(state))  // this will remove any extra spaces
        gen WestCoast = inlist(state, "California", "Oregon", "Washington")
        gen midwest = inlist(state, "Michigan", "Indiana", "Ohio")
        
        ** Same as above, but using two lines and using the | ("pipe") for "or"
        gen east = 0
        replace east = 1 if state=="Connecticut" | state=="Delaware" | state=="Maine"
        
        . list, noobs abbrev(12)
        
          +------------------------------------------+
          |       state   WestCoast   midwest   east |
          |------------------------------------------|
          |     Alabama           0         0      0 |
          |      Alaska           0         0      0 |
          |     Arizona           0         0      0 |
          |  California           1         0      0 |
          |    Colorado           0         0      0 |
          |------------------------------------------|
          | Connecticut           0         0      1 |
          |    Delaware           0         0      1 |
          |     Florida           0         0      0 |
          |     Georgia           0         0      0 |
          |      Hawaii           0         0      0 |
          |------------------------------------------|
          |       Idaho           0         0      0 |
          |     Indiana           0         1      0 |
          |       Maine           0         0      1 |
          |    Michigan           0         1      0 |
          |        Ohio           0         1      0 |
          |------------------------------------------|
          |      Oregon           1         0      0 |
          |  Washington           1         0      0 |
          +------------------------------------------+

        Comment


        • #5
          The code in post #2 does not work because the variable state is not a character variable. It is a numeric variable with value labels produced by encoding a string variable. (See earlier posts by the author.)

          Comment


          • #6
            Jason Browen Please back up and study https://www.statalist.org/forums/help#stata to appreciate that screenshots are not as helpful as you hope. Use dataex to show data examples. The colour coding of your state variable is a subtle clue which underlies the diagnosis in #5. But even experienced users missed it. So, the result is a slower route to a correct answer for you and a small waste of time and effort.

            Comment


            • #7
              Thank you all so much for all of your help! I figured out what my problem was. I encoded the state variable. Now it works

              Comment

              Working...
              X