I want to write in a column the index of the row based condition.
I can write the last row index of a group.
col1 | index |
---|---|
A | . |
A | . |
A | 3 |
B | . |
B | 5 |
C | . |
C | . |
C | . |
C | 9 |
I want to write in a new column row indexes based on index
column.
col1 | index | New column |
---|---|---|
A | . | . |
A | . | 2 |
A | 3 | . |
B | . | 4 |
B | 5 | . |
C | . | . |
C | . | . |
C | . | 8 |
C | 9 | . |
data HAVE;
infile datalines;
input col1 $1.;
datalines;
A
A
A
B
B
C
C
C
C
;
data have2;
set have;
by col1;
if last.col1 then index=_n_;
run;
Since your data are grouped by the ID, you could approach this as a look-ahead problem, to detect when the next ID changes (or next next). Something like:
data HAVE;
infile datalines;
input col1 $1.;
datalines;
A
A
A
B
B
C
C
C
C
;
options mergenoby=nowarn ;
data want ;
merge
have
have(keep=col1 rename=(col1=_col1_lead1) firstobs=2)
have(keep=col1 rename=(col1=_col1_lead2) firstobs=3)
;
if col1 ne _col1_lead1 then index =_n_ ;
else if col1 ne _col1_lead2 then index2=_n_ ;
drop _: ;
run ;
options mergenoby=error ;
proc print data=want ;
run ;
Returns:
Obs col1 index index2
1 A . .
2 A . 2
3 A 3 .
4 B . 4
5 B 5 .
6 C . .
7 C . .
8 C . 8
9 C 9 .
If you have an ID with only one record, this might not do what you want. But I'm not sure what you would want in that case.