<?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: Variables With Values as Partial or Full Dates - How to Select Smallest Date Between Variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Variables-With-Values-as-Partial-or-Full-Dates-How-to-Select/m-p/922312#M363193</link>
    <description>&lt;P&gt;Dates should NEVER be character strings if you are going to work with them. They should always be converted to dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As far as partial dates are concerned, you need to come up with logic on how to handle these, how to turn these into "non-partial" dates. Then once you have such logic, you can turn them into valid SAS dates which contain year/month/day (not year/month) and then program the rest of your operations.&lt;/P&gt;</description>
    <pubDate>Fri, 29 Mar 2024 21:29:09 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2024-03-29T21:29:09Z</dc:date>
    <item>
      <title>Variables With Values as Partial or Full Dates - How to Select Smallest Date Between Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variables-With-Values-as-Partial-or-Full-Dates-How-to-Select/m-p/922308#M363192</link>
      <description>&lt;P&gt;I want to select the smallest value of two variables for each row in a data set.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The&amp;nbsp;two variables have date values as character strings that follow the order of 4 digit year, 2 digit month, 2 digit day.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Some values for these variables have full dates (yyyymmdd) and others have partial dates (yyyymm or yyyy).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I select the smallest value between each of the two variables for each row? Will I need to convert the character strings to dates first? However, won't date formats on partial dates return missing value (".")?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried using the MIN function as Var3 = MIN(Var1, Var2)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;DIV&gt;Var1&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV&gt;Var2&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV&gt;Expected Minimum Date&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV&gt;Resulting Minimum Date Based on Date format&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV&gt;Resulting Minimum Date Based on Character String&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;DIV&gt;202108&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV&gt;20211201&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV&gt;202108&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV&gt;20211201 (because partial date of 202108 returns as "." when converting column to a date. Or is there a way to handle column of dates of varying lengths to prevent partial dates from being ".")&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV&gt;202108&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;DIV&gt;202112&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV&gt;20210824&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV&gt;20210824&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV&gt;20210824&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV&gt;202112&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2024 21:16:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variables-With-Values-as-Partial-or-Full-Dates-How-to-Select/m-p/922308#M363192</guid>
      <dc:creator>melc</dc:creator>
      <dc:date>2024-03-29T21:16:17Z</dc:date>
    </item>
    <item>
      <title>Re: Variables With Values as Partial or Full Dates - How to Select Smallest Date Between Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variables-With-Values-as-Partial-or-Full-Dates-How-to-Select/m-p/922312#M363193</link>
      <description>&lt;P&gt;Dates should NEVER be character strings if you are going to work with them. They should always be converted to dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As far as partial dates are concerned, you need to come up with logic on how to handle these, how to turn these into "non-partial" dates. Then once you have such logic, you can turn them into valid SAS dates which contain year/month/day (not year/month) and then program the rest of your operations.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2024 21:29:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variables-With-Values-as-Partial-or-Full-Dates-How-to-Select/m-p/922312#M363193</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-03-29T21:29:09Z</dc:date>
    </item>
    <item>
      <title>Re: Variables With Values as Partial or Full Dates - How to Select Smallest Date Between Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variables-With-Values-as-Partial-or-Full-Dates-How-to-Select/m-p/922314#M363194</link>
      <description>&lt;P&gt;You can get good mileage using 8601 date format.&amp;nbsp; SAS can read it and set missing day to 01 and same for missing month.&amp;nbsp; But you can do compare with the character variables.&amp;nbsp; Tones of papers and threads about partial dates you should check it out.&amp;nbsp; YYYY-MM-DD works well in listings too.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;49         data _null_;
50            v0='2021';
51            v1='2021-08';
52            v2='2021-12-01';
53            t1=v1 min v2;
54            t2=v1 max v2;
55            t3=v0 eq v1;
56            t4=v0 eq: v2;
57            put 'NOTE: ' (_all_)(/=);
58            run;
2 The SAS System                               15:14 Friday, March 29, 2024

