<?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: List the smallest date and largest date? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/List-the-smallest-date-and-largest-date/m-p/758277#M239398</link>
    <description>&lt;P&gt;Have you tried Proc SQL with a GROUP BY clause?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See example code followed by the results, below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;

format Sites 1. ScreenDate YYMMDD10.;

infile datalines dsd;

input Sites ScreenDate : yymmdd10.;

datalines;
1, 2016-12-01,
1, 2016-12-21,
1, 2018-08-15,
1, 2020-05-29,
2, 2017-12-03,
2, 2015-10-23,
2, 2016-09-14,
3, 2019-12-10,
3, 2004-12-09,
3, 2006-08-20,
3, 2016-12-10,
4, 2000-05-06,
4, 2007-12-06,
4, 2016-09-27,
;

Proc sort data=test; by sites ScreenDate; run;


PROC	SQL;
	SELECT Sites 
		,MIN(ScreenDate)	AS	Min_Screen_Date	FORMAT=YYMMDDD10.
		,MAX(ScreenDate)	AS	Max_Screen_Date	FORMAT=YYMMDDD10.
		FROM	WORK.TEST
		GROUP	BY	Sites
		;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1627593535684.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/62114i4AD6F398A96807F5/image-size/large?v=v2&amp;amp;px=999" role="button" title="jimbarbour_0-1627593535684.png" alt="jimbarbour_0-1627593535684.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 29 Jul 2021 21:19:09 GMT</pubDate>
    <dc:creator>jimbarbour</dc:creator>
    <dc:date>2021-07-29T21:19:09Z</dc:date>
    <item>
      <title>List the smallest date and largest date?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-the-smallest-date-and-largest-date/m-p/758273#M239394</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have dataset Test list below.&amp;nbsp;&amp;nbsp;&amp;nbsp; I would like to get the smallest date and the largest date from the each site.&amp;nbsp; For example, the result for Site 1 should be:&amp;nbsp; Starting Date 2016-12-01;&amp;nbsp; Ending Date 2020--5-29.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Please let me know how to approach it, thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; test;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;format&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; Sites &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;1.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt; ScreenDate &lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;YYMMDD10.&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;infile&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; datalines &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;dsd&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;input&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; Sites ScreenDate : &lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;yymmdd10.&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;datalines&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1, 2016-12-01,&lt;/P&gt;
&lt;P&gt;1, 2016-12-21,&lt;/P&gt;
&lt;P&gt;1, 2018-08-15,&lt;/P&gt;
&lt;P&gt;1, 2020-05-29,&lt;/P&gt;
&lt;P&gt;2, 2017-12-03,&lt;/P&gt;
&lt;P&gt;2, 2015-10-23,&lt;/P&gt;
&lt;P&gt;2, 2016-09-14,&lt;/P&gt;
&lt;P&gt;3, 2019-12-10,&lt;/P&gt;
&lt;P&gt;3, 2004-12-09,&lt;/P&gt;
&lt;P&gt;3, 2006-08-20,&lt;/P&gt;
&lt;P&gt;3, 2016-12-10,&lt;/P&gt;
&lt;P&gt;4, 2000-05-06,&lt;/P&gt;
&lt;P&gt;4, 2007-12-06,&lt;/P&gt;
&lt;P&gt;4, 2016-09-27,&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;Proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;sort&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;data&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;=test; &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; sites ScreenDate; &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;run&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jul 2021 21:08:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-the-smallest-date-and-largest-date/m-p/758273#M239394</guid>
      <dc:creator>ybz12003</dc:creator>
      <dc:date>2021-07-29T21:08:15Z</dc:date>
    </item>
    <item>
      <title>Re: List the smallest date and largest date?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-the-smallest-date-and-largest-date/m-p/758277#M239398</link>
      <description>&lt;P&gt;Have you tried Proc SQL with a GROUP BY clause?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See example code followed by the results, below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;

format Sites 1. ScreenDate YYMMDD10.;

infile datalines dsd;

