Hi again,
This is more or less related to the string vector I was looking for before.
I am struggling to get a for loop to work as I want. The problem is a little
bit more tricky now, as I will need to have the variable before the current
variable in the for loop. In programming, I'm used to build the variable
name then myself, like:
#delimit;
forvalues x=1/2 {;
if `x'==1 local gesl="_m";
if `x'==2 local gesl="_v";
forvalues y=1/20 {;
local groep="lft"+string((`y'-1)*5)+"_"+string(`y'*5)+`gesl';
local sterfte="stf"+`gesl';
replace `groep'=`groep'*(1-`sterfte'[`y']);
};
};
(this doesn't show the need for the previous variable just yet, but I will
need it, so when loking at lft15_20_m, I need to have also lft10_25_m.) The
code above complains that _m is not found. I didn't expect that one...
The I tried something else, in the spirit of the varlists as has been
suggested before:
#delimit;
scalar i=0;
foreach x of varlist lft0_5_m lft5_10_m lft10_15_m lft15_20_m lft20_25_m
lft25_30_m lft30_35_m
lft35_40_m lft40_45_m lft45_50_m lft50_55_m lft55_60_m lft60_65_m
lft65_70_m
lft70_75_m lft75_80_m lft80_85_m lft85_90_m lft90_95_m lft95__m {;
scalar i=`i'+1;
replace `x'=`x'*stf_m[`i'];
};
This complains that stf_m is not found. Didn't expect that either as it is
in the dataset...
Actually, my idea was to define a varlist:
#delimit;
local vars_m lft0_5_m lft5_10_m lft10_15_m lft15_20_m lft20_25_m lft25_30_m
lft30_35_m
lft35_40_m lft40_45_m lft45_50_m lft50_55_m lft55_60_m lft60_65_m
lft65_70_m
lft70_75_m lft75_80_m lft80_85_m lft85_90_m lft90_95_m lft95__m;
and then do a forloop and access variables with:
local name_1: word 'i-1' of 'vars_m';
local name_2: word 'i' of 'vars_m';
But then:
#delimit;
scalar i=0;
foreach x of local vars_m {;
scalar i=`i'+1;
replace `x'=`x'*stf_m[`i'];
};
Didn't do anything, not even complain... The complaint about stf_m came when
I replaced the foreach line with the explicit list as above.
Does anyone have an idea what is happening here? And how best to access 2
different varaibles from the same list? By building the string or by the
second approach?
Thanks already, best regards,
-Hendri.
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/