Use value label in if command

前端 未结 3 1586
猫巷女王i
猫巷女王i 2021-01-05 08:06

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

Conveniently, each year uses different values for the country

相关标签:
3条回答
  • 2021-01-05 08:24

    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.

    0 讨论(0)
  • 2021-01-05 08:34

    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.

    0 讨论(0)
  • 2021-01-05 08:36

    I know I'm responding to this post years later, but I wanted to provide a solution that will work for multiple variables in case anybody comes across this.

    My task was similar, except that I had to recode every variable that had a "Refused" response as a numerical value (8, 9, 99, etc) to the missing value type (., .r, .b, etc). All the variables had "Refused" coded a different value based on the value label, e.g. some variables had "Refused" coded as 9, while others had it as 99, or 8.

    Version Information Stata 15.1

    Code

        foreach v of varlist * {        
            if `"`: val label `v''"' == "yndkr" {
              recode `v' (9 = .r)
            }
    
            else if `"`: val label `v''"' == "bw3" {
              recode `v' (9 = .r)
            }
    
            else if `"`: val label `v''"' == "def_some" {
              recode `v' (9 = .r)
            }
    
            else if `"`: val label `v''"' == "difficulty5" {
              recode `v' (9 = .r)
            }               
        }
    

    You can keep adding as many else if commands as needed. I only showed a chunk of my entire loop, but I hope this demonstrates what needs to be done. If you need to find the name of your value labels, use the command labelbook and it will print them all for you.

    0 讨论(0)
提交回复
热议问题