BookmarkSubscribeRSS Feed
Anav0416
Calcite | Level 5

Hi, 

I have the following sample tables with time cycle starts from midnight (0:00:00) to 23:59:59. I need to perform the following tasks:

1. If the duration in hours (duration_hr) > hours to midnight (hr_to_midnight) but span over only to the next day (refer to row 6 in HAVE table) then do

  • set row 6 duration_hr=hr_to_midnight
  • add extra row between row 6 and 7 then (1) advance date by 1 day, (2) set time to 0:00:00, (3) carry forward the previous rate to the new rate variable, (4) set the remaining duration as the new value for the duration_hr variable (refer to row 8 in WANT table)

2. If the duration in hours (duration_hr) > hours to midnight (hr_to_midnight) but span over multiple days (refer to row 7-8 in HAVE table) then perform similar tasks describe in #1 and add additional rows until all durations in hours are accounted for (i.e., produce rows 10-15 in the WANT table for the entry on 5/2/17 in the HAVE table, and rows 17-20 in the WANT table for the entry on 5/8/17 in the HAVE table)

 

3. If it is the last record for a subject and the duration in hours (duration_hr) > hours to midnight (hr_to_midnight) then just set the duration_hr=hr_to_midnight.

 

Working Syntax Codes: 

data want;
set have;
by subjectID;

 

if last.subjectID and dur_hr>hrToMidnight then dur_hr=hrToMidnight;

 

retain rDur 0;
if dur_hr>hrToMidnight then do;
rDur=dur_hr-hrToMidnight;
dur_hr=hrToMidnight;
/* do; (perform a do while loop here?)
dur_hr=rDur;
date=preDt+1;
time=.;
output;
end;*/
end;
run;

 

Q: I am able to reset the values but not able to produce the extra rows correctly at the moment. I am working on the program but any help is greatly appreciated.

 

Thank you.

  • Have        
    ObssubjectIDdatetimedelivery_typerateduration_hrprevious_datehr_to_midnight
    115/1/20175:00:00scheduled0.50.81638888930-Apr-1719
    215/1/20175:48:59temp01.2483333331-May-1718.18361111
    315/1/20177:03:53temp00.7488888891-May-1716.93527778
    415/1/20177:48:49scheduled0.752.2533333331-May-1716.18638889
    515/1/201710:04:01scheduled0.80.5008333331-May-1713.93305556
    615/1/201710:34:04scheduled036.361111111-May-1713.43222222
    715/2/201723:00:34scheduled0133.06944441-May-170.990555556
    815/8/201722:08:12scheduled096.002222222-May-171.863333333
    915/12/201722:15:55scheduled00.5816666678-May-171.734722222
             
    Want        
    1subjectIDdatetimedelivery typerateduration_hrprevioud_datehr_to_midnight
    215/1/20175:00:00scheduled0.50.81638888930-Apr-1719
    315/1/20175:48:59temp01.2483333331-May-1718.18361111
    415/1/20177:03:53temp00.7488888891-May-1716.93527778
    515/1/20177:48:49scheduled0.752.2533333331-May-1716.18638889
    615/1/201710:04:01scheduled0.80.5008333331-May-1713.93305556
    715/1/201710:34:04scheduled013.432222221-May-1713.43222222
    815/2/20170:00:00scheduled022.928888891-May-1724
    915/2/201723:00:34scheduled00.9905555561-May-170.990555556
    1015/3/20170:00:00scheduled0242-May-1724
    1115/4/20170:00:00scheduled0243-May-1724
    1215/5/20170:00:00scheduled0244-May-1724
    1315/6/20170:00:00scheduled0245-May-1724
    1415/7/20170:00:00scheduled0246-May-1724
    1515/8/20170:00:00scheduled012.078888847-May-1724
    1615/8/201722:08:12scheduled01.8633333337-May-171.863333333
    1715/9/20170:00:00scheduled0248-May-1724
    1815/10/20170:00:00scheduled0249-May-1724
    1915/11/20170:00:00scheduled02410-May-1724
    2015/12/20170:00:00scheduled022.1388888711-May-1724
    2115/12/201722:15:55scheduled00.58166666711-May-171.734722222
3 REPLIES 3
andreas_lds
Jade | Level 19

Please post the data in usable form and use "insert sas code" to post the code preserving the formatting and making code readable.

Why to i ask for usable data? From the table you have posted it is hardly possible to know if "date" and "time" are properly imported.

The whole task sound like homework.

Amir
PROC Star

Hi @Anav0416,

 

Well done on figuring it out. Sharing your solution will help others who have a similar problem.

 

Thanks & kind regards,

Amir.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 499 views
  • 1 like
  • 3 in conversation