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 ;
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.