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

Hi there,

 

I have the following dataset structure

 

Facility  Appointment_date  Patient_id  

a  5/4/2013  123

a  5/5/2013  123

a  5/5/2013  232

a  5/15/2013  123

 

I want to find out the difference in successive appointment_dates for patients who had multiple visits. The structure of the output should look as follows

 

Facility  Appointment_date  Patient_id  Lag Diff

a  5/4/2013  123  .  .

a  5/5/2013  123  5/4/2013  1

a  5/5/2013  232 .  .

a  5/15/2013  232 5/5/2013  10

 

I know that I can get the result by using lag function in SQL with the following query

select *, lag(appointment_date,1) over (partition by patient_id order by appointment_date) as lag from table1;

 

Kindly let me know if there is a way I can do the same using SAS code?

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Then mark the question as 'solved'

View solution in original post

3 REPLIES 3
art297
Opal | Level 21

Sounds like you are looking for something like:

data have;
  input Facility $ Appointment_date mmddyy10. Patient_id;
  format Appointment_date mmddyy10.;
  cards;
a  5/4/2013  123
a  5/5/2013  123
a  5/5/2013  232
a  5/15/2013  232
;

data want;
  set have;
  format lag mmddyy10.;
  by facility Patient_id;
  lag=ifn(first.Patient_id,.,lag(Appointment_date));
  dif=ifn(first.Patient_id,.,dif(Appointment_date));
run;

Art, CEO, AnalystFinder.com

roh910
Calcite | Level 5

Thanks. It's working!!

art297
Opal | Level 21

Then mark the question as 'solved'

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!

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.

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
  • 3 replies
  • 4628 views
  • 0 likes
  • 2 in conversation