L.Vaiz
L.Vaiz

Reputation: 13

Taking advantage of the name of a variable in Stata

I´m using Stata and I have a set of variables named cal1, cal2, cal3 and so on until cal21. For every line of my dataset, i could have more or less cal* variables as non-missing (I designed the dataset with a reshape wide). I want to generate a new variable that returns the maximum name of variable cal* available for each line that is non-missing. For example, if line 1 has until cal3 as non- missing , this variable returns cal3; for the line 2 if i have cal1, cal2 and cal6, I want cal6. Is there a way to do this?

Upvotes: 0

Views: 62

Answers (1)

dimitriy
dimitriy

Reputation: 9460

This would be much easier to accomplish with data in long format layout, but it is doable with wide data too with a loop:

gen max_cal = "none"
forvalues v=1/21 {
    replace max_cal = "cal`v'" if !missing(cal`v')
}

This will update the max_cal variable each time there's a higher one not missing.

Upvotes: 1

Related Questions