Announcement

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

  • Generate new variable if the string value of an existing variable is in a list

    Dear All,

    I have a dataset contains Country variable, with country names as string values. I've been trying to generate a region variable, here's what I tried:
    Code:
    gen Region = " "
    Code:
    bysort Country Year: replace Region = "South America" if Country == "Argentina" | Country == "Brazil" | Country == "Peru"...
    I'm wondering if there's are easier way, say generate Region = "South America" if Country is inlist(Argentina, Brazil, Peru...), so that I don't have to type "Country ==" part many times?

    Any help is appreciated!

    Thank you!

    Best,
    Craig

  • #2
    The bysort prefix does no harm but is not needed. You can go


    Code:
    replace Region = "South America" if inlist(Country, "Argentina", "Brazil", "Peru")
    but inlist() has small limits on the number of string arguments which bite easily. A more principled approach is to set up a small dataset with the equivalences and then merge. More at https://www.stata.com/support/faqs/d...s-for-subsets/

    Comment


    • #3
      Thank you, Nick! Both your code and the link are very helpful!

      Comment

      Working...
      X