Hello. I have a patient dataset with observations by participant. I want to flag participants with multiple observations in the dataset. See below for an example.
| FID | Flag (want) |
| 1 | 1 |
| 1 | 0 |
| 2 | 0 |
| 3 | 0 |
| 4 | 1 |
| 4 | 0 |
| 4 | 0 |
| 5 | 0 |
data want;
set have;
by fid;
flag = (first.fid and not last.fid);
run;
Thanks @Kurt_Bremser. It seems that your code flagged the first observation for every participant. Is there a way to change the code so that it only flags those with multiple observations. If a participant has only one observation I dont want them flagged.
Simply cannot be with the data you posted and my code:
data have;
input fid;
datalines;
1
1
2
3
4
4
4
5
;
data want;
set have;
by fid;
flag = (first.fid and not last.fid);
run;
proc print data=want noobs;
run;
Result:
id flag 1 1 1 0 2 0 3 0 4 1 4 0 4 0 5 0
Matches exactly with your expected result from your initial post:
FID Flag (want) 1 1 1 0 2 0 3 0 4 1 4 0 4 0 5 0
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.