input Sites ScreenDate : yymmdd10.;

datalines;
1, 2016-12-01,
1, 2016-12-21,
1, 2018-08-15,
1, 2020-05-29,
2, 2017-12-03,
2, 2015-10-23,
2, 2016-09-14,
3, 2019-12-10,
3, 2004-12-09,
3, 2006-08-20,
3, 2016-12-10,
4, 2000-05-06,
4, 2007-12-06,
4, 2016-09-27,
;

Proc sort data=test; by sites ScreenDate; run;


PROC	SQL;
	SELECT Sites 
		,MIN(ScreenDate)	AS	Min_Screen_Date	FORMAT=YYMMDDD10.
		,MAX(ScreenDate)	AS	Max_Screen_Date	FORMAT=YYMMDDD10.
		FROM	WORK.TEST
		GROUP	BY	Sites
		;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1627593535684.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/62114i4AD6F398A96807F5/image-size/large?v=v2&amp;amp;px=999" role="button" title="jimbarbour_0-1627593535684.png" alt="jimbarbour_0-1627593535684.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jul 2021 21:19:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-the-smallest-date-and-largest-date/m-p/758277#M239398</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-07-29T21:19:09Z</dc:date>
    </item>
    <item>
      <title>Re: List the smallest date and largest date?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-the-smallest-date-and-largest-date/m-p/758279#M239399</link>
      <description>&lt;P&gt;Do you need a data set or report for people to read?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data set:&lt;/P&gt;
&lt;PRE&gt;Proc summary data=test nway;
   class site;
   var screendate;
   output out=want(drop=_type_ _freq_) min= max= /autoname;
run;&lt;/PRE&gt;
&lt;P&gt;The autoname creates variables screendate_min and screendate_max. You would want to attach a format to the variables to be read by people easily. Or you could use min=StartDate max=EndDate to set those names for the result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jul 2021 21:19:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-the-smallest-date-and-largest-date/m-p/758279#M239399</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-07-29T21:19:09Z</dc:date>
    </item>
    <item>
      <title>Re: List the smallest date and largest date?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-the-smallest-date-and-largest-date/m-p/758282#M239401</link>
      <description>&lt;P&gt;There are quite a few ways. Here's how I'd tackle it in either DATA step form or PROC SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means 
	data = test noprint;
		class sites;
		var screendate;
		ways 1;
			output out = want (drop = _:)
				min =
				max = / autoname;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs 	Sites 	ScreenDate_Min 	ScreenDate_Max
1 	1 	2016-12-01 	2020-05-29
2 	2 	2015-10-23 	2017-12-03
3 	3 	2004-12-09 	2019-12-10
4 	4 	2000-05-06 	2016-09-27&lt;/PRE&gt;
&lt;P&gt;PROC SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table 	want_sql as
		select
					sites,
					min(screendate) as min_screendate format = yymmdd10.,
					max(screendate) as max_screendate format = yymmdd10.
		from
					test
		group by
					sites;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs 	Sites 	min_screendate 	max_screendate
1 	1 	2016-12-01 	2020-05-29
2 	2 	2015-10-23 	2017-12-03
3 	3 	2004-12-09 	2019-12-10
4 	4 	2000-05-06 	2016-09-27&lt;/PRE&gt;</description>
      <pubDate>Thu, 29 Jul 2021 21:25:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-the-smallest-date-and-largest-date/m-p/758282#M239401</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2021-07-29T21:25:56Z</dc:date>
    </item>
    <item>
      <title>Re: List the smallest date and largest date?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-the-smallest-date-and-largest-date/m-p/758285#M239404</link>
      <description>&lt;P&gt;Thank you so much for the prompt reply.&amp;nbsp;&amp;nbsp; I got it work!&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jul 2021 21:29:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-the-smallest-date-and-largest-date/m-p/758285#M239404</guid>
      <dc:creator>ybz12003</dc:creator>
      <dc:date>2021-07-29T21:29:07Z</dc:date>
    </item>
  </channel>
</rss>

