<?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: Problems calculating age from a datetime in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Problems-calculating-age-from-a-datetime/m-p/790976#M32564</link>
    <description>&lt;P&gt;What happens when you run something like this:&lt;/P&gt;
&lt;PRE&gt;DATA have;
set have;
birth_date_formatted= input(birth_datetime_formatted,date9.);
RUN;&lt;/PRE&gt;
&lt;P&gt;Is that the variable BIRTH_DATETIME_FORMATTED, if it is numeric gets turned into a string of digits. If the value was a datetime like&amp;nbsp; 21SEP96:00:00:00 then the digits would be 1158883200, which in no way resembles a DATE9. format, for use by the input function before the informat is attempted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Seldom does implicit conversion of a numeric value get converted to a string that Input requires for a given numeric value. One example:&lt;/P&gt;
&lt;PRE&gt;data example;
   x=123;
   y= input(x,8.);
run;&lt;/PRE&gt;
&lt;P&gt;Y in this example will be missing. That is because before the informat 8. is used to read the value of X is turned into a string using the BEST12 format and is right justified. So there are nothing but spaces in the first 8 positions to read.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are going to attempt to use INPUT with a numeric variable it is up to you to convert the number into a string you know the content. Example&lt;/P&gt;
&lt;PRE&gt;data example;
   x=123;
   y= input(put(x,best12. -L),8.);
run;&lt;/PRE&gt;
&lt;P&gt;The Put function creates a string, the format controls the appearance and the option -L left justifies the result so the leading spaces don't become an issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is better to use the functions such as DATEPART or MDY or DHMS or HMS to covert numeric date and time values around.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/ta-p/424354" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/ta-p/424354&lt;/A&gt; has a PDF with much information about working with dates and such.&lt;/P&gt;</description>
    <pubDate>Wed, 19 Jan 2022 17:58:20 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2022-01-19T17:58:20Z</dc:date>
    <item>
      <title>Problems calculating age from a datetime</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Problems-calculating-age-from-a-datetime/m-p/790965#M32557</link>
      <description>&lt;P&gt;The current table I have is different people. One of the columns is their birthdate formatted as numeric, datetime16, length 8. i.e. -&amp;gt; &lt;SPAN&gt;21SEP96:00:00:00&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I convert this to date9. and add it as a new column&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;DATA have;
set have;
birth_date_formatted= input(birth_datetime_formatted,date9.);
RUN;&lt;/PRE&gt;&lt;P&gt;The result is this, an empty column.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="heyyou1_0-1642611938706.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/67515i9E274D6EAFA2BF4A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="heyyou1_0-1642611938706.png" alt="heyyou1_0-1642611938706.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="heyyou1_3-1642612010676.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/67519iF4FA6A87C65001D8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="heyyou1_3-1642612010676.png" alt="heyyou1_3-1642612010676.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="heyyou1_2-1642611995412.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/67517i4BFD6D2464051490/image-size/medium?v=v2&amp;amp;px=400" role="button" title="heyyou1_2-1642611995412.png" alt="heyyou1_2-1642611995412.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="heyyou1_1-1642611973751.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/67516i52BB46FCDE44B475/image-size/medium?v=v2&amp;amp;px=400" role="button" title="heyyou1_1-1642611973751.png" alt="heyyou1_1-1642611973751.png" /&gt;&lt;/span&gt;&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;P&gt;I then use the following code to split the dataset if the person is over 65 years old.&lt;/P&gt;&lt;PRE&gt;PROC sql;
	create table over_65_filtered as 
	select * FROM have
	WHERE intck('year',birth_date_formatted,TODAY()) &amp;gt;= 65;
QUIT;&lt;/PRE&gt;&lt;P&gt;However, this creates an empty table.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Why is the datetime to date conversion resulting in empty data?&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jan 2022 17:08:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Problems-calculating-age-from-a-datetime/m-p/790965#M32557</guid>
      <dc:creator>heyyou1</dc:creator>
      <dc:date>2022-01-19T17:08:04Z</dc:date>
    </item>
    <item>
      <title>Re: Problems calculating age from a datetime</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Problems-calculating-age-from-a-datetime/m-p/790973#M32562</link>
      <description>&lt;P&gt;Your approach is incorrect, to convert a datetime to a date, use datepart().&lt;BR /&gt;INPUT is used to convert a character to a date/datetime. &lt;BR /&gt;I do not recommend using the name HAVE repeated. &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA want;
