<?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: Character Date with Missing Values to SAS Date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Character-Date-with-Missing-Values-to-SAS-Date/m-p/494650#M130389</link>
    <description>&lt;P&gt;Lots of flexibility with regular expressions:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input ID DateStr $;
datalines;
1          20000314
2          20120523
3          19981299
4          19930118
5          20090899
6          19990799
7          19999907
;

data want;
set test;
dateStr = prxchange("s/(\d{6})99/\1\Q15/",1,dateStr);
dateStr = prxchange("s/(\d{4})99\d\d/\1\Q0701/",1,dateStr);
date = input(dateStr, yymmdd8.);
format date yymmdd10.;
run;

proc print; run;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 11 Sep 2018 21:44:18 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2018-09-11T21:44:18Z</dc:date>
    <item>
      <title>Character Date with Missing Values to SAS Date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Character-Date-with-Missing-Values-to-SAS-Date/m-p/494622#M130375</link>
      <description>&lt;P&gt;Hi all! I know there's a thread on working with incomplete character dates but my situation is slightly different.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a date of diagnosis variable formatted as YYYYMMDD, and it's a character variable (length=8). I would like to change it to a SAS date variable; however some dates contain missing values. For example, the DD portion will be coded as "99" if the day of diagnosis is unknown. So my data look like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;ID&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;Date of Diagnosis&lt;/U&gt;&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20000314&lt;BR /&gt;2&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;20120523&lt;BR /&gt;3&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;199812&lt;STRONG&gt;99&lt;/STRONG&gt;&lt;BR /&gt;4&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;19930118&lt;BR /&gt;5&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;200908&lt;STRONG&gt;99&lt;/STRONG&gt;&lt;BR /&gt;6&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;199907&lt;STRONG&gt;99&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="1 2 3 4 5 6 7"&gt;*fictitious date values for exemplary purposes; my data contain over thousands of people*&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This date conversion code would return the&amp;nbsp;dates 3, 5 and 6 as missing&amp;nbsp;(".")&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;date_new=input(date_old, yymmdd8.);
format date_new yymmdd8.;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way I can tell SAS to only read the first 6 characters of the variable (YYYYMM portion) and convert it to a SAS date formatted as MONYY. or YYMON.?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Alternatively, before converting, is there a way I can tell SAS to replace any YYYYMM99 dates for the day of diagnosis variable with YYYYMM15 (set to the middle of that month)? I am asking this second question because there will be scenarios where MM(month) is 99 too (i.e. 20059914) and if I can learn how to deal with the DD(day) portion then I can apply it to missing month dates.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using SAS Enterprise 7.1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your help!&lt;/P&gt;</description>
      <pubDate>Tue, 11 Sep 2018 20:04:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Character-Date-with-Missing-Values-to-SAS-Date/m-p/494622#M130375</guid>
      <dc:creator>TL93</dc:creator>
      <dc:date>2018-09-11T20:04:17Z</dc:date>
    </item>
    <item>
      <title>Re: Character Date with Missing Values to SAS Date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Character-Date-with-Missing-Values-to-SAS-Date/m-p/494630#M130380</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if substr(date_old,7,2) = '99' then substr(date_old,7,2) = '15';
date_new = input(date_old,yymmdd8.);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Sep 2018 20:31:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Character-Date-with-Missing-Values-to-SAS-Date/m-p/494630#M130380</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-09-11T20:31:01Z</dc:date>
    </item>
    <item>
      <title>Re: Character Date with Missing Values to SAS Date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Character-Date-with-Missing-Values-to-SAS-Date/m-p/494650#M130389</link>
      <description>&lt;P&gt;Lots of flexibility with regular expressions:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input ID DateStr $;
datalines;
1          20000314
2          20120523
3          19981299
4          19930118
5          20090899
6          19990799
7          19999907
;

data want;
set test;
dateStr = prxchange("s/(\d{6})99/\1\Q15/",1,dateStr);
dateStr = prxchange("s/(\d{4})99\d\d/\1\Q0701/",1,dateStr);
date = input(dateStr, yymmdd8.);
format date yymmdd10.;
run;

proc print; run;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Sep 2018 21:44:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Character-Date-with-Missing-Values-to-SAS-Date/m-p/494650#M130389</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-09-11T21:44:18Z</dc:date>
    </item>
    <item>
      <title>Re: Character Date with Missing Values to SAS Date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Character-Date-with-Missing-Values-to-SAS-Date/m-p/494740#M130440</link>
      <description>&lt;P&gt;You can't set a format conditionally, or to put it another way: you cannot show different formats for the same variable in different rows.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can see two solutions. You can either repair the date, and set the day to 15 if it is out of range:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data want;
  set have;
  date_new=input(date_old,?? yymmdd8.); /* ?? Means do not raise a log error */
  if missing(date_new) then do;
    date_new=input(substr(date_old,1,6)!!'15',yymmdd8.);
    date_corrected=1; 
    end;
  else
    date_corrected=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Or you can have two variables, one that shows the date (when valid), and one that shows the month (which is presumably always valid):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  date_new=input(date_old,?? yymmdd8.);
  month_date=input(date_old,yymmn6.);
  format month_date yymon7.;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The month_date will then always be the first of the month, if date_new is missing it means that the day was invalid.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Sep 2018 08:00:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Character-Date-with-Missing-Values-to-SAS-Date/m-p/494740#M130440</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-09-12T08:00:00Z</dc:date>
    </item>
    <item>
      <title>Re: Character Date with Missing Values to SAS Date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Character-Date-with-Missing-Values-to-SAS-Date/m-p/585617#M167040</link>
      <description>&lt;P&gt;Can I ask how you would write the syntax if there are multiple date variables please?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;S&lt;/P&gt;</description>
      <pubDate>Mon, 02 Sep 2019 14:11:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Character-Date-with-Missing-Values-to-SAS-Date/m-p/585617#M167040</guid>
      <dc:creator>sks521</dc:creator>
      <dc:date>2019-09-02T14:11:21Z</dc:date>
    </item>
    <item>
      <title>Re: Character Date with Missing Values to SAS Date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Character-Date-with-Missing-Values-to-SAS-Date/m-p/585661#M167071</link>
      <description>&lt;P&gt;Just repeat the 4 lines of code for every variable. Or use arrays and a do loop if that's more convenient.&lt;/P&gt;</description>
      <pubDate>Mon, 02 Sep 2019 20:58:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Character-Date-with-Missing-Values-to-SAS-Date/m-p/585661#M167071</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-09-02T20:58:20Z</dc:date>
    </item>
  </channel>
</rss>

