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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.