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

st: RE: How to numerate different events from the same person?


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: How to numerate different events from the same person?
Date   Thu, 13 Oct 2005 16:52:38 +0100

Others have quite rightly given you 
general advice against looping and 
a specific one-line solution. 

One specific problem with your code
is that 

 		replace ing=`j'

will -replace- all observations. That 
is most definitely not what you want. 

Nick 
[email protected] 

Alejandro Allepuz
 
> I would like to assing to each discharge event of the same 
> patient a number. The first discherge will be number 1, the 
> second number 2 and so on.
> 
> This is the syntax I'm using. What's wrong with it? I don't 
> know why local values remained unchange.
> 
> input patient diag
> 1 8151
> 1 8006
> 1 8153
> 2 8154
> 2 8155
> 3 8005
> 3 8155
> end
> 
> gen lag_pat=patient[_n-1]
> replace lag_pat=patient if lag_pat==.
> 
> gen ing=1
> local j=1
> local N=_N
> forvalues i=1(1)`N'{
> 	if (_n==`i' & patient==lag_pat) {
> 		replace ing=`j'
> 		local j=`j'+1
> 	}
> 	else local j=1
> }
> 
> The result of this syntax was supposed to be:
> 
>      +--------------------------------+
>      | patient   diag   lag_pat   ing |
>      |--------------------------------|
>   1. |       1   8151         1     1 |
>   2. |       1   8006         1     2 |
>   3. |       1   8153         1     3 |
>   4. |       2   8154         1     1 |
>   5. |       2   8155         2     2 |
>      |--------------------------------|
>   6. |       3   8005         2     1 |
>   7. |       3   8155         3     2 |
>      +--------------------------------+
> 
> But I have obtained: 
> 
>      +--------------------------------+
>      | patient   diag   lag_pat   ing |
>      |--------------------------------|
>   1. |       1   8151         1     1 |
>   2. |       1   8006         1     1 |
>   3. |       1   8153         1     1 |
>   4. |       2   8154         1     1 |
>   5. |       2   8155         2     1 |
>      |--------------------------------|
>   6. |       3   8005         2     1 |
>   7. |       3   8155         3     1 |
>      +--------------------------------+
> 
> Could anyboby help me?

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