<?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 what's wrong with this code  &amp;amp; how to  impute date with end date of month if date part not a valid. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/what-s-wrong-with-this-code-amp-how-to-impute-date-with-end-date/m-p/882190#M348552</link>
    <description>&amp;amp;nbsp;&lt;BR /&gt;proc format;&lt;BR /&gt;value $month&lt;BR /&gt;'JAN' = 'JAN'&lt;BR /&gt;'FEB' = 'FEB'&lt;BR /&gt;'MAR' = 'MAR'&lt;BR /&gt;'APR' = 'APR'&lt;BR /&gt;'MAY' = 'APR'&lt;BR /&gt;'JUN' = 'JUN'&lt;BR /&gt;'JUL' = 'JUL'&lt;BR /&gt;'AUG' = 'AUG'&lt;BR /&gt;'SEP' = 'SEP'&lt;BR /&gt;'OCT' = 'OCT'&lt;BR /&gt;'NOV' = 'NOV'&lt;BR /&gt;'DEC' = 'DEC'&lt;BR /&gt;other = ' '&lt;BR /&gt;;&lt;BR /&gt;RUN;&lt;BR /&gt;&lt;BR /&gt;data raw_datedata;&lt;BR /&gt;input date $10.;&lt;BR /&gt;date_datepart = substr(date,1,2);&lt;BR /&gt;date_monthpart=put(upcase(substr(date,3,3)),$month.);&lt;BR /&gt;date_yearpart=substr(date,6);&lt;BR /&gt;&lt;BR /&gt;put date_datepart= date_monthpart= date_yearpart=;&lt;BR /&gt;cards;&lt;BR /&gt;12jan2012&lt;BR /&gt;ukfeb2022&lt;BR /&gt;13dec2023&lt;BR /&gt;ukunk2012&lt;BR /&gt;15aug1995&lt;BR /&gt;ukjan2016&lt;BR /&gt;ukunk2019&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc print;run;&lt;BR /&gt;&lt;BR /&gt;data dt_impute;&lt;BR /&gt;set raw_datedata;&lt;BR /&gt;&lt;BR /&gt;if anydigit(date_datepart) = 0 then date_datepart = '';&lt;BR /&gt;else date_datepart = date_datepart;&lt;BR /&gt;if date_datepart eq '' and date_monthpart eq '' then date_datepart='01' and date_monthpart = 'DEC';&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc print;run;&lt;BR /&gt;Output of the above code:&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Obs date date_datepart date_monthpart date_yearpart&lt;BR /&gt;1 12jan2012 12 JAN 2012&lt;BR /&gt;2 ukfeb2022 FEB 2022&lt;BR /&gt;3 13dec2023 13 DEC 2023&lt;BR /&gt;4 ukunk2012 0 2012&lt;BR /&gt;5 15aug1995 15 AUG 1995&lt;BR /&gt;6 ukjan2016 JAN 2016&lt;BR /&gt;7 ukunk2019 0 2019&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Can anyone please let me know why for record 4 and 7, why date_datepart and date_monthpart has value 0 and ' ' instead of '01' and 'DEC'?&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;Also, please let me know how to replace with the end of the month date in case of date is not a valid one.&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;Thanks&lt;BR /&gt;&amp;amp;nbsp;</description>
    <pubDate>Fri, 23 Jun 2023 18:01:12 GMT</pubDate>
    <dc:creator>Moksha</dc:creator>
    <dc:date>2023-06-23T18:01:12Z</dc:date>
    <item>
      <title>what's wrong with this code  &amp; how to  impute date with end date of month if date part not a valid.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-s-wrong-with-this-code-amp-how-to-impute-date-with-end-date/m-p/882190#M348552</link>
      <description>&amp;amp;nbsp;&lt;BR /&gt;proc format;&lt;BR /&gt;value $month&lt;BR /&gt;'JAN' = 'JAN'&lt;BR /&gt;'FEB' = 'FEB'&lt;BR /&gt;'MAR' = 'MAR'&lt;BR /&gt;'APR' = 'APR'&lt;BR /&gt;'MAY' = 'APR'&lt;BR /&gt;'JUN' = 'JUN'&lt;BR /&gt;'JUL' = 'JUL'&lt;BR /&gt;'AUG' = 'AUG'&lt;BR /&gt;'SEP' = 'SEP'&lt;BR /&gt;'OCT' = 'OCT'&lt;BR /&gt;'NOV' = 'NOV'&lt;BR /&gt;'DEC' = 'DEC'&lt;BR /&gt;other = ' '&lt;BR /&gt;;&lt;BR /&gt;RUN;&lt;BR /&gt;&lt;BR /&gt;data raw_datedata;&lt;BR /&gt;input date $10.;&lt;BR /&gt;date_datepart = substr(date,1,2);&lt;BR /&gt;date_monthpart=put(upcase(substr(date,3,3)),$month.);&lt;BR /&gt;date_yearpart=substr(date,6);&lt;BR /&gt;&lt;BR /&gt;put date_datepart= date_monthpart= date_yearpart=;&lt;BR /&gt;cards;&lt;BR /&gt;12jan2012&lt;BR /&gt;ukfeb2022&lt;BR /&gt;13dec2023&lt;BR /&gt;ukunk2012&lt;BR /&gt;15aug1995&lt;BR /&gt;ukjan2016&lt;BR /&gt;ukunk2019&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc print;run;&lt;BR /&gt;&lt;BR /&gt;data dt_impute;&lt;BR /&gt;set raw_datedata;&lt;BR /&gt;&lt;BR /&gt;if anydigit(date_datepart) = 0 then date_datepart = '';&lt;BR /&gt;else date_datepart = date_datepart;&lt;BR /&gt;if date_datepart eq '' and date_monthpart eq '' then date_datepart='01' and date_monthpart = 'DEC';&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc print;run;&lt;BR /&gt;Output of the above code:&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Obs date date_datepart date_monthpart date_yearpart&lt;BR /&gt;1 12jan2012 12 JAN 2012&lt;BR /&gt;2 ukfeb2022 FEB 2022&lt;BR /&gt;3 13dec2023 13 DEC 2023&lt;BR /&gt;4 ukunk2012 0 2012&lt;BR /&gt;5 15aug1995 15 AUG 1995&lt;BR /&gt;6 ukjan2016 JAN 2016&lt;BR /&gt;7 ukunk2019 0 2019&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Can anyone please let me know why for record 4 and 7, why date_datepart and date_monthpart has value 0 and ' ' instead of '01' and 'DEC'?&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;Also, please let me know how to replace with the end of the month date in case of date is not a valid one.&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;Thanks&lt;BR /&gt;&amp;amp;nbsp;</description>
      <pubDate>Fri, 23 Jun 2023 18:01:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-s-wrong-with-this-code-amp-how-to-impute-date-with-end-date/m-p/882190#M348552</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-06-23T18:01:12Z</dc:date>
    </item>
    <item>
      <title>Re: what's wrong with this code  &amp; how to  impute date with end date of month if date part not a</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-s-wrong-with-this-code-amp-how-to-impute-date-with-end-date/m-p/882216#M348562</link>
      <description>&lt;P&gt;Incorrect syntax... AND is a logical operator it cannot be used to mean &lt;EM&gt;Do this&lt;/EM&gt; AND &lt;EM&gt;that&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Replace&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;if date_datepart eq '' and date_monthpart eq '' then date_datepart='01' and date_monthpart = 'DEC';&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;with&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;if date_datepart eq '' and date_monthpart eq '' then do;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;date_datepart='01';&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;date_monthpart = 'DEC';&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;end;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Jun 2023 19:44:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-s-wrong-with-this-code-amp-how-to-impute-date-with-end-date/m-p/882216#M348562</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2023-06-23T19:44:53Z</dc:date>
    </item>
    <item>
      <title>Re: what's wrong with this code  &amp; how to  impute date with end date of month if date part not a</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-s-wrong-with-this-code-amp-how-to-impute-date-with-end-date/m-p/882220#M348564</link>
      <description>&lt;P&gt;What specific result date do you want to impute when you only have a YEAR in the value?&lt;/P&gt;
