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
Related Posts with generate a variable with if condition
advice on using duplicates drop commandHello, I would like to make use of "duplicates drop" command to drop out certain rows, For example,…
Interaction term and lincomHi all, I am running a basic interaction model y=b0 + b1X1 + b2D + b3X1*D. where D is a dummy varia…
forvalues with indefinite end valueI would like to use a do loop to read in a series of excel files, save them as .dta files, then appe…
Xtreg vs REGFEHD, time varied weightsI am running a panel regression with the returns of various mutual funds as a function of time varie…
generate dummy variable using strposHi everyone! I have searched this forum and read the official stata help section, but I'm still not…
Subscribe to:
Post Comments (Atom)
0 Response to generate a variable with if condition
Post a Comment