Search code examples
loopsstatastata-macros

Looping over variables with wildcards in local macros


Suppose I have a dataset of variables with the following names (note the stub of x and hm):

x9, xdog, x_99, hma8j, hm40, hm0

I want to develop a programmatic way to provide a list of variable names (which may contain wildcards) and then loop through each variable name to recode all values less than 0 with a missing value (.).

In practice I have many columns and only want to recode some of them. I do not want to use column index or ranges because I do not know them, since my data are large.

My approach involves the following steps:

  1. Create a local macro named myvars containing the variable names with wildcards

    local myvars x* hm*
    
  2. Expand the strings in the variable list to contain the full variable name strings (this should produce the original variable names):

    syntax 'myvars'
    
  3. Loop through list of variable names to set values to missing:

    foreach x of local 'myvars' { 
        replace 'x' = . if 'x' < 0
    }
    

However, I can't figure out how to include the wildcards in the for loop. The above code does not work and produces invalid syntax errors.

I found the following threads on Statalist useful, but they do not provide a solution and the use of stubs does not seem efficient:

Can anyone help me?


Solution

  • foreach x of varlist x* h* {
       replace `x'= . if `x' < 0
    }
    

    from here:

    http://www.cpc.unc.edu/research/tools/data_analysis/statatutorial/labor_saving/loops