Hi
I have a question on how to destring and generate hundreds of variables that have a similar name. I would prefer to run as little code as possible to reduce duplication so any help is appreciated.
I need to destring a variable and save it as new variable with three additional letters in original variable name. The three letters are included after the first two letters but before the variable number. The number is either a single number or in some cases a number with an _letter/number.
In the following example I have a list of variables (aa*) that I need to destring and save as a new variable (aaide*). The foreach command runs all variables that do not have an underscore (accounting for 3 and 13 which do not exist). A program is the only way I can figure out how to run this process on the variables with an underscore after the number. This seems really inefficient and not ideal.
foreach i of num 1/18{
if inlist(`i',3,13) continue
clonevar aaide`i'=aa`i'
destring aaide`i', replace
}
capture program drop num
program define num
destring `1', generate (aaide`2')
end
num aa5_t 5_t
num aa11_1 11_1
num aa11_2 11_2
num aa11_3 11_3
num aa12_1 12_1
.
.
.
num aa18_11 18_11
num aa18_t 18_t
.
.
.
etc
I am currently using version 15.1 (upgrading soon to version 16).
Any assistance is greatly appreciated!
Thanks in advance for your time
0 Response to Destring and generate hundreds of variables with an underscore in the name
Post a Comment