<?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: Converting 24 hour to 12 hour in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Converting-24-hour-to-12-hour/m-p/935965#M367931</link>
    <description>&lt;P&gt;I very strongly suspect that how ever you brought that value into SAS you did not actually create a time value.&lt;/P&gt;
&lt;P&gt;For one thing if this appears to work properly, i.e. 1201 has AMPM as "PM" then TIME is not a time value. You have compared to a numeric 1200 which for a time value would be a number of seconds or only 20 minutes and represent a clock time of 00:20:01 on a 24 hour clock. So PUT(TIME,TIME8.) is creating the wrong value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if Time &amp;lt; 1200
then AMPM='AM';&lt;BR /&gt;else AMPM='PM';&lt;/PRE&gt;
&lt;P&gt;IF 1200 is supposed to be 12 noon then with your current values the HMS function to get the hours and minutes for a real time value is likely easiest.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data example;
   time=1257;
   timevalue= hms(int(time/100),mod(time,100),0);
   format timevalue time8.;
run;&lt;/PRE&gt;
&lt;P&gt;Once you have the correct time value then you have a number of options such as create custom format that shows only the hour with AM or PM added.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Such as:&lt;/P&gt;
&lt;PRE&gt;proc format library=work;
   picture hour_ampm 
   low-high='%I %p' (datatype=time)
   ;
run;

proc print data=example;
   format timevalue hour_ampm.;
