Hi all,
In order to be able to run a do-file on different datasets, I'm creating locals with variables' names that are as generic as possible in the following way:
The reason for this is that the potential datasets have a common structure in labeling variables but there might difference in the middle part. Thus, I put a * to be able to include all the variables that have the corresponding structure.
Later on in the do-file, I need to count the number of variables in each local (as an input for something else) and I've started coding the following loop:
However, it's not doing what I want. I set trace on and, for instance, for the first round of the loop, this is what I get:
- foreach var in pen dtx con dtr sub edu feesedu hlt feeshlt oth feesoth{
- local aux= wordcount("``var''")
= local aux= wordcount("pen_*_pc")
- di `aux'
= di 1
1
- local aux2= wordcount("``var'_t'")
= local aux2= wordcount("pen_*_t*")
- di `aux2'
= di 1
1
- }
So, the problem is that the way I refer to the local with the variables' names, it displays literally "pen_*_pc" instead of the list of variables with that structure (say, "pen_x1_pc pen_x2_pc"). Hence, the wordcount function only counts one variable.
Do you know how can I get Stata to properly count the number of variables in the local??
Many thanks in advance!
In order to be able to run a do-file on different datasets, I'm creating locals with variables' names that are as generic as possible in the following way:
Code:
local pen "pen_*_pc" local dtx "dtx_*_pc" local con "con_*_pc" local dtr "dtr_*_pc" local sub "sub_*_pc" local itx "itx_*_pc" local edu "edu_*_pc" local feesedu "" local hlt "hlt_*_pc" local feeshlt "" local oth "" local feesoth "" local pen_t "pen_*_t*" local dtx_t "dtx_*_t*" local con_t "con_*_t*" local dtr_t "dtr_*_t*" local sub_t "sub_*_t*" local edu_t "edu_*_t*" local feesedu_t "" local hlt_t "hlt_*_t*" local feeshlt_t "" local oth_t "" local feesoth_t ""
Later on in the do-file, I need to count the number of variables in each local (as an input for something else) and I've started coding the following loop:
Code:
foreach var in pen dtx con dtr sub edu feesedu hlt feeshlt oth feesoth { local aux= wordcount("``var''") di `aux' local aux2= wordcount("``var'_t'") di `aux2' }
- foreach var in pen dtx con dtr sub edu feesedu hlt feeshlt oth feesoth{
- local aux= wordcount("``var''")
= local aux= wordcount("pen_*_pc")
- di `aux'
= di 1
1
- local aux2= wordcount("``var'_t'")
= local aux2= wordcount("pen_*_t*")
- di `aux2'
= di 1
1
- }
Do you know how can I get Stata to properly count the number of variables in the local??
Many thanks in advance!
Comment