BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
daszlosek
Quartz | Level 8

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.

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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;

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

2 REPLIES 2
mkeintz
PROC Star

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;

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
daszlosek
Quartz | Level 8
Worked like a charm. Had some difficulty initially due to defining the visit variable in the previous datastep for some reason. Thanks a bunch!
How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 2695 views
  • 3 likes
  • 2 in conversation