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:13:01 +0100

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.


*   For searches and help try:

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