- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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().
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The input function in SAS converts character strings into numeric. If time1 is character, then use:
time1_numeric=input(time1,time5.);
Paige Miller