DATA Step, Macro, Functions and more

How to Number Visit Dates per ID.

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 76
Accepted Solution

How to Number Visit Dates per ID.

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.


Accepted Solutions
Solution
‎03-14-2017 01:24 PM
Trusted Advisor
Posts: 1,022

Re: How to Number Visit Dates per ID.

Posted in reply to daszlosek

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;

View solution in original post


All Replies
Solution
‎03-14-2017 01:24 PM
Trusted Advisor
Posts: 1,022

Re: How to Number Visit Dates per ID.

Posted in reply to daszlosek

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;

Frequent Contributor
Posts: 76

Re: How to Number Visit Dates per ID.

Worked like a charm. Had some difficulty initially due to defining the visit variable in the previous datastep for some reason. Thanks a bunch!
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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