Hello all,
I am trying to create a visit date for for each date that a subject has. The first date would be "visit 1", second "visit 2", etc. In the past I have sorted by id and date, then picked first.id or last.id, but in this case I have more than two dates per an id.
Example start data:
ID | Date |
1 | 10-Mar-15 |
1 | 12-Mar-15 |
1 | 16-Apr-15 |
2 | 1-Apr-15 |
2 | 8-Apr-15 |
3 | 2-Feb-15 |
3 | 8-Feb-16 |
3 | 11-Feb-16 |
And I would like it to end up as:
ID | Date | Visit |
1 | 10-Mar-15 | 1 |
1 | 12-Mar-15 | 2 |
1 | 16-Apr-15 | 3 |
2 | 1-Apr-15 | 1 |
2 | 8-Apr-15 | 2 |
3 | 2-Feb-15 | 1 |
3 | 8-Feb-16 | 2 |
3 | 11-Feb-16 | 3 |
I have looked on the forum and stack overflow with no luck so far. I assumed this would be a common question so parhaps I am just looking in the wrong places. Regardless, thank you in advance for the help.
If the data is already sorted by id/date, then:
data want;
set have;
by id;
visit+1;
if first.id then visit=1;
run;
If the data is already sorted by id/date, then:
data want;
set have;
by id;
visit+1;
if first.id then visit=1;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.