<?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 Difference between Two dataset variable values in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367584#M11091</link>
    <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have various proceedures where I need to work out the time or the&amp;nbsp;age difference between two datetime variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In principle I principle I thought that I could just deduct datetime2 from datetime1, as this would give me the time difference in seconds.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Data one;
Set  two;
REFER_VAR= (REFER_DT - LST_UPDT_DT  );
RUN;&lt;/PRE&gt;&lt;P&gt;Seems not to&amp;nbsp; work as results are faulty as can be seen on the screenshot below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this example below I want the difference between REFER_DT&amp;nbsp; and&amp;nbsp; LST_UPDT_DT resulting in REFER_VAR.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Eventually I want to keep only the smallest value of each&amp;nbsp; REFER_VAR for each REFER_ID.&lt;/P&gt;&lt;P&gt;I think I can do the last part with a proc means function (but any advice is appreaciated):&lt;/P&gt;&lt;P&gt;proc means data= a&amp;nbsp; NOPRINT ;&lt;/P&gt;&lt;P&gt;class REFER_ID;&lt;/P&gt;&lt;P&gt;var REFER_VAR;output out=b min=min max=max;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, I am&amp;nbsp; failling with the datetime values. Can somenody please help how to set them up between to datetime variables? There are plenty of examples in the net with single dates, but I am having trouble to incorporate my datetime variables:&amp;nbsp; REFER_DT&amp;nbsp; and&amp;nbsp; LST_UPDT_DT&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What do I need to do in order to get the right timedifferences given that different months have different mount of days?&lt;/P&gt;&lt;P&gt;Many thanks&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/13958i76B2F735D3BFAA7F/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="timedif.JPG" title="timedif.JPG" /&gt;</description>
    <pubDate>Fri, 16 Jun 2017 02:38:41 GMT</pubDate>
    <dc:creator>AnnaNZ</dc:creator>
    <dc:date>2017-06-16T02:38:41Z</dc:date>
    <item>
      <title>Difference between Two dataset variable values</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367584#M11091</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have various proceedures where I need to work out the time or the&amp;nbsp;age difference between two datetime variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In principle I principle I thought that I could just deduct datetime2 from datetime1, as this would give me the time difference in seconds.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Data one;
Set  two;
REFER_VAR= (REFER_DT - LST_UPDT_DT  );
RUN;&lt;/PRE&gt;&lt;P&gt;Seems not to&amp;nbsp; work as results are faulty as can be seen on the screenshot below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this example below I want the difference between REFER_DT&amp;nbsp; and&amp;nbsp; LST_UPDT_DT resulting in REFER_VAR.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Eventually I want to keep only the smallest value of each&amp;nbsp; REFER_VAR for each REFER_ID.&lt;/P&gt;&lt;P&gt;I think I can do the last part with a proc means function (but any advice is appreaciated):&lt;/P&gt;&lt;P&gt;proc means data= a&amp;nbsp; NOPRINT ;&lt;/P&gt;&lt;P&gt;class REFER_ID;&lt;/P&gt;&lt;P&gt;var REFER_VAR;output out=b min=min max=max;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, I am&amp;nbsp; failling with the datetime values. Can somenody please help how to set them up between to datetime variables? There are plenty of examples in the net with single dates, but I am having trouble to incorporate my datetime variables:&amp;nbsp; REFER_DT&amp;nbsp; and&amp;nbsp; LST_UPDT_DT&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What do I need to do in order to get the right timedifferences given that different months have different mount of days?&lt;/P&gt;&lt;P&gt;Many thanks&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/13958i76B2F735D3BFAA7F/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="timedif.JPG" title="timedif.JPG" /&gt;</description>
      <pubDate>Fri, 16 Jun 2017 02:38:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367584#M11091</guid>
      <dc:creator>AnnaNZ</dc:creator>
      <dc:date>2017-06-16T02:38:41Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dataset variable values</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367590#M11092</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG title="timedif.JPG" alt="timedif.JPG" src="https://communities.sas.com/t5/image/serverpage/image-id/9451iEDB612CE17D3A90F/image-size/original?v=1.0&amp;amp;px=-1" border="0" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Timedifference needed between LST_UPDT_DT&amp;nbsp; and REFER_DT&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then only those REFER_ID with the smaller&amp;nbsp;timedifference (for each REER_ID - there are several of the same) are selected to the next datastep&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2017 02:44:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367590#M11092</guid>
      <dc:creator>AnnaNZ</dc:creator>
      <dc:date>2017-06-16T02:44:26Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dataset variable values</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367591#M11093</link>
      <description>&lt;P&gt;The difference is being calculated currently. Why do you think it's wrong?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2017 02:48:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367591#M11093</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-06-16T02:48:16Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dataset variable values</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367594#M11094</link>
      <description>&lt;P&gt;may be something like this&lt;/P&gt;
&lt;PRE&gt; proc sql;
  create table have as 
  select * from
  (select *, (dtime2-dtime1) as difference from want)
  group by id
  having difference = min(difference);
 &lt;/PRE&gt;</description>
      <pubDate>Fri, 16 Jun 2017 03:01:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367594#M11094</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2017-06-16T03:01:08Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dataset variable values</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367597#M11095</link>
      <description>&lt;P&gt;So noithing else neeed to get the correct time difference?&lt;/P&gt;&lt;P&gt;- and then just divide by 3600 to get minutes or 86400 to get days.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2017 03:12:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367597#M11095</guid>
      <dc:creator>AnnaNZ</dc:creator>
      <dc:date>2017-06-16T03:12:35Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dataset variable values</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367600#M11096</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/144103"&gt;@AnnaNZ&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;So noithing else neeed to get the correct time difference?&lt;/P&gt;
