Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use value label in if command

Tags:

stata

I am working with a set of dta files representing surveys from different years.

Conveniently, each year uses different values for the country variable, so I am trying to set the country value labels for each year to match. I am having trouble comparing value labels though.

So far, I have come up with the following code:

replace country=1 if countryO=="Japan"
replace country=2 if countryO=="South Korea" | countryO=="Korea"
replace country=3 if countryO=="China"
replace country=4 if countryO=="Malaysia"

However, this doesn't work because "Japan" is the value label, not the actual value.

How do I tell Stata that I am comparing the value label?

like image 947
jamzsabb Avatar asked Mar 31 '14 17:03

jamzsabb


2 Answers

Try

replace country=1 if countryO=="Japan":country0valuelabel
replace country=2 if inlist(countryO,"South Korea":country0valuelabel,"Korea":country0valuelabel)

You will have to replace country0valuelabel with the corresponding value label name in your data. You can find out its name by looking at the penultimate column in the output of describe country0.

like image 103
dimitriy Avatar answered Oct 13 '22 00:10

dimitriy


To complement @Dimitriy's answer:

clear all
set more off

sysuse auto
keep foreign weight

describe foreign
label list origin

replace weight = . if foreign == 0

list in 1/15
list in 1/15, nolabel 

describe displays the value label associated with a variable. label list can show the content of a particular value label.

like image 39
Roberto Ferrer Avatar answered Oct 13 '22 00:10

Roberto Ferrer