<?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 Conflicting date formats when coalescing two variables to a new one in PROC SQL in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Conflicting-date-formats-when-coalescing-two-variables-to-a-new/m-p/775772#M81125</link>
    <description>&lt;P&gt;Hi, i've been puzzled by this problem for some time already and half gave up on figuring it out.&lt;/P&gt;&lt;P&gt;Let's say our dataset includes two date variables A and B, and one is missing when other is not. A is in format ddmmmyyy, B is a numeric 4-digit year. For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;|&amp;nbsp; &amp;nbsp; &amp;nbsp; B&lt;/P&gt;&lt;P&gt;01jan1990&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2003&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1996&lt;/P&gt;&lt;P&gt;08feb2017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I coalesce them and assign to another variable C resulting values are in datetime format, similar to following(values are random, purely for illustration):&lt;/P&gt;&lt;P&gt;C&lt;/P&gt;&lt;P&gt;14735&lt;/P&gt;&lt;P&gt;1992&lt;/P&gt;&lt;P&gt;2003&lt;/P&gt;&lt;P&gt;17631&lt;/P&gt;&lt;P&gt;I want to leave year values as they are, but put 5-digit ones to iso8601 ([dd-mm-yyyy]).&lt;/P&gt;&lt;P&gt;Hoping for community's help.&lt;/P&gt;</description>
    <pubDate>Thu, 21 Oct 2021 21:38:27 GMT</pubDate>
    <dc:creator>SSimonov</dc:creator>
    <dc:date>2021-10-21T21:38:27Z</dc:date>
    <item>
      <title>Conflicting date formats when coalescing two variables to a new one in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conflicting-date-formats-when-coalescing-two-variables-to-a-new/m-p/775772#M81125</link>
      <description>&lt;P&gt;Hi, i've been puzzled by this problem for some time already and half gave up on figuring it out.&lt;/P&gt;&lt;P&gt;Let's say our dataset includes two date variables A and B, and one is missing when other is not. A is in format ddmmmyyy, B is a numeric 4-digit year. For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;|&amp;nbsp; &amp;nbsp; &amp;nbsp; B&lt;/P&gt;&lt;P&gt;01jan1990&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2003&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1996&lt;/P&gt;&lt;P&gt;08feb2017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I coalesce them and assign to another variable C resulting values are in datetime format, similar to following(values are random, purely for illustration):&lt;/P&gt;&lt;P&gt;C&lt;/P&gt;&lt;P&gt;14735&lt;/P&gt;&lt;P&gt;1992&lt;/P&gt;&lt;P&gt;2003&lt;/P&gt;&lt;P&gt;17631&lt;/P&gt;&lt;P&gt;I want to leave year values as they are, but put 5-digit ones to iso8601 ([dd-mm-yyyy]).&lt;/P&gt;&lt;P&gt;Hoping for community's help.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Oct 2021 21:38:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conflicting-date-formats-when-coalescing-two-variables-to-a-new/m-p/775772#M81125</guid>
      <dc:creator>SSimonov</dc:creator>
      <dc:date>2021-10-21T21:38:27Z</dc:date>
    </item>
    <item>
      <title>Re: Conflicting date formats when coalescing two variables to a new one in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conflicting-date-formats-when-coalescing-two-variables-to-a-new/m-p/775997#M81139</link>
      <description>&lt;P&gt;A SAS Date value is a numerical value that is the count of days since 1/1/1960. You then can apply a SAS Date format to make such a count human readable when printing the value.&lt;/P&gt;
&lt;P&gt;To create a variable that stores a mix of SAS Date values and year numbers doesn't feel like a good idea and will make working with such a variable just hard (i.e. when trying to sort the table by year).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe it would be much better to convert the Year values to SAS Date values for a date on January 1. Function mdy() allows you to do this easily.&lt;/P&gt;
&lt;P&gt;iso8601 has year first. I've used another SAS format that creates the dd-mm-yyyy pattern you've asked for ...but you could simply use another format if you want to change this (this is just how SAS prints the values).&lt;/P&gt;
&lt;P&gt;Below some code that hopefully explains things to you. The last variable Mixed_Values_Formatted gives you what you've asked for but I wouldn't recommend to go down this route.&amp;nbsp; I'd go for&amp;nbsp;SAS_Date_Value_Formatted.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover dsd;
  input a :date9. b;
  format a date9.;
  datalines;
01jan1990,.
.,2003
.,1996
08feb2017,.
; 


proc format;
  value mixed_vals
    low-2099 = [32.]
    other    = [ddmmyyD10.]
    ;
run;
    

data want;
  set have;
  
  SAS_Date_Value=coalesce(a,mdy(1,1,b));

  format SAS_Date_Value_Formatted ddmmyyD10.;
  SAS_Date_Value_Formatted=SAS_Date_Value;

  Year_Number=coalesce(year(a),b);

  Mixed_Values=coalesce(a,b);
  format Mixed_Values_Formatted mixed_vals.;
  Mixed_Values_Formatted=Mixed_Values;

run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1634958719092.png" style="width: 925px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/64977iA75A5990B8BE8AC3/image-dimensions/925x155?v=v2" width="925" height="155" role="button" title="Patrick_0-1634958719092.png" alt="Patrick_0-1634958719092.png" /&gt;&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, 23 Oct 2021 03:12:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conflicting-date-formats-when-coalescing-two-variables-to-a-new/m-p/775997#M81139</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-10-23T03:12:35Z</dc:date>
    </item>
    <item>
      <title>Re: Conflicting date formats when coalescing two variables to a new one in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conflicting-date-formats-when-coalescing-two-variables-to-a-new/m-p/776015#M81140</link>
      <description>&lt;P&gt;Hi, Patrick! The solution I eventually came to is similar to yours in a lot of ways. Formatting year to the SAS Date format first is something I had in mind too, but it seems I approached it in a wrong way. Very helpful insight, thank you.&lt;/P&gt;</description>
      <pubDate>Sat, 23 Oct 2021 08:29:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conflicting-date-formats-when-coalescing-two-variables-to-a-new/m-p/776015#M81140</guid>
      <dc:creator>SSimonov</dc:creator>
      <dc:date>2021-10-23T08:29:44Z</dc:date>
    </item>
  </channel>
</rss>

