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

Hi guys, I thought below would manage calculating the difference between time periods across each rows. But I wind up with not only huge but negative numbers. Please help correcting this syntax out. What I want is about 132 days difference between two rows (07022002 and 11142002)  in the second row instead 4120000 and so forth so on for the rest of the rows.  

Thanks a lot in advance.

 

wrong output.png

data diff_date;
input Date_Visit $ ID $;
cards;
07022002 83
11142002 83
05152003 83
11042003 83
05042004 83
11102004 83
05032005 83
11012005 83
06062006 83
;

data b; set diff_date;  
/*format date_visit 9.;*/
by child_id;
retain r_date;
r_date=lag(date_visit);
if not first.child_id then do;
diff_visit=date_visit-r_date;
end;
drop r_:;
run; 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

It would take a few changes here and there, starting with how you read in the data.

 

data diff_date;
input Date_Visit mmddyy8.  ID $;
cards;
07022002 83
11142002 83
05152003 83
11042003 83
05042004 83
11102004 83
05032005 83
11012005 83
06062006 83
;

data want; 
set diff_date;  
by id;
diff_visit = dif(date_visit);
if first.id then diff_visit = .;
run; 

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

11142002 is not a date, but a 8-digit number (by now - more than 300 posts here - you should know that). Store dates as SAS date values, and use intck() to get distances in time.

Astounding
PROC Star

It would take a few changes here and there, starting with how you read in the data.

 

data diff_date;
input Date_Visit mmddyy8.  ID $;
cards;
07022002 83
11142002 83
05152003 83
11042003 83
05042004 83
11102004 83
05032005 83
11012005 83
06062006 83
;

data want; 
set diff_date;  
by id;
diff_visit = dif(date_visit);
if first.id then diff_visit = .;
run; 

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