BookmarkSubscribeRSS Feed
SerenaJJ
Obsidian | Level 7

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.

9 REPLIES 9
PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
SerenaJJ
Obsidian | Level 7
Thank you so much for your reply. Yes, these are char after checking.
Kurt_Bremser
Super User

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.

ballardw
Super User

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.

 

SerenaJJ
Obsidian | Level 7
Thank you so much for your reply. I checked the output, there are days for the variable time3.
Tom
Super User Tom
Super User

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;
SerenaJJ
Obsidian | Level 7

Thank you all for your help!!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 1038 views
  • 0 likes
  • 5 in conversation