Use value label in if command

前端 未结 3 1605
猫巷女王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: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.

提交回复
热议问题