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;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.