Announcement

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

  • Flow Control

    Hi all!

    I'm trying to create a situation where I can check a set of country/state names (education_SP) to see if they are US states, and then if they are not US states, to populate a (mostly blank) second variable (education_country) with that country name. If they do end up being US states, I'll put "USA" in the new variable.

    I put the code I'm attempting below.

    The prime thing I'm struggling with is working out how to create a dictionary of the 50 US States (will include PR, DC, and Guam) that I can use a logical operator to check against.

    Thanks so much for your time; this site has been a pretty great learning resource for me.


    Code:
    local var stateList = {"Alabama", "Alaska", "Arizona", "Arkansas"}
    
    replace education_country = education_SP if education_country == "" & education_SP ! in local(stateList)
    replace education_country = "USA" if education_country == "" & education_SP in local(stateList)



  • #2
    I'm a little confused so this may or may not help: why not create a separate dataset of just the names of the states (and territories, etc.) and then use -merge- to locate observations with/without a valid state and then use your replace command with an "if" qualifier based on the value of the _merge variable (or whatever name you decide to give that variable); see
    Code:
    help merge

    Comment


    • #3
      I second Rich's overall suggestion. If you have Stata 16 or newer, then you have access to frames. Using frames allows you to do the same thing as Rich suggested. I sketch an example using toy datasets below. There, anything observation that doesn't have a corresponding match in a state information dataset (correlates to the Names frame below), then the not_matched flag is equal to 1 and you can replace values based on that condition. This method is a common "lookup" method when the lookup values are known/defined in advance, or may be too numerous and cumbersome to work with using local macros.

      Code:
      clear *
      cls
      
      frame create Data
      frame change Data
      input byte id
      1
      1
      2
      3
      5
      10
      end
      
      frame create Names
      frame change Names
      input byte id str1 name
      1 "A"
      2 "B"
      3 "C"
      4 "D"
      5 "E"
      end
      
      frame change Data
      frlink m:1 id , frame(Names) gen(lname)
      
      gen byte not_matched = mi(lname)
      
      * pull in name, if you like
      frget name, from(lname)
      drop lname
      
      list, sep(0) abbrev(12)

      Comment

      Working...
      X