Anna Jones
Anna Jones

Reputation: 101

How to use * in Stata?

I am wondering how I can strategically use the * in Stata to make my do-file cleaner.

I'd like to replace values of 999 with missing (.) for a long list of variables, one for each year.

Here is my existing code:

replace age_1997 = . if age_1997 == 999
replace age_1998 = . if age_1998 == 999
replace age_1999 = . if age_1999 == 999

Is there some way to do this more efficiently? I have tried the following, but it did not work.

replace age* = . if age* == 999

Upvotes: 1

Views: 223

Answers (1)

Nick Cox
Nick Cox

Reputation: 37208

There is a special-purpose command for this problem

mvdecode age*, mv(999) 

which isn't necessarily faster than a loop:

foreach v of var age* { 
    replace `v' = . if `v' == 999 
} 

Note that help replace is explicit: the command only works on one variable at a time.

Upvotes: 3

Related Questions