Hi, I have time1: 2022-02-02T13:28
time2: 2022-02-02T12:28
They are both char vaules. Because I need to do the calculation if time1 > = time2 then
time3= time1 - time2 + 1, so how can I convert them to num value and then do the calculation.
Thank you.
First, are you sure these are char? You need to find if the output from PROC CONTENTS says these are char. Sometimes you can't simply tell by looking at the values. Can you tell us what PROC CONTENTS says?
Run a DATA step like this:
data want;
set have (rename=(time1=_time1 time2=_time2));
format time1 time2 e8601dt19.;
time1 = input(_time1,e8601dt16.);
time2 = input(_time2,e8601dt16.);
drop _time1 _time2;
run;
Untested, posted from my tablet.
You should show use exactly what you expect the result of time3= time1 - time2 + 1 would look like expressed as a time on the clock.
The values you show will most easily turn into a SAS datetime value which will have units of seconds.
So the Time3 value using the values shown would end up with a numeric value of 3601 which would represent 1:00:01 or one hour and 1 second.
If you want the number of HOURS you would would use something like
hours = intck('hour',time2,time1);
The INTCK function counts intervals. So could request seconds, hours, and since your values are DATETIME, could ask for day ('DTDAY' as the interval), month ('DTMONTH' as the interval) or a number of other intervals.
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.
If you want the difference in DAYS then convert the strings into DATE values. Those are stored in DAYS.
time3=input(time1,yymmdd10.)-input(time2,yymmdd10.)+1;
Thank you all for your help!!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.