run;&lt;/PRE&gt;
&lt;P&gt;The directives in the PICTURE statement in Proc format %I means show hour on a 12 hour clock, the %p shows AM or PM for the time. These must be in single quotes. If you don't want a space between the number and the AM/PM then remove it from the directives.&lt;/P&gt;
&lt;P&gt;That let's you use all the expertise of the folks at SAS to avoid nasty logic.&lt;/P&gt;
&lt;P&gt;Or with a &lt;STRONG&gt;valid&lt;/STRONG&gt; time value use something like&lt;/P&gt;
&lt;PRE&gt;if Hour(timevalue)&amp;gt; 12 then do;&lt;BR /&gt;    myhour=hour(timevalue)-12;&lt;BR /&gt;    ampm='PM';&lt;BR /&gt;run;
else do;&lt;BR /&gt;   myhour=hour(timevalue);&lt;BR /&gt;   ampm='AM'&lt;BR /&gt;end;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am afraid this makes no sense to me at all:(i.e. I need 1445 to change to 2, 936 to become 9, and 40 to become&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 16 Jul 2024 22:04:27 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2024-07-16T22:04:27Z</dc:date>
    <item>
      <title>Converting 24 hour to 12 hour</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-24-hour-to-12-hour/m-p/935958#M367928</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I have a variable that is supposed to be a 24 hour time variable, but it comes in as 1445 or 936 or 40. Ultimately, I'll need a new column that is AM or PM and I need another column to read only the hour in a 12 hour clock (i.e. I need 1445 to change to 2, 936 to become 9, and 40 to become 12).&amp;nbsp; I have been able to make the AM and PM column, but unable to get the time to convert to a time format, let alone down to only the hour from a 12 hour clock. This is what I have currently&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;data want;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;set Have;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if Time &amp;lt; 1200&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;then AMPM='AM';&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;else AMPM='PM';&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;If Time = 9999&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;*this indicates no value available*&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Then AMPM = ' ';&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;IF DINJHRMNG = 9999&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;then DINJHRMNG = .;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Run;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data want2;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;set want;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;format Time HHMM.;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Time2=put(input(Time, time8.),tod8.);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jul 2024 21:09:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-24-hour-to-12-hour/m-p/935958#M367928</guid>
      <dc:creator>HiddenMuggle</dc:creator>
      <dc:date>2024-07-16T21:09:51Z</dc:date>
    </item>
    <item>
      <title>Re: Converting 24 hour to 12 hour</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-24-hour-to-12-hour/m-p/935965#M367931</link>
      <description>&lt;P&gt;I very strongly suspect that how ever you brought that value into SAS you did not actually create a time value.&lt;/P&gt;
&lt;P&gt;For one thing if this appears to work properly, i.e. 1201 has AMPM as "PM" then TIME is not a time value. You have compared to a numeric 1200 which for a time value would be a number of seconds or only 20 minutes and represent a clock time of 00:20:01 on a 24 hour clock. So PUT(TIME,TIME8.) is creating the wrong value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if Time &amp;lt; 1200
then AMPM='AM';&lt;BR /&gt;else AMPM='PM';&lt;/PRE&gt;
&lt;P&gt;IF 1200 is supposed to be 12 noon then with your current values the HMS function to get the hours and minutes for a real time value is likely easiest.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data example;
   time=1257;
   timevalue= hms(int(time/100),mod(time,100),0);
   format timevalue time8.;
run;&lt;/PRE&gt;
&lt;P&gt;Once you have the correct time value then you have a number of options such as create custom format that shows only the hour with AM or PM added.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Such as:&lt;/P&gt;
&lt;PRE&gt;proc format library=work;
   picture hour_ampm 
   low-high='%I %p' (datatype=time)
   ;
run;

proc print data=example;
   format timevalue hour_ampm.;
run;&lt;/PRE&gt;
&lt;P&gt;The directives in the PICTURE statement in Proc format %I means show hour on a 12 hour clock, the %p shows AM or PM for the time. These must be in single quotes. If you don't want a space between the number and the AM/PM then remove it from the directives.&lt;/P&gt;
&lt;P&gt;That let's you use all the expertise of the folks at SAS to avoid nasty logic.&lt;/P&gt;
&lt;P&gt;Or with a &lt;STRONG&gt;valid&lt;/STRONG&gt; time value use something like&lt;/P&gt;
&lt;PRE&gt;if Hour(timevalue)&amp;gt; 12 then do;&lt;BR /&gt;    myhour=hour(timevalue)-12;&lt;BR /&gt;    ampm='PM';&lt;BR /&gt;run;
else do;&lt;BR /&gt;   myhour=hour(timevalue);&lt;BR /&gt;   ampm='AM'&lt;BR /&gt;end;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am afraid this makes no sense to me at all:(i.e. I need 1445 to change to 2, 936 to become 9, and 40 to become&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jul 2024 22:04:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-24-hour-to-12-hour/m-p/935965#M367931</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-07-16T22:04:27Z</dc:date>
    </item>
    <item>
      <title>Re: Converting 24 hour to 12 hour</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-24-hour-to-12-hour/m-p/935976#M367935</link>
      <description>&lt;P&gt;You seem to be saying that you have simple integers instead of actual time of day values.&lt;/P&gt;
&lt;P&gt;So you want to interpret a number like 1,234 as being 12:34 PM .&lt;/P&gt;
&lt;P&gt;If so then convert it to a time value first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's make a dataset with your example values and convert into actual TIME values.&amp;nbsp; Then we can work on making your AMPM and HOUR variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input badtime;
cards;
9999
1445
936
40
;

data want;
  set have;
  if badtime=9999 then time=.;
  else time=hms(int(badtime/100),mod(badtime,100),0);
  format time tod5.;
  if time &amp;gt; '12:00't then ampm='PM';
  else if time &amp;gt;= 0  then ampm='AM';
  else ampm='  ';
  if not missing(time) then hour = hour(time) - 12*(ampm='PM');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1721171445290.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/98445iADEC4BB67ED0F806/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1721171445290.png" alt="Tom_0-1721171445290.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;But it might be better to print BADTIME using the COMMA format so it is more obvious those as just integer and not really time values at all.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_1-1721171509053.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/98446i98B127D21B250B21/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_1-1721171509053.png" alt="Tom_1-1721171509053.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jul 2024 23:11:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-24-hour-to-12-hour/m-p/935976#M367935</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-07-16T23:11:57Z</dc:date>
    </item>
    <item>
      <title>Re: Converting 24 hour to 12 hour</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-24-hour-to-12-hour/m-p/936098#M367984</link>
      <description>This worked exactly how I needed it! Thank you!</description>
      <pubDate>Wed, 17 Jul 2024 21:47:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-24-hour-to-12-hour/m-p/936098#M367984</guid>
      <dc:creator>HiddenMuggle</dc:creator>
      <dc:date>2024-07-17T21:47:00Z</dc:date>
    </item>
  </channel>
</rss>

