Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: manipulating part of a variable name.


From   "Dev Vencappa" <[email protected]>
To   <[email protected]>
Subject   Re: st: manipulating part of a variable name.
Date   Mon, 05 Apr 2004 14:48:05 +0100

Dev


>>> [email protected] 04/05/04 02:42pm >>>
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" <[email protected]>
To: <[email protected]>
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/


*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index