Announcement

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

  • Replace variable if command

    I have a set of European regions for many different years and I would like to create a variable which identifies each region based on the country it belongs. A small sample of my data looks like this: (note that I have 12 different countries, 100+different regions and my the time period is from 2000 to 2017):

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int time str78 geo str6 value str3 countrycode
    2000 "Baden-Württemberg"                  "26,300" "."
    2000 "Stuttgart"                           "29,500" "."
    2000 "Rheinland-Pfalz"                     "20,600" "."
    2000 "Koblenz"                             "19,500" "."
    2000 "Trier"                               "17,700" "."
    2000 "Rheinhessen-Pfalz"                   "22,200" "."
    2000 "Saarland"                            "21,100" "."
    2000 "Sachsen"                             "15,100" "."
    end
    I have generated a countrycode variable and assumed no value for it for the time being.
    I would like to input the country code for each region without having to manually select each region as such:

    Code:
    replace countrycode="LUX" if geo=="Luxembourg"
    How do I select a group of regions belonging to the same country and name the variable countrycode with the initials of each country? I tried using this but it does not appear to work (Greece has 13 regions and the country code for Greece is GR):
    Code:
    replace countrycode="GR" if inlist(geo,Anatoliki Makedonia Thraki,Kentriki Makedonia,Dytiki Makedonia,Ipeiros,Thessalia,Ionia Nisia,Dytiki Ellada,Sterea Ellada,Peloponnisos,Attiki,Voreio Aigaio,Notio Aigaio,Kriti)



    Last edited by George Kollias; 04 Jul 2019, 04:38.

  • #2
    There are two problems here -- in fact three.

    1. inlist() requires double quotes at least. Here is a silly example:

    Code:
    clear
    input str3 what
    "cat"
    "dog"
    "emu"
    "fox"
    end
    
    list if what == "cat"
    
         +------+
         | what |
         |------|
      1. |  cat |
         +------+
    
    list if inlist(what, "cat", "dog")
    
         +------+
         | what |
         |------|
      1. |  cat |
      2. |  dog |
         +------+
    2. There is a limit on how many string values you can supply. See https://www.stata.com/help.cgi?inlist for the limit of 10 string values.

    3. "does not appear to work" is not an informative problem report. See FAQ Advice #12.

    Never say just that something "doesn't work" or "didn't work", but explain precisely in what sense you didn't get what you wanted.
    In practice, creating a file with translations and using merge is likely to be quicker. See https://www.stata.com/support/faqs/d...s-for-subsets/ for some advice.

    Comment


    • #3
      Thank you for the fast reply! With regards to number 3. I get this error message:
      Code:
      replace countrycode="GR" if inlist(geo, Anatoliki Makedonia Thraki, Kentriki Makedonia, Dytiki Makedonia, Ipeiros,Thessalia, Ionia Nisia, Dytiki
      >  Ellada, Sterea Ellada, Peloponnisos, Attiki, Voreio Aigaio, Notio Aigaio, Kriti)
      Kriti not found
      r(111);
      Correcting for number 1 and number 2 do infact solve my problem, furthemore I will take your suggestion in using the merge command, thank you so much!

      Comment


      • #4
        Stata was looking for a variable called Kriti and couldn't find one. That's why the double quotes are needed to underline that you are looking for literal string values.

        Comment


        • #5
          Thank you so much for the clarity in the explanation! everything works now

          Comment

          Working...
          X