Someone passed me date variables like that follows. Both Finished_date (Char 15 $15. $15.) and Start_date (Char 10 ) are characters.
I'd like to compute difference in days between Finished_date and Start_date; that is, Diff_days = Finished_date - Start_date, but it does not work. How can I do this simple subtraction? convert character to date format first?
Thank you very much for your help! Lee
Finished_date Start_date
2/15/2020 10/15/2018
2/15/2020 3/1/2018
2/15/2020 7/8/2018
2/16/2020 9:28 8/20/2019
2/16/2020 12:44 1/1/2017
2/17/2020 0:10 9/25/2016
2/17/2020 7:54 10/15/2019
I would play it safe and go with :
diff_days = input(scan(finished_date, 1, " "), mmddyy10.) - input(scan(start_date, 1, " "), mmddyy10.);
Given that both are character variables, you have to convert from character to numeric on the fly, as part of the computation:
diff_days = input(finished_date, mmddyy10.) - input(start_date, mmddyy10.);
This assumes that the time values are part of FINISHED_DATE, not part of START_DATE. Even then, there could be complications if you have 8-character dates with a time piece like 2/6/2017 9:28. If that occurs, a slightly more complex formula would be needed.
I would play it safe and go with :
diff_days = input(scan(finished_date, 1, " "), mmddyy10.) - input(scan(start_date, 1, " "), mmddyy10.);
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.