Search code examples
statastata-macros

In Stata: how to evaluate a string stored in a macro as a variable name?


In a Stata program, I'm trying to get a variable name, modify it using regexr(oldvarname, "some_regex", "replacement"), store it in a new local local newvarname, and use that modified variable name in a replace othervar = 1 if `newvarname' == 1 statement. Below is a minimal example of what I would like to achieve.

program define testprog
  syntax varlist
 

  foreach var of varlist `varlist' {
    local bin_check regexm("`var'", "bin(bot|mid|top)$")

    if `bin_check' {
        local varfreq regexr("`var'", "bin(bot|mid|top)$", "freq")
    }
    else {
        local varfreq `var'freq
    }
    
    gen `var'_wk = .
    replace `var'_wk = 1 if `varfreq' == 1
  }
end

gen joboffers_earn = 0
gen joboffers_earnbinbot = 0
gen joboffers_earnfreq = 1

testprog joboffers_earn joboffers_earnbinbot

In this example, varfreq is interpreted correctly for the first variable (which involves appending "freq" to the end of the variable), but not the second variable (which involves a slightly more complex replacing of substrings in the variable name).

In short, how do I get Stata to evaluate the result of regexr() as a variable name instead of a string?


Solution

  • The problem lied in the macro assignment: for the expression to be evaluated, it has to be assigned using the = operator. Without it, the content of the macro is stored as a literal string.

    Thus, local varfreq regexr("`var'", "bin(bot|mid|top)$", "freq") needs to be changed to local varfreq = regexr("`var'", "bin(bot|mid|top)$", "freq").

    The fully working code becomes:

    program define testprog
      syntax varlist
     
    
      foreach var of varlist `varlist' {
        local bin_check regexm("`var'", "bin(bot|mid|top)$")
    
        if `bin_check' {
            local varfreq = regexr("`var'", "bin(bot|mid|top)$", "freq")
        }
        else {
            local varfreq `var'freq
        }
        
        gen `var'_wk = .
        replace `var'_wk = 1 if `varfreq' == 1
      }
    end
    
    gen joboffers_earn = 0
    gen joboffers_earnbinbot = 0
    gen joboffers_earnfreq = 1
    
    testprog joboffers_earn joboffers_earnbinbot
    

    H/T to Clyde for his answer on Statalist.