<?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: data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/data-step/m-p/246293#M46054</link>
    <description>&lt;P&gt;Here is the solution from SAS-L&lt;/P&gt;
&lt;P&gt;&lt;A href="https://listserv.uga.edu/cgi-bin/wa?A0=SAS-L" target="_blank"&gt;https://listserv.uga.edu/cgi-bin/wa?A0=SAS-L&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc format; 
  picture durt 
  other='%0nD %0H:%0M:%0s' (datatype=time); 
run; 

proc fcmp outlib=work.func.fmt;
   function xx(c) $;
      length fmt $ 20;
      day=int(divide(c,60*60*24));
      return (put(day,z2.) || 'D ' || put(c,tod.));
   endsub;
run;
options cmplib=(work.func);
proc format;
   value new(default=20) other=[xx()];
 run;
data _null_; 
 
   t= 368000; 
     put t durt. / t new.; 
run; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;OUTPUT:
&amp;nbsp;&amp;nbsp;&amp;nbsp;4D 06:13:20
&amp;nbsp;04D 06:13:20 &lt;/PRE&gt;</description>
    <pubDate>Wed, 27 Jan 2016 07:46:12 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2016-01-27T07:46:12Z</dc:date>
    <item>
      <title>data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/data-step/m-p/246285#M46051</link>
      <description>&lt;P&gt;In my data set I have two datetime variables in numeric format. I need to calculate difference between the datetime and put the difference into three variables, Forexample,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;variable1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; variable2&lt;/P&gt;&lt;P&gt;22oct15:10:47:00 &amp;nbsp; &amp;nbsp; &amp;nbsp; 21oct15:10:00:02&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the difference between &amp;nbsp;variable1-variable2=1day 0 hour 47min into three variables &amp;nbsp;days hour min&lt;/P&gt;&lt;P&gt;&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; &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;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp;47&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using the following code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;data one;&lt;BR /&gt;set two;&lt;/P&gt;&lt;P&gt;&amp;nbsp;LDDAYS= variable1-variable2;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;this code is not giving me expected values. Thank you&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2016 06:38:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/data-step/m-p/246285#M46051</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2016-01-27T06:38:56Z</dc:date>
    </item>
    <item>
      <title>Re: data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/data-step/m-p/246287#M46053</link>
      <description>&lt;P&gt;datetime values area stored in SAS as numbers, counting seconds from 01jan1960:00:00:00.&lt;/P&gt;
&lt;P&gt;Therefore your initial subtraction will give you another datetime value, which will probably be in early January, 1960, if you assign it a datetime format.&lt;/P&gt;
&lt;P&gt;I suggest this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;diff = variable1 - variable2; * difference in seconds;
lddays = int(diff / 86400); * days;
difftime = diff - lddays * 86400; * subtract the days;
format difftime time8.; * display as time value;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2016 07:21:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/data-step/m-p/246287#M46053</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-01-27T07:21:29Z</dc:date>
    </item>
    <item>
      <title>Re: data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/data-step/m-p/246293#M46054</link>
      <description>&lt;P&gt;Here is the solution from SAS-L&lt;/P&gt;
&lt;P&gt;&lt;A href="https://listserv.uga.edu/cgi-bin/wa?A0=SAS-L" target="_blank"&gt;https://listserv.uga.edu/cgi-bin/wa?A0=SAS-L&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc format; 
  picture durt 
  other='%0nD %0H:%0M:%0s' (datatype=time); 
run; 

proc fcmp outlib=work.func.fmt;
   function xx(c) $;
      length fmt $ 20;
      day=int(divide(c,60*60*24));
      return (put(day,z2.) || 'D ' || put(c,tod.));
   endsub;
run;
options cmplib=(work.func);
proc format;
   value new(default=20) other=[xx()];
 run;
data _null_; 
 
   t= 368000; 
     put t durt. / t new.; 
run; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;OUTPUT:
&amp;nbsp;&amp;nbsp;&amp;nbsp;4D 06:13:20
&amp;nbsp;04D 06:13:20 &lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Jan 2016 07:46:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/data-step/m-p/246293#M46054</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-01-27T07:46:12Z</dc:date>
    </item>
    <item>
      <title>Re: data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/data-step/m-p/246294#M46055</link>
      <description>&lt;P&gt;Thank you. That worked. But I need to separate hours and minutes into two variables.&lt;/P&gt;&lt;P&gt;Forexample I got difftime(In redink below code) 1:15:48. I need to place the 1 in hour variable and round of 15.48=16 in Min variable. Thanks for your quick response.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;diff &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; variable1 &lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt; variable2&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;SPAN class="token comment"&gt;* difference in seconds;&lt;/SPAN&gt;
lddays &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;int&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;diff &lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;86400&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;SPAN class="token comment"&gt;* days;&lt;/SPAN&gt;
&lt;FONT color="#FF0000"&gt;&lt;EM&gt;difftime&lt;/EM&gt; &lt;/FONT&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; diff &lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt; lddays &lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;86400&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;SPAN class="token comment"&gt;* subtract thedays;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;format&lt;/SPAN&gt; difftime time8&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;SPAN class="token comment"&gt;* display as time vale;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2016 08:05:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/data-step/m-p/246294#M46055</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2016-01-27T08:05:24Z</dc:date>
    </item>
    <item>
      <title>Re: data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/data-step/m-p/246295#M46056</link>
      <description>&lt;P&gt;Well, you use the same principle. Use factor 3600 to extract the hours, and 60 to extract the minutes. Use the &lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000245942.htm" target="_self"&gt;round()&lt;/A&gt; function on the minutes and assign simple numeric formats to the target variables.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2016 08:20:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/data-step/m-p/246295#M46056</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-01-27T08:20:32Z</dc:date>
    </item>
    <item>
      <title>Re: data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/data-step/m-p/246306#M46061</link>
      <description>&lt;P&gt;I would also suggest, that rather than using a set number, which can be tricky to read if you don't know what that number represents, you can use the SAS functions -&amp;gt; year(), month(), day(), hour(), minute() to extract parts from a date/datetime and use those. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
  variable1="22oct15:10:47:00"dt; variable2="21oct15:10:00:02"dt;
  difference_in_days=datepart(variable1)-datepart(variable2);
  difference_in_hours=hour(timepart(variable1)-timepart(variable2));
  difference_in_minutes=minute(timepart(variable1)-timepart(variable2));
  format variable1 variable2 datetime.;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Jan 2016 09:19:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/data-step/m-p/246306#M46061</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-01-27T09:19:54Z</dc:date>
    </item>
    <item>
      <title>Re: data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/data-step/m-p/246317#M46064</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;RW9 wrote: &lt;BR /&gt;
&lt;PRE&gt;data want;
  variable1="22oct15:10:47:00"dt; variable2="21oct15:10:00:02"dt;
  difference_in_days=datepart(variable1)-datepart(variable2);
  difference_in_hours=hour(timepart(variable1)-timepart(variable2));
  difference_in_minutes=minute(timepart(variable1)-timepart(variable2));
  format variable1 variable2 datetime.;
run;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;When doing this, one needs to take care of cases where timepart(variable1)-timepart(variable2) turns out to be negative, and reduce the days by 1.&lt;/P&gt;
&lt;P&gt;ie&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;variable1='27jan2016:09:23:15'dt;
variable2='26jan2016:15:45:20'dt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Jan 2016 10:04:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/data-step/m-p/246317#M46064</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-01-27T10:04:36Z</dc:date>
    </item>
  </channel>
</rss>

