<?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: Find the min and max date values across multiple date variables using Array in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Find-the-min-and-max-date-values-across-multiple-date-variables/m-p/916269#M83318</link>
    <description>&lt;P&gt;Thanks! On the off chance, do you know if there is a way to use datepart() in either the proc summary/means or transpose?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Typically I have a couple of variables which contain datetime and wanted to save another processing step if I could. (I tried setting additional format lines for the impacted fields to the proc transpose but that just overrides everything.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Appreciate the support!&lt;/P&gt;</description>
    <pubDate>Thu, 15 Feb 2024 09:18:01 GMT</pubDate>
    <dc:creator>sasheadache</dc:creator>
    <dc:date>2024-02-15T09:18:01Z</dc:date>
    <item>
      <title>Find the min and max date values across multiple date variables using Array</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Find-the-min-and-max-date-values-across-multiple-date-variables/m-p/916053#M83311</link>
      <description>&lt;P&gt;Hi All, I have a dataset which contains a list of date variables (in columns) and I need to return the min() and max() values for each one.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have used the below code to get all date variables from my source table, to then pass them into a macro variable to ensure the field list stays dynamic.&lt;/P&gt;&lt;PRE&gt;&lt;FONT size="2"&gt;/* OBTAIN FIELD LIST */&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;proc contents data=test out=field_list; run;&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;/* PASS DATE FORMATTED COLUMNS INTO MACRO TOKEN */&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;PROC SQL;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;SELECT NAME INTO:DATE_FIELDS separated by ' ' FROM field_list WHERE FORMAT = "DATE"; &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;QUIT;&lt;/FONT&gt;&lt;/PRE&gt;&lt;P&gt;I was then hoping to use the &amp;amp;field_list in an array find the min and max value for each, can anyone assist please?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;Desired Array Output:&lt;/FONT&gt;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;MIN&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;MAX&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="2"&gt;Var1&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;01-Jan-00&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;30-Jan-24&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="2"&gt;Var2&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;etc.&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;etc.&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="2"&gt;Var3&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;etc.&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;etc.&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="2"&gt;Var4&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;etc.&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;etc.&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="2"&gt;Var5&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;etc.&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;etc.&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Wed, 14 Feb 2024 13:34:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Find-the-min-and-max-date-values-across-multiple-date-variables/m-p/916053#M83311</guid>
      <dc:creator>sasheadache</dc:creator>
      <dc:date>2024-02-14T13:34:21Z</dc:date>
    </item>
    <item>
      <title>Re: Find the min and max date values across multiple date variables using Array</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Find-the-min-and-max-date-values-across-multiple-date-variables/m-p/916055#M83312</link>
      <description>&lt;P&gt;Isn't that what PROC MEANS is for?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means min max data=test;
  var &amp;amp;date_fields;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS you can get a better list of variables if you use FMTINFO() function.&amp;nbsp; Then you can find the variables that are using YYMMDD and other date type formats.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;SELECT NAME
  INTO :DATE_FIELDS separated by ' ' 
  FROM field_list 
  WHERE 'date' = fmtinfo(format,'cat')
  order by varnum
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Feb 2024 13:48:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Find-the-min-and-max-date-values-across-multiple-date-variables/m-p/916055#M83312</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-02-14T13:48:03Z</dc:date>
    </item>
    <item>
      <title>Re: Find the min and max date values across multiple date variables using Array</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Find-the-min-and-max-date-values-across-multiple-date-variables/m-p/916056#M83313</link>
      <description>&lt;P&gt;Query the DICTIONARY table, and run PROC MEANS.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select name into :date_fields separated by " "
from dictionary.columns
where libname = "WORK" and memname = "TEST" and format like 'DATE%',
quit;

proc means data=test min max;
var &amp;amp;date_fields.;
output out=want / autoname;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Feb 2024 13:45:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Find-the-min-and-max-date-values-across-multiple-date-variables/m-p/916056#M83313</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-02-14T13:45:25Z</dc:date>
    </item>
    <item>
      <title>Re: Find the min and max date values across multiple date variables using Array</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Find-the-min-and-max-date-values-across-multiple-date-variables/m-p/916061#M83314</link>
      <description>&lt;P&gt;The 'Results' is exactly the view I wanted to see (I just need to re-format the mix/max columns to show as a date).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The 'Output data' however has transposed the data in the opposite way i.e. variables are in columns and min max in rows. I ideally wanted output table to match the 'Results' output.... do you know if there is a simple way to force the output to show the same view? Or will I need to do some further steps to recreate this view i.e.&lt;/P&gt;&lt;P&gt;1. drop the _TYPE_ _FREQ_ columns&lt;/P&gt;&lt;P&gt;2. retain where _STAT_&amp;nbsp; &amp;nbsp;in ('MIN ' MAX')&lt;/P&gt;&lt;P&gt;3. transpose the data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 14 Feb 2024 14:14:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Find-the-min-and-max-date-values-across-multiple-date-variables/m-p/916061#M83314</guid>
      <dc:creator>sasheadache</dc:creator>
      <dc:date>2024-02-14T14:14:36Z</dc:date>
    </item>
    <item>
      <title>Re: Find the min and max date values across multiple date variables using Array</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Find-the-min-and-max-date-values-across-multiple-date-variables/m-p/916071#M83315</link>
      <description>Look into the STACKODSOUTPUT option to get results more similar to the displayed output.</description>
      <pubDate>Wed, 14 Feb 2024 15:40:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Find-the-min-and-max-date-values-across-multiple-date-variables/m-p/916071#M83315</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2024-02-14T15:40:01Z</dc:date>
    </item>
    <item>
      <title>Re: Find the min and max date values across multiple date variables using Array</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Find-the-min-and-max-date-values-across-multiple-date-variables/m-p/916076#M83316</link>
      <description>&lt;P&gt;Just transpose the normal output of PROC MEANS/SUMMARY.&lt;/P&gt;
&lt;P&gt;Add a FORMAT statement to attach whatever type of date display you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=test;
  var &amp;amp;date_fields;
  output out=stats;
run;
proc transpose data=stats out=want;
  where _stat_ in ('MIN' 'MAX');
  id _stat_;
  var &amp;amp;date_fields;
  format &amp;amp;date_fields yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Feb 2024 16:11:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Find-the-min-and-max-date-values-across-multiple-date-variables/m-p/916076#M83316</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-02-14T16:11:24Z</dc:date>
    </item>
    <item>
      <title>Re: Find the min and max date values across multiple date variables using Array</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Find-the-min-and-max-date-values-across-multiple-date-variables/m-p/916269#M83318</link>
      <description>&lt;P&gt;Thanks! On the off chance, do you know if there is a way to use datepart() in either the proc summary/means or transpose?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Typically I have a couple of variables which contain datetime and wanted to save another processing step if I could. (I tried setting additional format lines for the impacted fields to the proc transpose but that just overrides everything.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Appreciate the support!&lt;/P&gt;</description>
      <pubDate>Thu, 15 Feb 2024 09:18:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Find-the-min-and-max-date-values-across-multiple-date-variables/m-p/916269#M83318</guid>
      <dc:creator>sasheadache</dc:creator>
      <dc:date>2024-02-15T09:18:01Z</dc:date>
    </item>
    <item>
      <title>Re: Find the min and max date values across multiple date variables using Array</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Find-the-min-and-max-date-values-across-multiple-date-variables/m-p/916298#M83321</link>
      <description>&lt;P&gt;If you are willing to accept the normal standard output of PROC SUMMARY then you won't have any trouble. Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  now=datetime();
  today=date();
  format now datetime19. today date9.;
run;

proc summary data=test;
  var now today;
  output out=stats;
run;

proc print data=stats;
  where _stat_ in ('MIN','MAX');
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1708005423594.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/93781i417B83EEA5E158C8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1708005423594.png" alt="Tom_0-1708005423594.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;But if you transpose them you will need to add a step somewhere to convert them.&amp;nbsp; Perhaps by converting the DATETIME values into DATE values?&amp;nbsp; It might be faster to do it after creating the transposed dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=stats out=want;
  where _stat_ in ('MIN','MAX');
  id _stat_;
  var now today;
run;

data want;
  set want;
  if _name_ in ('now') then do;
   min=datepart(min);
   max=datepart(max);
  end;
  format min max date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_1-1708005771468.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/93783i5D822E5BB2100557/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_1-1708005771468.png" alt="Tom_1-1708005771468.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Feb 2024 14:03:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Find-the-min-and-max-date-values-across-multiple-date-variables/m-p/916298#M83321</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-02-15T14:03:03Z</dc:date>
    </item>
    <item>
      <title>Re: Find the min and max date values across multiple date variables using Array</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Find-the-min-and-max-date-values-across-multiple-date-variables/m-p/916299#M83322</link>
      <description>&lt;P&gt;You can get the list of both date and datetime variables.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
SELECT case when 'date' = fmtinfo(format,'cat') then name else ' ' end
     , case when 'datetime' = fmtinfo(format,'cat') then name else ' ' end
  INTO :DATE_FIELDS separated by ' ' 
     , :DATETIME_FIELDS separated by ' '
  FROM field_list 
  WHERE fmtinfo(format,'cat') in ('date','datetime')
  order by varnum
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then use both lists in the PROC SUMMARY step and only the DATETIME list in the DATEPART() step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary ....
  var &amp;amp;date_fields &amp;amp;datetime_fields;
...

...
  if indexw("&amp;amp;datetime_fields",trim(_name_),' ') then do;
....&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Feb 2024 14:14:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Find-the-min-and-max-date-values-across-multiple-date-variables/m-p/916299#M83322</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-02-15T14:14:45Z</dc:date>
    </item>
    <item>
      <title>Re: Find the min and max date values across multiple date variables using Array</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Find-the-min-and-max-date-values-across-multiple-date-variables/m-p/916411#M83325</link>
      <description>Thanks Tom! Really appreciate your time</description>
      <pubDate>Fri, 16 Feb 2024 07:39:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Find-the-min-and-max-date-values-across-multiple-date-variables/m-p/916411#M83325</guid>
      <dc:creator>sasheadache</dc:creator>
      <dc:date>2024-02-16T07:39:10Z</dc:date>
    </item>
  </channel>
</rss>

