It appears that you don't really understand the difference between the -if-
command and the -if- qualifier in Stata and need to read up on it. You may
also want to try to learn more about subscripting. Perhaps most
importantly, you should try to remember that any time you loop across the
observations in a Stata dataset, you are probably making a mistake or making
something more difficult than it needs to be. Rather than try to fix your
code, I think you can get what you want from 1 line:
bysort patient: gen ing=_n
Michael Blasnik
[email protected]
----- Original Message -----
From: "Alejandro Allepuz" <[email protected]>
To: <[email protected]>
Sent: Thursday, October 13, 2005 10:44 AM
Subject: st: How to numerate different events from the same person?
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?
Thanks a lot
Alejandro Allepuz Palau
*
* 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/