BookmarkSubscribeRSS Feed
monday89
Fluorite | Level 6

Hi Currently working on the data set that i received help from forum members before but I have to add more constraints.

 

Let's say the current dataset is the following:

 

subjectdaystart_datestop_datevaluevalue1
111/2/2019 14:341/2/2019 19:322311
121/3/2019 19:331/3/2019 23:442123
131/4/2019 21:491/7/2019 14:104199
212/3/2019 21:332/3/2019 23:442365
222/4/2019 23:332/5/2019 1:441223

 

If start and stop date is not the same (meaning it carries over the next day), i would like to assign a midnight cut off (and it works), but I need the following date to carry over as needed with appropriate value and value1 being status

 

Something like this below:

 

subjectstart_date_timestop_date_timenew_stop_date_timevaluevalue1
11/2/2019 14:341/2/2019 19:321/2/2019 19:322311
11/3/2019 19:331/3/2019 23:441/3/2019 23:442123
11/4/2019 21:491/7/2019 14:101/4/2019 23:594199
11/5/2019 0:00 1/5/2019 23:594199
11/6/2019 0:00 1/6/2019 23:594199
11/7/2019 0:00 1/7/2019 14:104199
22/3/2019 21:332/3/2019 23:442/3/2019 23:442365
22/4/2019 23:332/5/2019 1:442/4/2019 23:5912

23

 

 

I broke date/time into date formats only first

then did the following

if (stop date> start date) then new_stop_date = dhms(startdate, 23,59,59)

else new_stop_date = stop_date

 

but I am stuck on how to carry over the same values from that same field AND adding new start date as needed

 

Any help would be appreciated

5 REPLIES 5
ballardw
Super User

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

PaigeMiller
Diamond | Level 26

@monday89 wrote:


but I am stuck on how to carry over the same values from that same field AND adding new start date as needed

 


Show us what this means.

 

And follow the link from @ballardw regarding providing data in the desired format.

--
Paige Miller
monday89
Fluorite | Level 6

It was something like for carrying over the values but not that filling the dates +1 

 

data a.want; set have;

by subject;

if first.subject then new_value = value;

else new_value = coalesce (value, new_value); retain new_value;

run;

 

Tom
Super User Tom
Super User

First let's convert your printout into an actual dataset.

data have ;
  input subject day start_date start_time stop_date stop_time value value1 ;
  informat start_date stop_date mmddyy. start_time stop_time time.;
  start_dt = dhms(start_date,0,0,start_time);
  stop_dt = dhms(stop_date,0,0,stop_time);
  format start_dt stop_dt datetime20.;
  drop start_date start_time stop_date stop_time ;
cards;
1 1 1/2/2019 14:34 1/2/2019 19:32 23 11
1 2 1/3/2019 19:33 1/3/2019 23:44 21 23
1 3 1/4/2019 21:49 1/7/2019 14:10 41 99
2 1 2/3/2019 21:33 2/3/2019 23:44 23 65
2 2 2/4/2019 23:33 2/5/2019 1:44 12 23
;

Now let's make a program to split the overnight stays.

data want ;
  set have ;
  format start_new stop_new datetime. ;
  intervals=intck('dtday',start_dt,stop_dt);
  do offset=0 to intervals;
    if offset=0 then start_new=start_dt;
    else start_new=intnx('dtday',start_dt,offset,'b');
    if offset=intervals then stop_new=stop_dt;
    else stop_new=intnx('dtday',start_new,0,'e');
    output;
  end;
run;

Results:

image.png

You might want to drop the original dates and rename the new dates. And perhaps drop the other two variables.

monday89
Fluorite | Level 6

Thank you! I tried it but it showing me: Invalid DO loop control information, either the INITIAL or TO expression is missing or the BY

       expression is missing, zero, or invalid" 

 

I added "by" subject ID after "do", it wont work 😞

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 5 replies
  • 888 views
  • 0 likes
  • 4 in conversation