I have a dataset with different strings in different variables. The variables are v1-v3. In one of v1-v3, will be a lowercase state. But in each obs, the state will/could be in a different cell. (In reality there are more than 3 variables.) The variables (v1-v3) that do not have state, have other string values inside them. An example dataset is here.
I'd like to create a variable called state and fill it with the state from v1-v3. The solution to the dataset would be:
This is my attempt to solve the problem, but it doesn't work. (I am not great at loops.)
Thanks in advance.
Code:
clear input str20 v1 str20 v2 str20 v3 "zippy" "pancake" "ohio" "indiana" "sidewinder" "billy" "waffles are good" "new york" "susie" end
I'd like to create a variable called state and fill it with the state from v1-v3. The solution to the dataset would be:
Code:
clear input str20 v1 str20 v2 str20 v3 str20 state "zippy" "pancake" "ohio" "ohio" "indiana" "sidewinder" "billy" "indiana" "waffles are good" "new york" "susie" "new york" end
This is my attempt to solve the problem, but it doesn't work. (I am not great at loops.)
Code:
local states /// "alabama" "alaska" "arizona" "arkansas" "california" "colorado" /// "connecticut" "delaware" "florida" "georgia" "hawaii" "idaho" /// "illinois" "indiana" "iowa" "kansas" "kentucky" "louisiana" /// "maine" "maryland" "massachusetts" "michigan" "minnesota" /// "mississippi" "missouri" "montana" "nebraska" "nevada" /// "new hampshire" "new jersey" "new mexico" "new york" /// "north carolina" "north dakota" "ohio" "oklahoma" "oregon" /// "pennsylvania" "rhode island" "south carolina" "south dakota" /// "tennessee" "texas" "utah" "vermont" "virginia" "washington" /// "west virginia" "wisconsin" "wyoming" gen state = "" forval i = 1/3 { foreach s of local states { replace state = v`i' if lower(trim(v`i')) == "`s'" & state == "" } }
Comment