Announcement

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

  • generate a variable with if condition

    Dear community,

    I am a real newbie to both Stata and this forum so please be patient with me and my easy question. At least I hope it is an easy one :-). I have a dataset with a string variable (names of districts) and would like to generate a new variable with the ISO codes of these districts. I found out how to generate the variable for only one of the strings:

    gen district_code=102 if locationlevel2=="KAMPALA"

    So now I would need to extend this by a list somehow to have something like: gen district_code=[102, 314] if locationlevel2==["KAMPALA", "ABIM"] that generates district_code that is 102 for Kampala, 314 for Abim etc. but I cannot figure out the right syntax for that. I also tried smth like gen district_code=0 and then if locationlevel2=="KAMPALA" {district_code=102} but that didn't work either.

    I hope this question is understandable and easy to answer and wanna thank you in advance for your help!

    Best, Tabea

  • #2
    You might want to check -dataex- for how you can post examples of your data. Now it is not clear to me where you have those ISO codes, and how is it that you know the mapping from Districts to ISO codes. (Where and in what form is that mapping contained?)

    For what I know so far from what you are saying, it might be useful for you to check how to do parallel lists in Stata, otherwise known as "cat says miaw, dog says djaf, cow says mooo, pigs says gruhgrug"

    https://www.stata.com/support/faqs/p...arallel-lists/

    Comment


    • #3
      I presume there are many different districts and many corresponding codes. For that kind of situation, I'd approach your problem as a -merge-. This would require that you construct a separate data file in which each observation contains a district name and corresponding code. Then, you could use -merge- with district name as the key by which to merge the corresponding code onto each observation in your original data set.
      Loops with parallel lists, as Joro points to, would provide a nice solution, if you have a relatively small number of names and codes. My guess, though, is that you have a large number of names and codes.

      Comment

      Working...
      X