Hello,
In a program, I'm trying to get a variable name, modify it using `regexr(oldvarname, "some_regex", "replacement")`, store it in a new local "newvarname", and use that modified variable name in a `replace ... if [newvarname] == 1` statement. Below is a minimal example of what I would like to achieve.
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?
In a program, I'm trying to get a variable name, modify it using `regexr(oldvarname, "some_regex", "replacement")`, store it in a new local "newvarname", and use that modified variable name in a `replace ... if [newvarname] == 1` statement. Below is a minimal example of what I would like to achieve.
Code:
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 short, how do I get Stata to evaluate the result of `regexr()` as a variable name instead of a string?
Comment