<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Concatenate hours and minutes into hh:mm format in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Concatenate-hours-and-minutes-into-hh-mm-format/m-p/937579#M83579</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;&amp;nbsp;, many thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- Dr. Abhijeet Safai&lt;/P&gt;</description>
    <pubDate>Tue, 30 Jul 2024 09:58:20 GMT</pubDate>
    <dc:creator>DrAbhijeetSafai</dc:creator>
    <dc:date>2024-07-30T09:58:20Z</dc:date>
    <item>
      <title>Concatenate hours and minutes into hh:mm format</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Concatenate-hours-and-minutes-into-hh-mm-format/m-p/373598#M65225</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hi, &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I’m a SAS new learner and I have a problem that I haven’t able to solve. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;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. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;TGU= CATS (OF C3_2 C3_3); /* C3_2: hours; C3_3: minutes */&lt;/P&gt;&lt;P&gt;TGU= compress(C3_2)||compress(c3_3);&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;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.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHAT I HAVE&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHAT I WANT TO HAVE&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;BEDTIME&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TIME TO GET UP&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BEDTIME&amp;nbsp;&amp;nbsp; TIME TO GET UP&amp;nbsp;&amp;nbsp;&amp;nbsp; TIME DIF&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;HOUR&amp;nbsp; MIN&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; HOUR &amp;nbsp; MIN&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; 23&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 5&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 23:00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 05:00&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 6&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; 30&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 00:00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 04:00&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 4&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 45&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 30&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 01:45&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 07:30&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5:45&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; 22&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 15 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 22:00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 07:15&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 9:15&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/**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 ; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jul 2017 11:40:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Concatenate-hours-and-minutes-into-hh-mm-format/m-p/373598#M65225</guid>
      <dc:creator>Lea1702</dc:creator>
      <dc:date>2017-07-06T11:40:54Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate hours and minutes into hh:mm format</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Concatenate-hours-and-minutes-into-hh-mm-format/m-p/373608#M65227</link>
      <description>&lt;P&gt;First, avoid coding all in uppercase, its really hard to read. &amp;nbsp;You can put HH and MM into a time variable by:&lt;/P&gt;
&lt;PRE&gt;data want;
  hours=23;
  mins=45;
  time=input(catx(':',put(hours,z2.),put(mins,z2.)),time5.);
  format time time5.;
run;
&lt;/PRE&gt;
&lt;P&gt;However there are better ways of doing it, using the HMS() function:&lt;/P&gt;
&lt;PRE&gt;data want;
  hours=23;
  mins=45;
  time=hms(23,45,0);
  format time time5.;
run;
&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Jul 2017 11:52:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Concatenate-hours-and-minutes-into-hh-mm-format/m-p/373608#M65227</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-07-06T11:52:05Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate hours and minutes into hh:mm format</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Concatenate-hours-and-minutes-into-hh-mm-format/m-p/937579#M83579</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;&amp;nbsp;, many thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- Dr. Abhijeet Safai&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2024 09:58:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Concatenate-hours-and-minutes-into-hh-mm-format/m-p/937579#M83579</guid>
      <dc:creator>DrAbhijeetSafai</dc:creator>
      <dc:date>2024-07-30T09:58:20Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate hours and minutes into hh:mm format</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Concatenate-hours-and-minutes-into-hh-mm-format/m-p/937613#M83580</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/27897"&gt;@DrAbhijeetSafai&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;&amp;nbsp;, many thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- Dr. Abhijeet Safai&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;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:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;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;&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Sep 2024 04:26:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Concatenate-hours-and-minutes-into-hh-mm-format/m-p/937613#M83580</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-09-05T04:26:09Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate hours and minutes into hh:mm format</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Concatenate-hours-and-minutes-into-hh-mm-format/m-p/942627#M83628</link>
      <description>&lt;P&gt;To address your problem and calculate the time difference correctly, you should convert the hours and minutes into the &lt;CODE&gt;HH:MM&lt;/CODE&gt; 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'.&lt;/P&gt;&lt;P&gt;Here’s a step-by-step solution:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;STRONG&gt;Concatenate Hours and Minutes:&lt;/STRONG&gt; 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".&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;Convert the Time into SAS Time Format:&lt;/STRONG&gt; Once you have the time in &lt;CODE&gt;HH:MM&lt;/CODE&gt; format, you can use the &lt;CODE&gt;INPUT&lt;/CODE&gt; function to convert it into a SAS time value.&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;Calculate Time Difference:&lt;/STRONG&gt; After converting the times, you can subtract them to find the difference, and format the result in hours and minutes.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;Here’s a code example that does this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/* 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 &amp;gt;= 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.;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Explanation:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;STRONG&gt;Concatenation:&lt;/STRONG&gt; &lt;CODE&gt;cats(put(C3_0, z2.), ":", put(C3_1, z2.))&lt;/CODE&gt; ensures that hours and minutes are always two digits.&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;SAS Time Value:&lt;/STRONG&gt; &lt;CODE&gt;input(BT, time5.)&lt;/CODE&gt; converts the concatenated string (&lt;CODE&gt;HH:MM&lt;/CODE&gt;) into a SAS time value.&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;Time Difference Calculation:&lt;/STRONG&gt; If the time to get up is earlier than bedtime (crossing midnight), the code handles that by adding 24 hours (&lt;CODE&gt;'24:00't&lt;/CODE&gt;).&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;Formatting:&lt;/STRONG&gt; The result is formatted in the &lt;CODE&gt;HH:MM&lt;/CODE&gt; time format using the &lt;CODE&gt;time5.&lt;/CODE&gt; format.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;This will give you the correct output with the time difference calculated in &lt;CODE&gt;HH:MM&lt;/CODE&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Sep 2024 01:54:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Concatenate-hours-and-minutes-into-hh-mm-format/m-p/942627#M83628</guid>
      <dc:creator>Sarath_A_SAS</dc:creator>
      <dc:date>2024-09-05T01:54:46Z</dc:date>
    </item>
  </channel>
</rss>

