You can use the inline macro expansion operator `= as follows:
local z "1986 1988 1990"
foreach x of local z {
gen XPRI3yr`x' = ( XPRI`x' - XPRI`=`x' - 2' )
}
note that `x'-2 is enclosed with `= and '
Tom
----- Original Message -----
From: "Dev Vencappa" <lexdvv@nottingham.ac.uk>
To: <statalist@hsphsun2.harvard.edu>
Sent: Monday, April 05, 2004 9:13 AM
Subject: Re: st: manipulating part of a variable name.
> Stata users, I am stuck with a simple problem here.
> I have variables called XPRI1984 XPRI1986 XPRI1988 XPRI1990, and so on. I
want to ask stata to create a variable that computes a three-year difference
(I count the first year as well) ie:
>
> gen XPRI3yr1986=XPRI1986-XPRI1984
> gen XPRI3yr1988=XPRI1988-XPRI1986
>
> and so on.
>
> I want to put that in a loop and tried the following:
>
> local z "1986 1988 1990"
> foreach x of local z{
> gen XPRI3yr`x'=(XPRI`x'-XPRI`x'-2)
> }
>
> This is the bit I am stuck with. Is there a way of asking Stata to read
the values of the variables that represent the previous two years values,
i.e. XPRI`x'-2?
>
> I know what I have done above is incorect and think the solution is to
ask stata to read the last four digits of the variable name, and use it as a
numeric variable and hence the local will turn numeric,so that one can
manipulate it in the way I did in my loop. I am not sure how to do that
though. Please help
>
> Thanks in advance.
>
> Dev
>
>
>
> *
> * 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/
>
*
* 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/