<?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: Max/Min date function on a variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Max-Min-date-function-on-a-variable/m-p/15449#M2056</link>
    <description>&lt;P&gt;&lt;EM&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Editor's Note:&amp;nbsp; The solution provided by buckeye will allow you to total up SALES while capturing the earliest DATE.&amp;nbsp; Because a SAS date is a numeric value, the MIN function can be used to capture the earliest date.&amp;nbsp; The DATA step version of buckeye's PROC SQL code is show below as well.&amp;nbsp; Please note that in order for the DATA step version to work, the data set will have to be sorted by ID and DATE.&lt;/P&gt;
&lt;P&gt;&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;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
 PROC SQL;
 CREATE TABLE mySum AS
 SELECT ID,MIN(date) AS Earliest, SUM(sales) AS SalesTot FROM salesrecord GROUP BY ID;
 QUIT;



/*DATA Step version of the above code*/

data a; 
input id date : date9. sales; 
datalines; 
1 01Jan11 12
1 02Jan11 20
2 12Jan11 34
2 23Feb11 21
;

data b; 
set a; 
by id; 
retain earliest; 
if first.id then do; 
earliest=date;
 sales_sum=0; 
end; 
sales_sum+sales;
if last.id then output; 
drop sales date; 
run; 

proc print; 
format earliest date9.; 
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 09 Nov 2016 20:37:49 GMT</pubDate>
    <dc:creator>buckeye</dc:creator>
    <dc:date>2016-11-09T20:37:49Z</dc:date>
    <item>
      <title>Max/Min date function on a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-Min-date-function-on-a-variable/m-p/15444#M2051</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
I have a transactional dataset that I want to roll-up to the contact level.&lt;BR /&gt;
example:&lt;BR /&gt;
id date sales&lt;BR /&gt;
1 01Jan11 12&lt;BR /&gt;
1 02Jan11 20&lt;BR /&gt;
2 12Jan11 34&lt;BR /&gt;
2 23Feb11 21&lt;BR /&gt;
etc..&lt;BR /&gt;
&lt;BR /&gt;
the rolled up set would look like&lt;BR /&gt;
id earliestdate sales_sum&lt;BR /&gt;
1 01Jan11 32&lt;BR /&gt;
2 12Jan11 55&lt;BR /&gt;
etc.&lt;BR /&gt;
&lt;BR /&gt;
the trouble I am having is outputing the 'earliestdate' as the end of my datastep is if last.id then output ;&lt;BR /&gt;
&lt;BR /&gt;
Thanks for your help.</description>
      <pubDate>Thu, 16 Jun 2011 13:56:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-Min-date-function-on-a-variable/m-p/15444#M2051</guid>
      <dc:creator>Danglytics</dc:creator>
      <dc:date>2011-06-16T13:56:23Z</dc:date>
    </item>
    <item>
      <title>Re: Max/Min date function on a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-Min-date-function-on-a-variable/m-p/15445#M2052</link>
      <description>Share what SAS code you have created thus far to solve the problem/objective.  You should be using a RETAIN statement to capture / retain a "temporary date variable" and then on LAST.&lt;BREAK_VARNAME&gt; you will want to output.&lt;BR /&gt;
&lt;BR /&gt;
Otherwise why not use PROC SUMMARY/MEANS with the MIN function instead?&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;/BREAK_VARNAME&gt;</description>
      <pubDate>Thu, 16 Jun 2011 13:58:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-Min-date-function-on-a-variable/m-p/15445#M2052</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2011-06-16T13:58:24Z</dc:date>
    </item>
    <item>
      <title>Re: Max/Min date function on a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-Min-date-function-on-a-variable/m-p/15446#M2053</link>
      <description>This is an overly simplified example of the actual data i'm using, and for that reason theproc summary / means wouldnt work, so i was hoping to incorporate some sort code to my data step.&lt;BR /&gt;
&lt;BR /&gt;
but in this case i would do&lt;BR /&gt;
&lt;BR /&gt;
data rollup ; &lt;BR /&gt;
set transactions ;&lt;BR /&gt;
by id ;&lt;BR /&gt;
if first.id then do sales_sum=. ;&lt;BR /&gt;
sales_sum +  sales ; &lt;BR /&gt;
if last.id then output ; &lt;BR /&gt;
run ;&lt;BR /&gt;
&lt;BR /&gt;
i havent been able to figure out capturing the earliest transaction date part of the code yet.</description>
      <pubDate>Thu, 16 Jun 2011 14:14:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-Min-date-function-on-a-variable/m-p/15446#M2053</guid>
      <dc:creator>Danglytics</dc:creator>
      <dc:date>2011-06-16T14:14:37Z</dc:date>
    </item>
    <item>
      <title>Re: Max/Min date function on a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-Min-date-function-on-a-variable/m-p/15447#M2054</link>
      <description>I think ive figured it out.&lt;BR /&gt;
&lt;BR /&gt;
data rollup ; &lt;BR /&gt;
set transactions ;&lt;BR /&gt;
by id ;&lt;BR /&gt;
retain earliestdate ;&lt;BR /&gt;
if first.id then do;&lt;BR /&gt;
 sales_sum=. ;&lt;BR /&gt;
 earliestdate= '01jan3000'd *set date to sometime way in the future, i know probably not the best practice.. ;&lt;BR /&gt;