&lt;P&gt;When you only have a month and year?&lt;/P&gt;
&lt;P&gt;Do you have any cases with only day and year values? Like 15unk2023? If so, what month would you want to impute?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is how I would go about creating a SAS date value from that stuff. Using TRANWRD and Strip to remove the UK and UNK bits from the date text value.&lt;/P&gt;
&lt;PRE&gt;data raw_datedata;
input date $10.;
date=strip(tranwrd(date,'uk',''));
date=strip(tranwrd(date,'unk',''));
sasdate  = input(date,?? date9. );
if missing(sasdate) then do;
  /*sas informat monyy will impute the first day of the month*/
  if length(date)=7 then sasdate=input(date,monyy7.);
  /* impute month as 6, day of month as 1 replace as desired*/
  else if length(date)=4 then sasdate=mdy(6,1,input(date,4.));
end;
format sasdate date9.;
cards;
12jan2012
ukfeb2022
13dec2023
ukunk2012
15aug1995
ukjan2016
ukunk2019
;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Jun 2023 20:01:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-s-wrong-with-this-code-amp-how-to-impute-date-with-end-date/m-p/882220#M348564</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-06-23T20:01:51Z</dc:date>
    </item>
    <item>
      <title>Re: what's wrong with this code  &amp; how to  impute date with end date of month if date part not a</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-s-wrong-with-this-code-amp-how-to-impute-date-with-end-date/m-p/882264#M348585</link>
      <description>&lt;P&gt;Thank you for the reply. I have used the code as per your suggestion, it worked.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 24 Jun 2023 12:23:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-s-wrong-with-this-code-amp-how-to-impute-date-with-end-date/m-p/882264#M348585</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-06-24T12:23:43Z</dc:date>
    </item>
    <item>
      <title>Re: what's wrong with this code  &amp; how to  impute date with end date of month if date part not a</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-s-wrong-with-this-code-amp-how-to-impute-date-with-end-date/m-p/882265#M348586</link>
      <description>&lt;P&gt;Thank you for the reply. The imputation rules are:&lt;/P&gt;