NOTE: 
v0=2021
v1=2021-08
v2=2021-12-01
t1=2021-08
t2=2021-12-01
t3=0
t4=1&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Mar 2024 22:22:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variables-With-Values-as-Partial-or-Full-Dates-How-to-Select/m-p/922314#M363194</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2024-03-29T22:22:10Z</dc:date>
    </item>
    <item>
      <title>Re: Variables With Values as Partial or Full Dates - How to Select Smallest Date Between Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variables-With-Values-as-Partial-or-Full-Dates-How-to-Select/m-p/922315#M363195</link>
      <description>&lt;P&gt;Given that you have partial dates (of the form YYYY, YYYYMM) it's uncharacteristically lucky that you have kept these as character variables.&amp;nbsp; &amp;nbsp;You can find the minimum of two character dates (partial or complete) by using a comparison operator (i.e "&amp;lt;", "&amp;gt;"), which will honor&amp;nbsp;lexicographic ordering (i.e. "alphabetic" ordering).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   mindate=ifc(var1&amp;lt;var2,var1,var2);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now mindate will still be a character variable, which you may need to convert to a regular numeric variable holding date values.&amp;nbsp; But the initial comparison is easy.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;CAVEAT: If var1 or var2 is blank that blank value will be less than all YYYY, YYYYMM, and YYYYMMDD character values.&amp;nbsp; So you may need to protect against blank VAR1 or VAR2 values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2024 22:23:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variables-With-Values-as-Partial-or-Full-Dates-How-to-Select/m-p/922315#M363195</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-03-29T22:23:54Z</dc:date>
    </item>
    <item>
      <title>Re: Variables With Values as Partial or Full Dates - How to Select Smallest Date Between Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variables-With-Values-as-Partial-or-Full-Dates-How-to-Select/m-p/922317#M363197</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/148296"&gt;@melc&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I want to select the smallest value of two variables for each row in a data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The&amp;nbsp;two variables have date values as character strings that follow the order of 4 digit year, 2 digit month, 2 digit day.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Some values for these variables have full dates (yyyymmdd) and others have partial dates (yyyymm or yyyy).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I select the smallest value between each of the two variables for each row? Will I need to convert the character strings to dates first? However, won't date formats on partial dates return missing value (".")?&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;First worry about INFORMATS to read the values. Pick the desired format after you have a date value to display.&lt;/P&gt;
&lt;P&gt;Things that appear as YYMM or YYYYMM can be read into a data value using the YYMMNw. informat. It will assume that the day of the month used for the date is the first day.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you only have a 4-digit year then convert to numeric and use the MDY function to create the day and month in that year that you want to use for comparisons.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The real problem can be if you have mixed 6 character values that are in the YYMMDD and YYYYMM as 201212 might be 12 Dec 2020 (or 1920) or Dec 2012.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2024 22:31:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variables-With-Values-as-Partial-or-Full-Dates-How-to-Select/m-p/922317#M363197</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-03-29T22:31:45Z</dc:date>
    </item>
    <item>
      <title>Re: Variables With Values as Partial or Full Dates - How to Select Smallest Date Between Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variables-With-Values-as-Partial-or-Full-Dates-How-to-Select/m-p/922336#M363210</link>
      <description>&lt;P&gt;Please share your code.&amp;nbsp; Looks like normal comparisons do what you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input (Var1 Var2 Expected) (:$8.);
cards;
202108 20211201 202108
202112 20210824 20210824
;

data want;
  set have;
  if var1&amp;lt;var2 then min=var1; else min=var2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs     Var1       Var2      Expected      min

 1     202108    20211201    202108      202108
 2     202112    20210824    20210824    20210824

&lt;/PRE&gt;</description>
      <pubDate>Sat, 30 Mar 2024 03:22:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variables-With-Values-as-Partial-or-Full-Dates-How-to-Select/m-p/922336#M363210</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-03-30T03:22:25Z</dc:date>
    </item>
    <item>
      <title>Re: Variables With Values as Partial or Full Dates - How to Select Smallest Date Between Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variables-With-Values-as-Partial-or-Full-Dates-How-to-Select/m-p/922339#M363211</link>
      <description>&lt;P&gt;As already suggested you could use the appropriate informat to convert the date strings to SAS Date values (count of days since 1/1/1960 stored in a numerical variable).&lt;/P&gt;
&lt;P&gt;For your date and partial date strings informat&amp;nbsp;b8601da. appears to do the job. Partial dates will get aligned to the beginning of what's available (=beginning of month or beginning of year).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data demo;
  infile datalines truncover dlm=' ';
  input dt_string_1:$8. dt_string_2:$8.;

  sas_dt_1=input(dt_string_1,b8601da.);
  sas_dt_2=input(dt_string_2,b8601da.);
  min_date=min(sas_dt_1,sas_dt_2);
  format sas_dt_1 sas_dt_2 min_date yymmdd10.;
  datalines;
202108 20211201
202112 20210824
2021 20210824
;

proc print data=demo;
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-1711770584049.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/95039i9CDF8501F814D85B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1711770584049.png" alt="Patrick_0-1711770584049.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 30 Mar 2024 03:50:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variables-With-Values-as-Partial-or-Full-Dates-How-to-Select/m-p/922339#M363211</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-03-30T03:50:49Z</dc:date>
    </item>
  </channel>
</rss>

