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

st: updating variables [was: <title unreadable>]


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: updating variables [was: <title unreadable>]
Date   Thu, 30 Oct 2003 14:49:54 -0000

cuiliweibj (please don't send HTML!) 

> Now here is my data,
> 
> . list t1 t2 p12 p21
> 
>      +---------------------------------------+
>      |     t1       t2        p12        p21 |
>      |---------------------------------------|
>   1. | 884926   115074   .3470322   .1175582 |
>   2. |      .        .   .3154392    .137751 |
>   3. |      .        .   .2806379   .1431388 |
>   4. |      .        .   .2409214   .1699163 |
>   5. |      .        .   .1775456   .2156969 |
>      |---------------------------------------|
>   6. |      .        .   .1161977   .2085497 |
>   7. |      .        .   .1420289   .2278323 |
>   8. |      .        .   .1545061   .1971488 |
>   9. |      .        .   .1151673   .2456649 |
>  10. |      .        .   .0701624   .1987177 |
>      |---------------------------------------|
>  11. |      .        .   .0555115   .2351439 |
>  12. |      .        .   .0643219    .215013 |
>  13. |      .        .   .0767569   .1852408 |
>      +---------------------------------------+
> 
> 
> In order to get what I want, I did this.
> 
> . replace t1=t1[_n-1]+t2[_n-1]*p12[_n-1] if _n>1
> (1 real change made)
> 
> . replace t2=t2[_n-1]+t1[_n-1]*p21[_n-1] if _n>1
> (2 real changes made)
> 
> . list t1 t2 p12 p21
> 
>      +-------------------------------------------+
>      |       t1         t2        p12        p21 |
>      |-------------------------------------------|
>   1. |   884926     115074   .3470322   .1175582 |
>   2. | 924860.4   219104.3   .3154392    .137751 |
>   3. |        .   346504.8   .2806379   .1431388 |
>   4. |        .          .   .2409214   .1699163 |
>   5. |        .          .   .1775456   .2156969 |
>      |-------------------------------------------|
>   6. |        .          .   .1161977   .2085497 |
>   7. |        .          .   .1420289   .2278323 |
>   8. |        .          .   .1545061   .1971488 |
>   9. |        .          .   .1151673   .2456649 |
>  10. |        .          .   .0701624   .1987177 |
>      |-------------------------------------------|
>  11. |        .          .   .0555115   .2351439 |
>  12. |        .          .   .0643219    .215013 |
>  13. |        .          .   .0767569   .1852408 |
>      +-------------------------------------------+
> 
> 
> But this is not what I want, I want all the t1 and t2 replaced.
> I tried this, but it doesn't work.
> 
> while _n>1 {
>   replace t1=t1[_n-1]+t2[_n-1]*p12[_n-1]
>   replace t2=t2[_n-1]+t1[_n-1]*p21[_n-1]
> }
> 

No, that won't work. One reason alone is that 
as far as Stata is concerned each statement
within the loop remains identical. Another 
is that although 

while _n > 1 { 
	...
} 

makes sense to Stata, you will never get 
this loop started because _n by itself 
will be interpreted as _n _for the first 
observation_ which is 1, so the loop is 

while 1 > 1 { 
	...
} 

and thus will never be entered. At least 
that's my guess. A related problem is discussed at 
http://www.stata.com/support/faqs/lang/ifqualifier.html

This should be closer to what you want: 

forval i = 2/13 {
	replace t1=t1[_n-1]+t2[_n-1]*p12[_n-1] in `i' 
	replace t2=t2[_n-1]+t1[_n-1]*p21[_n-1] in `i' 
}

Nick 
[email protected] 
*
*   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