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

Hello,

In my data set, which is an Excel input, I have a list of dates from patient visits. See example below:

SubjectNumDateVisit

1

01Jan2015
101Feb2015
111Feb2015
201Dec2014
212Dec2014
201Jan2015
207Jan2015


I'm trying to find the number of days between each visit for each subject. When I use the following code, SAS substracts the DateVisit observations regardless of Subject Number:

data xyz;

set abc;

DaysBtwn=dif(DateVisit);

run;

I tried adding a by (by=SubjectNum) statement, and that didn't work. Not sure if a by statement would do anything here anyway. What's a good (and easy) way to get this done? Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
naveen_srini
Quartz | Level 8

I suggest Use @Astounding expert help as I copy many of his codes too, but if you are novice like me, just play:

data have;

input subjectnum $ datevisit :date9.;

format datevisit date9.;

datalines;

1 01Jan2015

1 01Feb2015

1 11Feb2015

2 01Dec2014

2 12Dec2014

2 01Jan2015

2 07Jan2015

;

data want;

set have;

by subjectnum notsorted;

k=lag(datevisit);

if not first.subjectnum then DaysBtwn=intck('day',k,datevisit);

drop k;

run;

View solution in original post

4 REPLIES 4
slchen
Lapis Lazuli | Level 10

data xyz;

set abc;

by SubjectNum

retain LastVisit;

LastVisit=lag(DateVisit)

if first.SubjectNum then Diff=.;

else DaysBtwn=DateVisit-LastVisit;

drop LastVisit;

run;

Or:

data xyz;

  set abc;

  if _n_^=1 then set abc (keep=SubjectNum DateVisit rename=(SubjectNum=_SubjectNum DateVisit=_DateVisit));

  Diff=.;

  if SubjectNum=_SubjectNum and not first.SubjectNum then Diff=_DateVisit-DateVisit;

  drop _SubjectNum _DateVisit;

  run;

Astounding
PROC Star

While earlier suggestions might work, this would be a little more similar to your original attempts:

data xyz;

set abc;

by SubjectNum;

DaysBtwn = diff(DateVisit);

if first.SubjectNum then DaysBtwn=.;

run;

Note that DIFF (as well as LAG) must execute on every observation to get the right answer.  Well, at least 99% of the time anyway.  So never do this:

data xyz;

set abc;

by SubjectNum;

if first.SubjectNum=0 then DaysBtwn = diff(DateVisit);

run;

naveen_srini
Quartz | Level 8

I suggest Use @Astounding expert help as I copy many of his codes too, but if you are novice like me, just play:

data have;

input subjectnum $ datevisit :date9.;

format datevisit date9.;

datalines;

1 01Jan2015

1 01Feb2015

1 11Feb2015

2 01Dec2014

2 12Dec2014

2 01Jan2015

2 07Jan2015

;

data want;

set have;

by subjectnum notsorted;

k=lag(datevisit);

if not first.subjectnum then DaysBtwn=intck('day',k,datevisit);

drop k;

run;

dereck255
Fluorite | Level 6

Thank you for your help! It worked out perfectly!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 2479 views
  • 6 likes
  • 4 in conversation