set have;
birth_date_formatted= datepart(USER_pat_birth_date);
format birth_date_formatted date9.;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;EDIT: to refer to correct variable.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/406768"&gt;@heyyou1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The current table I have is different people. One of the columns is their &lt;STRONG&gt;birthdate formatted as numeric, datetime16, length 8&lt;/STRONG&gt;. i.e. -&amp;gt; &lt;SPAN&gt;21SEP96:00:00:00&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I convert this to date9. and add it as a new column&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;DATA have;
set have;
birth_date_formatted= input(birth_datetime_formatted,date9.);
RUN;&lt;/PRE&gt;
&lt;P&gt;The result is this, an empty column.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="heyyou1_0-1642611938706.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/67515i9E274D6EAFA2BF4A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="heyyou1_0-1642611938706.png" alt="heyyou1_0-1642611938706.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="heyyou1_3-1642612010676.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/67519iF4FA6A87C65001D8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="heyyou1_3-1642612010676.png" alt="heyyou1_3-1642612010676.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="heyyou1_2-1642611995412.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/67517i4BFD6D2464051490/image-size/medium?v=v2&amp;amp;px=400" role="button" title="heyyou1_2-1642611995412.png" alt="heyyou1_2-1642611995412.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="heyyou1_1-1642611973751.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/67516i52BB46FCDE44B475/image-size/medium?v=v2&amp;amp;px=400" role="button" title="heyyou1_1-1642611973751.png" alt="heyyou1_1-1642611973751.png" /&gt;&lt;/span&gt;&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;P&gt;I then use the following code to split the dataset if the person is over 65 years old.&lt;/P&gt;
&lt;PRE&gt;PROC sql;
	create table over_65_filtered as 
	select * FROM have
	WHERE intck('year',birth_date_formatted,TODAY()) &amp;gt;= 65;
QUIT;&lt;/PRE&gt;
&lt;P&gt;However, this creates an empty table.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why is the datetime to date conversion resulting in empty data?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jan 2022 18:01:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Problems-calculating-age-from-a-datetime/m-p/790973#M32562</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-01-19T18:01:21Z</dc:date>
    </item>
    <item>
      <title>Re: Problems calculating age from a datetime</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Problems-calculating-age-from-a-datetime/m-p/790976#M32564</link>
      <description>&lt;P&gt;What happens when you run something like this:&lt;/P&gt;
&lt;PRE&gt;DATA have;
set have;
birth_date_formatted= input(birth_datetime_formatted,date9.);
RUN;&lt;/PRE&gt;
&lt;P&gt;Is that the variable BIRTH_DATETIME_FORMATTED, if it is numeric gets turned into a string of digits. If the value was a datetime like&amp;nbsp; 21SEP96:00:00:00 then the digits would be 1158883200, which in no way resembles a DATE9. format, for use by the input function before the informat is attempted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Seldom does implicit conversion of a numeric value get converted to a string that Input requires for a given numeric value. One example:&lt;/P&gt;
&lt;PRE&gt;data example;
   x=123;
   y= input(x,8.);
run;&lt;/PRE&gt;
&lt;P&gt;Y in this example will be missing. That is because before the informat 8. is used to read the value of X is turned into a string using the BEST12 format and is right justified. So there are nothing but spaces in the first 8 positions to read.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are going to attempt to use INPUT with a numeric variable it is up to you to convert the number into a string you know the content. Example&lt;/P&gt;
&lt;PRE&gt;data example;
   x=123;
   y= input(put(x,best12. -L),8.);
run;&lt;/PRE&gt;
&lt;P&gt;The Put function creates a string, the format controls the appearance and the option -L left justifies the result so the leading spaces don't become an issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is better to use the functions such as DATEPART or MDY or DHMS or HMS to covert numeric date and time values around.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/ta-p/424354" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/ta-p/424354&lt;/A&gt; has a PDF with much information about working with dates and such.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jan 2022 17:58:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Problems-calculating-age-from-a-datetime/m-p/790976#M32564</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-01-19T17:58:20Z</dc:date>
    </item>
    <item>
      <title>Re: Problems calculating age from a datetime</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Problems-calculating-age-from-a-datetime/m-p/790977#M32565</link>
      <description>&lt;P&gt;Maxim 2: Read the Log.&lt;/P&gt;
&lt;P&gt;You will find a NOTE for an uninitialized variable.&lt;/P&gt;
&lt;P&gt;This will be the correct code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
birth_date_formatted = datepart(user_part_birth_date);
format birth_date_formatted yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Use the existing variable as source, the correct function, and apply a date format.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jan 2022 17:59:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Problems-calculating-age-from-a-datetime/m-p/790977#M32565</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-01-19T17:59:23Z</dc:date>
    </item>
  </channel>
</rss>

