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;
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;
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;
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().
The input function in SAS converts character strings into numeric. If time1 is character, then use:
time1_numeric=input(time1,time5.);
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.