&lt;P&gt;If date part is not valid then replace it with last day of the month in the given date.&amp;nbsp; For example, ukfeb2022 has invalid date part 'uk' and month is feb and year 2022 is not a leap year I should get it as 28FEB2022.&lt;/P&gt;
&lt;P&gt;If both date part and month part are not valid then it should be 31DEC for example xyunk2019 has invalid date part xy and invalid month part unk and hence it should be 31DEC2019.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the first part, I have used the following and it is working:&lt;/P&gt;
&lt;P&gt;imputed_date = put(intnx('mon',input(cats('01',date_monthpart,date_yearpart),date9.),0,'e'),$date9.);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 24 Jun 2023 13:09:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-s-wrong-with-this-code-amp-how-to-impute-date-with-end-date/m-p/882265#M348586</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-06-24T13:09:40Z</dc:date>
    </item>
    <item>
      <title>Re: what's wrong with this code  &amp; how to  impute date with end date of month if date part not a</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-s-wrong-with-this-code-amp-how-to-impute-date-with-end-date/m-p/882290#M348593</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/439180"&gt;@Moksha&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you for the reply. The imputation rules are:&lt;/P&gt;
&lt;P&gt;If date part is not valid then replace it with last day of the month in the given date.&amp;nbsp; For example, ukfeb2022 has invalid date part 'uk' and month is feb and year 2022 is not a leap year I should get it as 28FEB2022.&lt;/P&gt;
&lt;P&gt;If both date part and month part are not valid then it should be 31DEC for example xyunk2019 has invalid date part xy and invalid month part unk and hence it should be 31DEC2019.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the first part, I have used the following and it is working:&lt;/P&gt;
&lt;P&gt;imputed_date = put(intnx('mon',input(cats('01',date_monthpart,date_yearpart),date9.),0,'e'),$date9.);&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Strongly suspect the line above throws an invalid format message with the $date9. format. You have, created a date value and $date, a character format, would not be applied by SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using INTNX with my code for the end of the month and Dec 31 for the year only values:&lt;/P&gt;
&lt;PRE&gt;data raw_datedata;
input date $10.;
date=strip(tranwrd(date,'uk',''));
date=strip(tranwrd(date,'unk',''));
sasdate  = input(date,?? date9. );
if missing(sasdate) then do;
  /*sas informat monyy will impute the first day of the month, use INTNX 
    to increment to the end of the mont*/
  if length(date)=7 then sasdate=intnx('month',input(date,monyy7.),0,'E');
  /* impute month as 6, day of month as 1 replace as desired*/
  else if length(date)=4 then sasdate=mdy(12,31,input(date,4.));
