Help using Base SAS procedures

Concatenate hours and minutes into hh:mm format

Reply
Occasional Contributor
Posts: 5

Concatenate hours and minutes into hh:mm format

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 ; 

 

 

Super User
Super User
Posts: 7,997

Re: Concatenate hours and minutes into hh:mm format

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;
Ask a Question
Discussion stats
  • 1 reply
  • 121 views
  • 0 likes
  • 2 in conversation