<?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: Time variable with greater than, less than in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Time-variable-with-greater-than-less-than/m-p/560693#M156855</link>
    <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;. Actually, my timevar is age in hours for babies. The time goes from birth through 72 hours. Some of the values of my timevar, for example, are 69:15, 34:56, 3:45.&lt;/P&gt;&lt;P&gt;The solution you presented doesn't work for values over 24. Do you have any other suggestions?&lt;/P&gt;&lt;P&gt;Maybe turn the timevar to a numeric variable?&amp;nbsp; Thank you.&lt;/P&gt;</description>
    <pubDate>Tue, 21 May 2019 23:46:37 GMT</pubDate>
    <dc:creator>eabc0351</dc:creator>
    <dc:date>2019-05-21T23:46:37Z</dc:date>
    <item>
      <title>Time variable with greater than, less than</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-variable-with-greater-than-less-than/m-p/560688#M156852</link>
      <description>&lt;P&gt;Hello. I am trying to use a time variable in an if-then statement. Please see example code below. The time is formatted as HHMM.&lt;/P&gt;&lt;P&gt;This code is not working due to the timevar portion. I understand that SAS doesn't store time in the formatted form. Is there a way to find out the equivalent values for my hours (i.e. 1,2,3,4...)&amp;nbsp; and still use this code? Or some better way?&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want; set have;
 if var1=1 and 
(2=&amp;lt;timevar&amp;lt;3 and var2=-.09) 
OR (3=&amp;lt;timevar&amp;lt;4 and var2&amp;lt;=-1.26) 
OR (4=&amp;lt;timevar&amp;lt;5 and var2&amp;lt;=-1.54) 
OR (5=&amp;lt;timevar&amp;lt;6 and var2&amp;lt;=-1.76) 
then varwant=1; else varwant=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 May 2019 23:19:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-variable-with-greater-than-less-than/m-p/560688#M156852</guid>
      <dc:creator>eabc0351</dc:creator>
      <dc:date>2019-05-21T23:19:47Z</dc:date>
    </item>
    <item>
      <title>Re: Time variable with greater than, less than</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-variable-with-greater-than-less-than/m-p/560691#M156853</link>
      <description>&lt;P&gt;You probably want to use HOUR(timevar) in some form.&lt;/P&gt;
&lt;P&gt;If you have a time like 13:45 then Hour function would return 13&lt;/P&gt;
&lt;P&gt;This type of test&lt;/P&gt;
&lt;PRE&gt;3=&amp;lt;timevar&amp;lt;4
&lt;/PRE&gt;
&lt;P&gt;would be&lt;/P&gt;
&lt;PRE&gt;3 = hour(timevar)&lt;/PRE&gt;
&lt;P&gt;If the time were 4:00 the hour would be 4. Hour(&amp;nbsp;3:59:59 ) would return 3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The question might be though do &lt;STRONG&gt;you&lt;/STRONG&gt; mean 1AM or 1PM? The SAS Hour function returns 0 to 23. 0 for time from midnight to 00:59:59 in the morning.&lt;/P&gt;</description>
      <pubDate>Tue, 21 May 2019 23:31:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-variable-with-greater-than-less-than/m-p/560691#M156853</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-05-21T23:31:11Z</dc:date>
    </item>
    <item>
      <title>Re: Time variable with greater than, less than</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-variable-with-greater-than-less-than/m-p/560693#M156855</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;. Actually, my timevar is age in hours for babies. The time goes from birth through 72 hours. Some of the values of my timevar, for example, are 69:15, 34:56, 3:45.&lt;/P&gt;&lt;P&gt;The solution you presented doesn't work for values over 24. Do you have any other suggestions?&lt;/P&gt;&lt;P&gt;Maybe turn the timevar to a numeric variable?&amp;nbsp; Thank you.&lt;/P&gt;</description>
      <pubDate>Tue, 21 May 2019 23:46:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-variable-with-greater-than-less-than/m-p/560693#M156855</guid>
      <dc:creator>eabc0351</dc:creator>
      <dc:date>2019-05-21T23:46:37Z</dc:date>
    </item>
    <item>
      <title>Re: Time variable with greater than, less than</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-variable-with-greater-than-less-than/m-p/560699#M156859</link>
      <description>&lt;P&gt;The following will give you hours elapsed since birth (&lt;CODE class=" language-sas"&gt;20may2019:10:30:00&lt;/CODE&gt;) to current time in hh:mm:ss format&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
hours=(intck('seconds','20may2019:10:30:00'dt,datetime()));
format hours time12.;
put hours;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 May 2019 01:30:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-variable-with-greater-than-less-than/m-p/560699#M156859</guid>
      <dc:creator>ghosh</dc:creator>
      <dc:date>2019-05-22T01:30:02Z</dc:date>
    </item>
    <item>
      <title>Re: Time variable with greater than, less than</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-variable-with-greater-than-less-than/m-p/560702#M156861</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Maybe turn the timevar to a numeric variable?&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Time variables are numeric. They contain seconds.&amp;nbsp; Unless it is a character variable.&amp;nbsp; In which case you should convert to a number.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 May 2019 01:47:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-variable-with-greater-than-less-than/m-p/560702#M156861</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-22T01:47:56Z</dc:date>
    </item>
    <item>
      <title>Re: Time variable with greater than, less than</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-variable-with-greater-than-less-than/m-p/560704#M156862</link>
      <description>&lt;P&gt;You didn't specify a "have" dataset, so I can only guess at representative values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does this work for you?&amp;nbsp; Adjust as required:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input time time5.;
   format time time8.;
   datalines;