end;
format sasdate date9.;
cards;
12jan2012
ukfeb2022
13dec2023
ukunk2012
15aug1995
ukjan2016
ukunk2019
;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 24 Jun 2023 23:47:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-s-wrong-with-this-code-amp-how-to-impute-date-with-end-date/m-p/882290#M348593</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-06-24T23:47:00Z</dc:date>
    </item>
    <item>
      <title>Re: what's wrong with this code  &amp; how to  impute date with end date of month if date part not a</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-s-wrong-with-this-code-amp-how-to-impute-date-with-end-date/m-p/882383#M348618</link>
      <description>&lt;P&gt;You are absolutely right that the code that I have mentioned has given the following warning message:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;WARNING: Character format specified for the result of a numeric expression.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Thank you very much for pointing out and providing the code. However, I want the sasdate to be character but in the code provided by you it's numeric. Can you please advise how to achieve this?&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;I have tried using put function but since sasdate is numeric, getting warning messages for this also.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Once, again thank you very much for your valuable guidance.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jun 2023 05:43:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-s-wrong-with-this-code-amp-how-to-impute-date-with-end-date/m-p/882383#M348618</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-06-26T05:43:50Z</dc:date>
    </item>
    <item>
      <title>Re: what's wrong with this code  &amp; how to  impute date with end date of month if date part not a</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-s-wrong-with-this-code-amp-how-to-impute-date-with-end-date/m-p/882478#M348638</link>
      <description>By removing $, the following worked :
imputed_date = put(intnx('mon',input(cats('01',date_monthpart,date_yearpart),date9.),0,'e'),date9.);

If there are any better ways to do this, please advise.</description>
      <pubDate>Mon, 26 Jun 2023 16:48:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-s-wrong-with-this-code-amp-how-to-impute-date-with-end-date/m-p/882478#M348638</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-06-26T16:48:30Z</dc:date>
    </item>
    <item>
      <title>Re: what's wrong with this code  &amp; how to  impute date with end date of month if date part not a</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-s-wrong-with-this-code-amp-how-to-impute-date-with-end-date/m-p/882490#M348646</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/439180"&gt;@Moksha&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;You are absolutely right that the code that I have mentioned has given the following warning message:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;WARNING: Character format specified for the result of a numeric expression.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Thank you very much for pointing out and providing the code. However, I want the sasdate to be character but in the code provided by you it's numeric. Can you please advise how to achieve this?&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;I have tried using put function but since sasdate is numeric, getting warning messages for this also.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Once, again thank you very much for your valuable guidance.&lt;/FONT&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;PUT the sasdate variable with the format you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you haven't described how this is actually used other than some other program will somehow use the file you still may not need that step as how you create the file could be where the format is actually needed.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jun 2023 17:38:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-s-wrong-with-this-code-amp-how-to-impute-date-with-end-date/m-p/882490#M348646</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-06-26T17:38:21Z</dc:date>
    </item>
    <item>
      <title>Re: what's wrong with this code  &amp; how to  impute date with end date of month if date part not a</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-s-wrong-with-this-code-amp-how-to-impute-date-with-end-date/m-p/882653#M348721</link>
      <description>&lt;P&gt;Sure and once again thank you very much.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jun 2023 14:59:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-s-wrong-with-this-code-amp-how-to-impute-date-with-end-date/m-p/882653#M348721</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-06-27T14:59:45Z</dc:date>
    </item>
  </channel>
</rss>

