I am writing a command which takes a varlist as anargument. The variables correspond to different drugs, eg. smoking, alcohol, cannabis. There are several variables for each drug. I'd like to write a loop which takes the variables for each drug and calculates a quantity. What is the best way to extract the variables for each drug.
Can I do this using the variable name wildcards such as "*"?
Example: the dataset contains 6 variables:
Code:
june_smoke jul_smoke jun_alc jul_alc june_canabis july canabis
I want to create three variables:
Code:
total_smoke total_alc total_canabis
My program looks something like this:
Code:
prog calculate_substance_totals
syntax varlist
foreach substance in smoke alc canabis {
local substance_vars I want to define sublist of varlist ending _`substance' for a particular substance here
egen total_`substance' = rowtotal(`substance_vars')
}
end
How do I define a local macro substance_vars that contains variable names from varlist corresponding to a particular substance? Is there an alternative approach?
Many thanks,
Gordon
0 Response to Extracting a subset of a varlist using wildcards such as *
Post a Comment