00:15
13:45
23:59
24:15
28:45
37:10
40:40
50:50
65:55
71:59
73:01
;
run;

data want1;
   set have;
   hour=hour(time);  * so that doesn't work ;
   format _all_;  * shows you the internal value for your time column ;
run;

data want2;
   set have;
   select;
      when ("00:00:00"t &amp;lt;= time &amp;lt; "24:00:00"t) day=1;
      when ("24:00:00"t &amp;lt;= time &amp;lt; "48:00:00"t) day=2;
      when ("48:00:00"t &amp;lt;= time &amp;lt; "72:00:00"t) day=3;
      otherwise day=-1;  * representing "unknown" day ;
   end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000780334.htm" target="_blank"&gt;https://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000780334.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a002200738.htm" target="_blank"&gt;https://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a002200738.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note the 2nd link says:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN class="strong"&gt;SAS time value&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; is a value representing the number of seconds since midnight of the current day. SAS time values are between 0 and 86400.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;IMO this is incorrect given the test case above.&lt;/P&gt;</description>
      <pubDate>Wed, 22 May 2019 02:29:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-variable-with-greater-than-less-than/m-p/560704#M156862</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-05-22T02:29:20Z</dc:date>
    </item>
    <item>
      <title>Re: Time variable with greater than, less than</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-variable-with-greater-than-less-than/m-p/560750#M156886</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15043"&gt;@ScottBass&lt;/a&gt;yes, some version of that worked for me. Instead of days, I did hours. That provided a numeric variable (best12., not a time format) to use in the previous code. Thank you all!&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
   set have;
   select;
      when ("00:00:00"t &amp;lt;= timevar &amp;lt; "01:00:00"t) hours=1;
      when ("01:00:00"t &amp;lt;= timevar&amp;lt; "02:00:00"t) hours=2;
      when ("02:00:00"t &amp;lt;= timevar&amp;lt; "03:00:00"t) hours=3;
      when ("03:00:00"t &amp;lt;= timevar&amp;lt; "04:00:00"t) hours=4;
      otherwise hours=-1;  * representing "unknown" day ;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 May 2019 10:02:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-variable-with-greater-than-less-than/m-p/560750#M156886</guid>
      <dc:creator>eabc0351</dc:creator>
      <dc:date>2019-05-22T10:02:09Z</dc:date>
    </item>
    <item>
      <title>Re: Time variable with greater than, less than</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-variable-with-greater-than-less-than/m-p/560983#M156973</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/148180"&gt;@eabc0351&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15043"&gt;@ScottBass&lt;/a&gt;yes, some version of that worked for me. Instead of days, I did hours. That provided a numeric variable (best12., not a time format) to use in the previous code. Thank you all!&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
   set have;
   select;
      when ("00:00:00"t &amp;lt;= timevar &amp;lt; "01:00:00"t) hours=1;
      when ("01:00:00"t &amp;lt;= timevar&amp;lt; "02:00:00"t) hours=2;
      when ("02:00:00"t &amp;lt;= timevar&amp;lt; "03:00:00"t) hours=3;
      when ("03:00:00"t &amp;lt;= timevar&amp;lt; "04:00:00"t) hours=4;
      otherwise hours=-1;  * representing "unknown" day ;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I did think of another approach which, depending on your circumstances, may be a better approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The above works fine for a small number of ranges (i.e. the three you specified), but gets problematic if the number of ranges increases.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another approach is to create a range format.&amp;nbsp; And, since formats can be created programatically via a cntlin dataset, you can easily create as many ranges as required.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is sample code.&amp;nbsp; Hope this helps...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* for debugging: create a test format with high end exclusion ;
proc format;
   value foo
      1 - &amp;lt; 2=1
      2 - &amp;lt; 3=2
   ;
run;

* for debugging: examine the format for the cntlout dataset ;
proc format cntlout=cntlout;
run;

* create a proc format cntlin dataset ;
data cntlin;
   format fmtname type start end label;  * this is just to set the PDV order and is optional ;
   length fmtname $32 type $1 eexcl $1;  * or this too could have set the PDV order ;

   fmtname='time2hours';
   type='N';
   eexcl='Y';

   start="00:00:00"t;
   do label=0 to 72;
      end=intnx('hour',start,1,'S');
      output;
      start=end;
   end;
   format start end time.;
run;

* for debugging only: show the actual (unformatted) values of start and end ;
proc print;
   format _all_;
run;

* create the format ;
proc format cntlin=cntlin;
run;

/* a quick lesson on formats/informats:
formats:    can accept either numeric or character input, always returns character output
informats:  always accepts character input, returns either numeric or character output

I used a numeric format, so it accepts numeric input (time) but returns character output (hour)
So we will need to convert the output from character to numeric using the input function
*/

data test;
   set cntlin;
   hour=input(put(start,time2hours.),best.);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;That provided a numeric variable (best12., not a time format)&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The time variables are &lt;U&gt;always&lt;/U&gt; numeric, it is only the time format that makes it appear as a "time".&amp;nbsp; There is no "best12." or "time." data type.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 May 2019 22:42:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-variable-with-greater-than-less-than/m-p/560983#M156973</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-05-22T22:42:14Z</dc:date>
    </item>
  </channel>
</rss>