end;&lt;BR /&gt;
earliestdate = min(earliestdate,date) ;&lt;BR /&gt;
sales_sum + sales ; &lt;BR /&gt;
if last.id then output ; &lt;BR /&gt;
run ;</description>
      <pubDate>Thu, 16 Jun 2011 14:31:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-Min-date-function-on-a-variable/m-p/15447#M2054</guid>
      <dc:creator>Danglytics</dc:creator>
      <dc:date>2011-06-16T14:31:26Z</dc:date>
    </item>
    <item>
      <title>Re: Max/Min date function on a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-Min-date-function-on-a-variable/m-p/15448#M2055</link>
      <description>Slight coding simplification, if you have FIRST.ID, then you can assign directly the DATE and SALES variables to capture it without using MIN function. Also eliminates need to set high values and/or MISSING value.&lt;BR /&gt;
&lt;BR /&gt;
Honestly, you can accomplish this processing with PROC SUMMARY/MEANS -- have a look at the DOC.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Thu, 16 Jun 2011 14:39:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-Min-date-function-on-a-variable/m-p/15448#M2055</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2011-06-16T14:39:43Z</dc:date>
    </item>
    <item>
      <title>Re: Max/Min date function on a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-Min-date-function-on-a-variable/m-p/15449#M2056</link>
      <description>&lt;P&gt;&lt;EM&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Editor's Note:&amp;nbsp; The solution provided by buckeye will allow you to total up SALES while capturing the earliest DATE.&amp;nbsp; Because a SAS date is a numeric value, the MIN function can be used to capture the earliest date.&amp;nbsp; The DATA step version of buckeye's PROC SQL code is show below as well.&amp;nbsp; Please note that in order for the DATA step version to work, the data set will have to be sorted by ID and DATE.&lt;/P&gt;
&lt;P&gt;&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;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
 PROC SQL;
 CREATE TABLE mySum AS
 SELECT ID,MIN(date) AS Earliest, SUM(sales) AS SalesTot FROM salesrecord GROUP BY ID;
 QUIT;



/*DATA Step version of the above code*/

data a; 
input id date : date9. sales; 
datalines; 
1 01Jan11 12
1 02Jan11 20
2 12Jan11 34
2 23Feb11 21
;

data b; 
set a; 
by id; 
retain earliest; 
if first.id then do; 
earliest=date;
 sales_sum=0; 
end; 
sales_sum+sales;
if last.id then output; 
drop sales date; 
run; 

proc print; 
format earliest date9.; 
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2016 20:37:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-Min-date-function-on-a-variable/m-p/15449#M2056</guid>
      <dc:creator>buckeye</dc:creator>
      <dc:date>2016-11-09T20:37:49Z</dc:date>
    </item>
    <item>
      <title>Re: Max/Min date function on a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-Min-date-function-on-a-variable/m-p/15450#M2057</link>
      <description>Danglytics &lt;BR /&gt;
like the approach : starting with the "earliest" at a "high value"&lt;BR /&gt;
Going a little further lets the SAS system indicate the missing with ****&lt;BR /&gt;
The date constants allow for a year like 20000 (count those zeroes)&lt;BR /&gt;
 This SASlog indicates a change when the year grows larger than 20000[pre]19         data ;&lt;BR /&gt;
20         retain earliest '31dec20000'd ;&lt;BR /&gt;
21             do date= earliest to earliest +1  ;&lt;BR /&gt;
22               put date date11. ;&lt;BR /&gt;
23               put date date9. ;&lt;BR /&gt;
24               put date ddmmyy10. ;&lt;BR /&gt;
25            end ;&lt;BR /&gt;
26         stop ;&lt;BR /&gt;
27         run;&lt;BR /&gt;
&lt;BR /&gt;
31-DEC-****&lt;BR /&gt;
31DEC****&lt;BR /&gt;
31/12/****&lt;BR /&gt;
***********&lt;BR /&gt;
*********&lt;BR /&gt;
**********&lt;BR /&gt;
NOTE: The data set [/pre] So, for a high-value date, I would recommend the number 6589336  &lt;BR /&gt;
or (my preference) the syntax  &lt;BR /&gt;
%sysevalf( "31dec20000"d +1 )</description>
      <pubDate>Thu, 16 Jun 2011 17:16:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-Min-date-function-on-a-variable/m-p/15450#M2057</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2011-06-16T17:16:56Z</dc:date>
    </item>
    <item>
      <title>Re: Max/Min date function on a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-Min-date-function-on-a-variable/m-p/15451#M2058</link>
      <description>Friend,&lt;BR /&gt;
&lt;BR /&gt;
I hope the below code will suffix your problem, if not please do update me to make corrections&lt;BR /&gt;
&lt;BR /&gt;
data test;&lt;BR /&gt;
input id date date7. sales;&lt;BR /&gt;
format date date7.;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1 01Jan11 12&lt;BR /&gt;
1 02Jan11 20&lt;BR /&gt;
2 12Jan11 34&lt;BR /&gt;
2 23Feb11 21&lt;BR /&gt;
;&lt;BR /&gt;
proc sort;&lt;BR /&gt;
by id descending date;&lt;BR /&gt;
run;&lt;BR /&gt;
data test1;&lt;BR /&gt;
set test;&lt;BR /&gt;
by id;&lt;BR /&gt;
if first.id then total_sales=0;&lt;BR /&gt;
total_sales+sales;&lt;BR /&gt;
if last.id;&lt;BR /&gt;
run;&lt;BR /&gt;
proc print;&lt;BR /&gt;
run;</description>
      <pubDate>Sun, 19 Jun 2011 11:58:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-Min-date-function-on-a-variable/m-p/15451#M2058</guid>
      <dc:creator>Ankitsas</dc:creator>
      <dc:date>2011-06-19T11:58:06Z</dc:date>
    </item>
  </channel>
</rss>

