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.
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;
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;
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.
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.