<?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 Caveat when calculating ages in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Caveat-when-calculating-ages/m-p/858081#M339034</link>
    <description>&lt;P&gt;At first&amp;nbsp;I thought there was a bug in SAS!&amp;nbsp; Later I realized it was a problem with one of my "if" conditions.&amp;nbsp; It was actually an issue of me not realizing that '01JAN1960' has a value of 0.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let's say I want to calculate the age for some records, given a birthdate field that is usually missing.&lt;/P&gt;&lt;P&gt;Here is a sample data set:&lt;/P&gt;&lt;P&gt;data sample;&lt;BR /&gt;id='1';birthdate='01JAN1970'd; output;&lt;BR /&gt;id='2';birthdate='02JAN1970'd;output;&lt;BR /&gt;id='3';birthdate=.;output;&lt;BR /&gt;id='4';birthdate='01JAN1960'd;output;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;/*In a second data step, I calculate the ages:*/&lt;/P&gt;&lt;P&gt;data ages;&lt;BR /&gt;set sample;&lt;BR /&gt;if birthdate then age = floor((intck('month',birthdate,today()) - (day(today()) &amp;lt; day(birthdate))) / 12);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you see the problem?&amp;nbsp; The intention is to calculate the age only if the birthdate is not missing.&amp;nbsp; However, if the birthdate is on January 1, 1960, then the value is 0, which is a numeric false.&amp;nbsp; The "if" resolves to false, so the "then" part of the statement doesn't get executed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The "if" statement should read as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp; if birthdate ne . then age&amp;nbsp;= floor((intck('month',birthdate,today()) - (day(today()) &amp;lt; day(birthdate))) / 12);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It was one of those gotcha moments.&lt;/P&gt;</description>
    <pubDate>Thu, 09 Feb 2023 16:27:56 GMT</pubDate>
    <dc:creator>TCL</dc:creator>
    <dc:date>2023-02-09T16:27:56Z</dc:date>
    <item>
      <title>Caveat when calculating ages</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Caveat-when-calculating-ages/m-p/858081#M339034</link>
      <description>&lt;P&gt;At first&amp;nbsp;I thought there was a bug in SAS!&amp;nbsp; Later I realized it was a problem with one of my "if" conditions.&amp;nbsp; It was actually an issue of me not realizing that '01JAN1960' has a value of 0.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let's say I want to calculate the age for some records, given a birthdate field that is usually missing.&lt;/P&gt;&lt;P&gt;Here is a sample data set:&lt;/P&gt;&lt;P&gt;data sample;&lt;BR /&gt;id='1';birthdate='01JAN1970'd; output;&lt;BR /&gt;id='2';birthdate='02JAN1970'd;output;&lt;BR /&gt;id='3';birthdate=.;output;&lt;BR /&gt;id='4';birthdate='01JAN1960'd;output;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;/*In a second data step, I calculate the ages:*/&lt;/P&gt;&lt;P&gt;data ages;&lt;BR /&gt;set sample;&lt;BR /&gt;if birthdate then age = floor((intck('month',birthdate,today()) - (day(today()) &amp;lt; day(birthdate))) / 12);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you see the problem?&amp;nbsp; The intention is to calculate the age only if the birthdate is not missing.&amp;nbsp; However, if the birthdate is on January 1, 1960, then the value is 0, which is a numeric false.&amp;nbsp; The "if" resolves to false, so the "then" part of the statement doesn't get executed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The "if" statement should read as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp; if birthdate ne . then age&amp;nbsp;= floor((intck('month',birthdate,today()) - (day(today()) &amp;lt; day(birthdate))) / 12);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It was one of those gotcha moments.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Feb 2023 16:27:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Caveat-when-calculating-ages/m-p/858081#M339034</guid>
      <dc:creator>TCL</dc:creator>
      <dc:date>2023-02-09T16:27:56Z</dc:date>
    </item>
    <item>
      <title>Re: Caveat when calculating ages</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Caveat-when-calculating-ages/m-p/858091#M339035</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if not missing(birthdate) then age = intck('year',birthdate,today(),'c');&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Feb 2023 17:18:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Caveat-when-calculating-ages/m-p/858091#M339035</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-02-09T17:18:16Z</dc:date>
    </item>
    <item>
      <title>Re: Caveat when calculating ages</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Caveat-when-calculating-ages/m-p/858113#M339038</link>
      <description>&lt;P&gt;Might want to look at the YRDIF function with the Floor function if you don't need a decimal part of a year.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Feb 2023 18:36:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Caveat-when-calculating-ages/m-p/858113#M339038</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-02-09T18:36:50Z</dc:date>
    </item>
    <item>
      <title>Re: Caveat when calculating ages</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Caveat-when-calculating-ages/m-p/858137#M339047</link>
      <description>&lt;P&gt;I'm slightly curious as to why SAS doesn't create an "age" function, since it's something that is often computed.&amp;nbsp; The "YRDIF" function doesn't immediately come to mind.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Feb 2023 21:30:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Caveat-when-calculating-ages/m-p/858137#M339047</guid>
      <dc:creator>TCL</dc:creator>
      <dc:date>2023-02-09T21:30:43Z</dc:date>
    </item>
  </channel>
</rss>

