BookmarkSubscribeRSS Feed
Lea1702
Fluorite | Level 6

Hi,

I’m a SAS new learner and I have a problem that I haven’t able to solve.

I’m trying to process two variables ‘bedtime’ and ‘time to get up’, but my row data has only separated hours and minutes, so that I would like to concatenate them into HH:MM 24hours format (00:00). I was trying to do it through both compress function and cat function but not matter which procedure I have applied I just could obtain results as this: 1130, 120, 20, 210, etc.

TGU= CATS (OF C3_2 C3_3); /* C3_2: hours; C3_3: minutes */

TGU= compress(C3_2)||compress(c3_3);

So, it does not allow me to make a subtraction between the previous variables that I mentioned (‘bedtime’ and ‘time to get up’), so I would really appreciate your help.

 

         WHAT I HAVE                                    WHAT I WANT TO HAVE              

 BEDTIME       TIME TO GET UP        BEDTIME   TIME TO GET UP    TIME DIF

HOUR  MIN            HOUR   MIN

   23       0                  5         0                 23:00              05:00                    6

    0       30                 4         0                 00:00              04:00                    4

    1       45                 7        30                01:45              07:30                  5:45

   22        0                 7        15                22:00              07:15                  9:15

/**Laying Down Time**/

/*PROC_ASSIGNING VALUES_Bedtime combining Hours and Minutes*/
BT = CATS (OF C3_0 C3_1); 

/*PROC_COMPRESSING_Bedtime combining Hours and Minutes*/
BT=compress(c3_0)||compress(c3_1);

/**Time to get up **/  

/*PROC_ASSIGNING VALUES_time to get up combining Hours and Minutes*/
TGU= CATS (OF C3_2 C3_3) ;
 
/*PROC__COMPRESSING_time to get up combining Hours and Minutes*/
TGU=compress(C3_2)||compress(c3_3);

/*PROC_Bedtime-Time to get up*/
BTTGUDT= TGU-BT ; 

 

 

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

First, avoid coding all in uppercase, its really hard to read.  You can put HH and MM into a time variable by:

data want;
  hours=23;
  mins=45;
  time=input(catx(':',put(hours,z2.),put(mins,z2.)),time5.);
  format time time5.;
run;

However there are better ways of doing it, using the HMS() function:

data want;
  hours=23;
  mins=45;
  time=hms(23,45,0);
  format time time5.;
run;
DrAbhijeetSafai
Lapis Lazuli | Level 10

@RW9 , many thanks!

 

Thank you.

 

- Dr. Abhijeet Safai

Dr. Abhijeet Safai
Certified Base and Clinical SAS Programmer
Associate Data Analyst
Actu-Real
ballardw
Super User

@DrAbhijeetSafai wrote:

@RW9 , many thanks!

 

Thank you.

 

- Dr. Abhijeet Safai


If you have hours and minutes (optionally seconds) as numeric variables then use of the HMS function will be simpler and perhaps easier to follow than build a string and use an informat:

 

data have;
 input bedhour bedmin uphour upmin;
datalines;
23  0  5 0 
 0 30  4 0 
 1 45  7 30 
22  0  7 15 
;
data want;
   set have;
   bedtime = hms(bedhour,bedmin,0);
   uptime  = hms(uphour,upmin,0);
   format bedtime uptime time5.;
   timedif= 
run;

The HMS function takes hours, minutes and seconds and returns a time value. If you don't have have one of the units you can place a zero in that position so the function has all the parameters needed.

Sarath_A_SAS
Obsidian | Level 7

To address your problem and calculate the time difference correctly, you should convert the hours and minutes into the HH:MM 24-hour format, and then convert these time strings into actual SAS time values. Once you have the time values, you can easily calculate the difference between 'bedtime' and 'time to get up'.

Here’s a step-by-step solution:

  1. Concatenate Hours and Minutes: You need to format your hours and minutes properly to ensure that they have two digits, particularly for values like "5" minutes, which should be "05".
  2. Convert the Time into SAS Time Format: Once you have the time in HH:MM format, you can use the INPUT function to convert it into a SAS time value.
  3. Calculate Time Difference: After converting the times, you can subtract them to find the difference, and format the result in hours and minutes.

Here’s a code example that does this:

/* Combine hours and minutes into HH:MM format for Bedtime */
BT = cats(put(C3_0, z2.), ":", put(C3_1, z2.));

/* Combine hours and minutes into HH:MM format for Time to get up */
TGU = cats(put(C3_2, z2.), ":", put(C3_3, z2.));

/* Convert the time strings into SAS time values */
BT_time = input(BT, time5.);
TGU_time = input(TGU, time5.);

/* Calculate the time difference, adjust for crossing midnight */
if TGU_time >= BT_time then Time_Diff = TGU_time - BT_time;
else Time_Diff = (TGU_time + '24:00't) - BT_time;

/* Format the result as hours and minutes */
format BT_time TGU_time Time_Diff time5.;

 Explanation:

  1. Concatenation: cats(put(C3_0, z2.), ":", put(C3_1, z2.)) ensures that hours and minutes are always two digits.
  2. SAS Time Value: input(BT, time5.) converts the concatenated string (HH:MM) into a SAS time value.
  3. Time Difference Calculation: If the time to get up is earlier than bedtime (crossing midnight), the code handles that by adding 24 hours ('24:00't).
  4. Formatting: The result is formatted in the HH:MM time format using the time5. format.

This will give you the correct output with the time difference calculated in HH:MM.

 

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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