BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Hello_there
Lapis Lazuli | Level 10

Hi, I'm trying to find the difference between time2 from time1 in the following data set.

Time1, Time2 are character variables. I'm having a difficult time trying to subtract the different times.

The difference should be in the unit of hours.

ex: So if the difference is 30 minutes more, between time2 and time1, the value should be 0.50 (2 decimal places extended maximum with a leading 0). Or if the difference is 45 minutes less between time2 and time1, the value should be -0.75.

 

Thanks

data have; 
infile datalines dsd dlm=",";
	input day $ time1 $ time2 $;
datalines;
day1, 08:25, 08:30
day2, 09:15, 09:45
day3, 10:05, 09:45
day4, 14:05, 14:10
day5, 15:10, 14:30
;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Time values (and also date values and date-time values) should be numeric, not character! Then, differences are found by subtraction.

 

data have; 
infile datalines dsd dlm=",";
	input day $ time1 time2;
    informat time1 time2 time5.;
    format time1 time2 time5.;
datalines;
day1, 08:25, 08:30
day2, 09:15, 09:45
day3, 10:05, 09:45
day4, 14:05, 14:10
day5, 15:10, 14:30
;
run;

data want;
    set have;
    difference=time2-time1;
    hours=difference/3600;
    format difference time5.;
run;

 

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Time values (and also date values and date-time values) should be numeric, not character! Then, differences are found by subtraction.

 

data have; 
infile datalines dsd dlm=",";
	input day $ time1 time2;
    informat time1 time2 time5.;
    format time1 time2 time5.;
datalines;
day1, 08:25, 08:30
day2, 09:15, 09:45
day3, 10:05, 09:45
day4, 14:05, 14:10
day5, 15:10, 14:30
;
run;

data want;
    set have;
    difference=time2-time1;
    hours=difference/3600;
    format difference time5.;
run;

 

--
Paige Miller
Hello_there
Lapis Lazuli | Level 10

Thanks, for the reply and answer. The data was given to me in character. I didn't know if there was a function i didn't know about that converts into numeric easily besides using input().

PaigeMiller
Diamond | Level 26

The input function in SAS converts character strings into numeric. If time1 is character, then use:

 

time1_numeric=input(time1,time5.);
--
Paige Miller
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
  • 3 replies
  • 1462 views
  • 3 likes
  • 2 in conversation