&lt;P&gt;- and then just divide by 3600 to get minutes or 86400 to get days.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Not necessarily.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You just stated it was incorrect, and I'm saying it's not. There's a big difference between 'correct'&amp;nbsp;and not what you want, especially because you had already stated you knew it would give you the difference in seconds, which is what is shown.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want the difference in days/months use INTCK, with DTMONTH or DTDAY to get the months or days difference. Personally, I prefer the division method for anything besides weeks and days. There isn't a standard definition for a 'month' which is annoying from a mathematical perspective. But I'm Canadian and we have the metric system &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could also convert to date variables by using the DATEPART function and then use your standard processes that you know.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2017 03:23:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367600#M11096</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-06-16T03:23:38Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dataset variable values</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367601#M11097</link>
      <description>&lt;P&gt;yes, very true, it is was a case of not seeing the wood for the trees - thnak you very much for your clarification&lt;/P&gt;&lt;P&gt;For reaspons of smplicity, I would like to keep the variables in datetime, as it is easier to come back to them later.&lt;/P&gt;&lt;P&gt;Thank you very much &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2017 03:35:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367601#M11097</guid>
      <dc:creator>AnnaNZ</dc:creator>
      <dc:date>2017-06-16T03:35:45Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dataset variable values</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367602#M11098</link>
      <description>&lt;P&gt;worked beautifully thank you&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2017 03:36:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367602#M11098</guid>
      <dc:creator>AnnaNZ</dc:creator>
      <dc:date>2017-06-16T03:36:46Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dataset variable values</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367603#M11099</link>
      <description>&lt;P&gt;What is the best way then to convert this into age?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Say I have two datetime varoiables: DOB and a variable called&amp;nbsp;School Entry, at which age were the children schooled ?&lt;/P&gt;&lt;P&gt;So again I could just get Time_in_Seconds = (School Entry - DOB) - what is the best way to convert this into age including months and days?&lt;/P&gt;&lt;P&gt;May thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2017 03:48:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367603#M11099</guid>
      <dc:creator>AnnaNZ</dc:creator>
      <dc:date>2017-06-16T03:48:35Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dataset variable values</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367604#M11100</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/144103"&gt;@AnnaNZ&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So again I could just get Time_in_Seconds = (School Entry - DOB) - what is the best way to convert this into age including months and days?&lt;/P&gt;
&lt;P&gt;May thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;What does that mean? &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you want age in months - how are you defining months? ie Zuli&amp;nbsp;is 360 months old&lt;/P&gt;
&lt;P&gt;Do you want age in days? ie Zuli is 3650 days old&lt;/P&gt;
&lt;P&gt;Do you want age in Year, month, days? Ie Zuli is 10 years, 5 months and 4 days old&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2017 03:56:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367604#M11100</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-06-16T03:56:28Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dataset variable values</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367606#M11101</link>
      <description>&lt;P&gt;I am age in terms of years, months and days&lt;/P&gt;&lt;P&gt;Often it is just displayed in years, but I would want a more exact approach, therefore something like in your last example: 'Ie Zuli is 10 years, 5 months and 4 days old. '&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I could create someting over multiple colums, whereby&lt;/P&gt;&lt;P&gt;age_min&amp;nbsp; =ROUND ((age_sec)/60);&lt;BR /&gt;age_h&amp;nbsp;&amp;nbsp;&amp;nbsp; =ROUND&amp;nbsp; ((age_sec)/3600);&lt;BR /&gt;age_d&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; =ROUND ((age_sec)/86400);&lt;BR /&gt;age_y&amp;nbsp;&amp;nbsp; =ROUND ((age_sec)/31536000);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but that is not very elegant. Would yu know a better way to deal with this?&lt;/P&gt;&lt;P&gt;many thanks&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2017 04:04:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367606#M11101</guid>
      <dc:creator>AnnaNZ</dc:creator>
      <dc:date>2017-06-16T04:04:20Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dataset variable values</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367607#M11102</link>
      <description>&lt;P&gt;I doubt there's a way with 'less code' but if you're going to do this occoasionally I'd consider a custom function or format.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I feel like I've seen a time format that could do this (custom time) via proc format.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Those are the approaches I would try. I can't provide any code now, but I'll try to remember to look at this question again tomorrow, if someone hasn't already answered it.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2017 04:28:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367607#M11102</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-06-16T04:28:24Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dataset variable values</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367935#M11106</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;&lt;P&gt;the appraoch does in principle resolve my issue however, it delets all&amp;nbsp;&lt;SPAN&gt;REFER_IDs&amp;nbsp;&lt;/SPAN&gt;that have&amp;nbsp;no timedifference, meaning,it deletes all the enties where&amp;nbsp;&amp;nbsp;&lt;SPAN&gt; LST_UPDT_DT &amp;nbsp;=&amp;nbsp; REFER_DT&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;How can I immplement a keep statement ? Something like keep REFER_ID if &amp;nbsp; LST_UPDT_DT &amp;nbsp;=&amp;nbsp; REFER_DT&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Many thanks&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Jun 2017 08:44:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Difference-between-Two-dataset-variable-values/m-p/367935#M11106</guid>
      <dc:creator>AnnaNZ</dc:creator>
      <dc:date>2017-06-17T08:44:12Z</dc:date>
    </item>
  </channel>
</rss>

