Search code examples
stata

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

Solution

  • 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.