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 ; 

 

 

1 REPLY 1
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;

sas-innovate-2024.png

 

Time is running out to save with the early bird rate. Register by Friday, March 1 for just $695 - $100 off the standard rate.

 

Check out the agenda and get ready for a jam-packed event featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events. 

 

Register now!

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
  • 1 reply
  • 4281 views
  • 1 like
  • 2 in conversation