BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
NevermoreRedres
Obsidian | Level 7

Hello everyone, hope you're all good!

I'm having some trouble figuring how to make SAS make some things to me. Lemme show you my code so i can talk about what i mean:

DATA analise2014;
infile cards dlm='09'x dsd truncover;
input nnecropsia  (a b c d e ) (:time.);
format a--e time8.;
CARDS;
 1	16:10:00	17:24:00	19:02:00	08:15:00	09:59:00
 2	15:39:00	18:19:00	20:01:00	08:15:00	11:02:00
 3	12:10:00	19:41:00	20:38:00	10:00:00	12:43:00
 4	05:10:00	08:41:00	09:56:00	10:30:00	12:45:00
RUN;

data need;
  set analise2014;
  a_to_b=ifn(b>a,b-a,'24:00:00't-a+b);  
  b_to_c=ifn(c>b,c-b,'24:00:00't-b+c);  
  c_to_d=ifn(d>c,d-c,'24:00:00't-c+d);  
  d_to_e=ifn(e>d,e-d,'24:00:00't-d+e);
  format a_to_b -- d_to_e time8. ;
run;
proc tabulate data=need noseps;
  var a_to_b -- d_to_e;
  tables  a_to_b--d_to_e , mean*f=time10.0;
run;

So the first part of the code are a small part of my time samples and the code that makes SAS realize i'm dealing with time in the hh:mm:ss format.

The second part of the code (data need) is the part where i ask SAS to show me how much time passed from one time to another (Example: a_to_b in the first line is 1:14:00, because 17:24:00 - 16:10:00 = 1:14:00, and the "ifn(b>a....) part is for times that pass trough midnight, as you can se in the third for the fourth collum, because if i dont use this code the time will come as a negative number.

The third part of the code (proc tabulate) is so i can have an table with the arithmetic average of these diferences in time.

 

Now i need another variable, that i'll call "f", what does "f" will be?

I'm making this research for a necropsy lab, and they only receive corpses until 19h, and start doing the necropsies at 8h, and they want me to get the "effective residence time" with means discount 13 hours (19h to 8h are 13h) if any corpses arrived there after 19h PM. If you run the second part of the code, in the first line of c_to_d you'll see that it gives me 13:13:00, because thats the time that passed from 19:02:00 to 08:15:00, so "f" would subtract 13h from c_to_d and make it 00:13:00 (don't worry, it isn't manipulation of data as i will show both c_to_d and f in the results and the resume, it will serve as an argument of why i think the university [the place is subpart of one public university] should allow then to hire more staff and work 24/7).

 

That part should not be that dificult, however if you look at the second line, there is this c_to_d that gives the result as 12:14:00 (08:15:00 - 20:01:00) and if i take this result and subtract 13h it will come as negative, so i need this subtraction to start at 19h and not in whatever time "c" is presenting me IF "c"19h, hope i could put it in a way that makes this easy to understand.

 

Also, how do i make a graph of the third part of the code (the averages), one if i include "f" and one if i don't.

Really hope you guys can help me, i thank you in advance.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

I think you should handle it like this:

b_to_c=ifn(c>b,c-b,'24:00:00't-min('19:00:00't,b)+c-'13:00:00't); 

Anytime a corpse arrives after 19:00, it is assumed to have arrived at 8:00 in the following morning.

View solution in original post

11 REPLIES 11
Kurt_Bremser
Super User

I think you should handle it like this:

b_to_c=ifn(c>b,c-b,'24:00:00't-min('19:00:00't,b)+c-'13:00:00't); 

Anytime a corpse arrives after 19:00, it is assumed to have arrived at 8:00 in the following morning.

NevermoreRedres
Obsidian | Level 7
It worked wonders, KurtBremser, thank you so much!
However i just had to change the b_to_c to c_to_d and its correspondent letters in the code, as this was the one i needed, but no problem them.
Also, do you know how i can put this in a new collum called "f"? Because i want both times (c_to_d without subtracting the 13h and "f" subtracting it) appearing on the table.
NevermoreRedres
Obsidian | Level 7

Hi againg KurtBremser and everyone, i've found out how to add another collum, if anyone want to see it here is the code: 

DATA analise2014;
infile cards dlm='09'x dsd truncover;
input nnecropsia  (a b c d e) (:time.);
format a--e time8.;
CARDS;
 1	16:10:00	17:24:00	19:02:00	08:15:00	09:59:00
 2	15:39:00	18:19:00	20:01:00	08:15:00	11:02:00
 3	12:10:00	19:41:00	20:38:00	10:00:00	12:43:00
 4	05:10:00	08:41:00	09:56:00	10:30:00	12:45:00
RUN;

data need;
  set analise2014;
  a_to_b=ifn(b>a,b-a,'24:00:00't-a+b);  
  b_to_c=ifn(c>b,c-b,'24:00:00't-b+c);  
  c_to_d=ifn(d>c,d-c,'24:00:00't-c+d);  
  d_to_e=ifn(e>d,e-d,'24:00:00't-d+e);
  f_to_f=ifn(d>c,d-c,'24:00:00't-min('19:00:00't,c)+d-'13:00:00't);
  format a_to_b -- f_to_f time8. ;
run;
proc tabulate data=need noseps;
  var a_to_b -- f_to_f;
  tables  a_to_b--f_to_f , mean*f=time10.0;
run;

Thank you again for all the help! S2
Reeza
Super User
Go back and ask for the dates that align with the times so you have a full datetime variable for your calculation. Most medical records require this so it should be stored somewhere.
NevermoreRedres
Obsidian | Level 7
Hello Reeza!
I had the dates, but my advisor said i just needed the hours. Also the dates are in sequence, there is no interval with more than 24h. Thank you for the concern!
ballardw
Super User

@NevermoreRedres wrote:
Hello Reeza!
I had the dates, but my advisor said i just needed the hours. Also the dates are in sequence, there is no interval with more than 24h. Thank you for the concern!

Sounds like assumptions to be tested.

And it wouldn't be the first time an advisor made work harder than needed.

Your C and D variables sure look like a change across the date barrier for the first 3 records given the times shown.

If C and D were datetime values instead of just time values there wouldn't have to be any of the IFN type processing to get around the date boundary.

 

 

NevermoreRedres
Obsidian | Level 7
Hello ballardw. Yeah, no kidding, but i had to get all the times from 2014 to 2019, for these five variables BY HAND, tipping it all to excel, because they didn't had it already... Guess the graduate always get the short end of the stick uh?
ballardw
Super User

@NevermoreRedres wrote:
Hello ballardw. Yeah, no kidding, but i had to get all the times from 2014 to 2019, for these five variables BY HAND, tipping it all to excel, because they didn't had it already... Guess the graduate always get the short end of the stick uh?

Yup.

 

I've been a "data compilation assistant" at one time. Though I was dealing with instruments that provided proper date an time hacks. Unless someone reset the system clocks. Which happened every time the system rebooted after a power outage. To Nov 1980 for data that was collected in the 1990's.

Guess what? I wrote a SAS program that would jigger the date/time to the proper date and time. (Though we had to scrap some of the cumulative interval instrument data as the intervals were no longer starting and ending at the specified interval).

Applied this to a couple hundred instrumental reporting layouts.

 

 

Reeza
Super User
Well, your advisor is wrong, dates would make this a simpler problem.
NevermoreRedres
Obsidian | Level 7
Thank you for the advice Reeza! I'm going to remember this next time!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 11 replies
  • 1444 views
  • 5 likes
  • 4 in conversation