<?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: proc sql to find the max(variable) in each year in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-find-the-max-variable-in-each-year/m-p/482222#M286838</link>
    <description>&lt;P&gt;I think with "real" SQL this would be simple because you could use the RANK OVER / PARTITION BY functions that PROC SQL lacks (I think?) - otherwise might be awkward.&amp;nbsp; Here's a data step solution that assigns an integer to each customer ID (which I guessed was a character variable, length $20 - change as appropriate) for the purpose of gathering up data, then back-converts to customer ID at the end.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select count(distinct customer_id) into :ncust from testdata;
select min(year(dt)), max(year(dt)) into :firstyr, :lastyr from testdata;

create table CN as select "cust2num" as fmtname, "C" as type, 
customer_id as start, customer_id as end, monotonic() as label 
from 
	(select distinct customer_id from testdata);

create table NC as select "num2cust" as fmtname, "N" as type,
monotonic() as start, monotonic() as end, customer_id as label
from
	(select distinct customer_id from testdata);
quit;

proc format cntlin=CN; run;
proc format cntlin=NC; run;

data top_customer_by_year (keep=year customer total);
set testdata END=LAST;
length year 3 customer $20 total 8;
array T {&amp;amp;ncust,&amp;amp;firstyr:&amp;amp;lastyr} _temporary_;
T[put(customer_id,$cust2num.)*1,year(dt)]+total_retail_price;
if last then do;
	do year=lbound(T,2) to hbound(T,2);
		customer = '';
		total = .;
		do c=1 to dim(T,1);
			if T[c,year]&amp;gt;total then do;
				customer=put(c,num2cust.);
				total=T[c,year];
			end;
		end;
		output;
	end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 29 Jul 2018 02:18:32 GMT</pubDate>
    <dc:creator>quickbluefish</dc:creator>
    <dc:date>2018-07-29T02:18:32Z</dc:date>
    <item>
      <title>proc sql to find the max(variable) in each year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-find-the-max-variable-in-each-year/m-p/482217#M286837</link>
      <description>&lt;P&gt;&amp;nbsp;Hello,&lt;/P&gt;&lt;P&gt;I've written a query below that gets the customer which generated the maximum revenue or spent the most money. But I would like to get the customer(s) generated the most money in each individual year. The set contains data from 2007 to 2015 and date is in mmddyy10. How would I go about achieving this with proc sql? thanks a lot for the help&lt;/P&gt;&lt;PRE&gt;proc sql;
select Customer_ID, sum
	from(
		select Customer_ID, sum(total_retail_Price) as Sum label= "Total Revenue" format= dollar10.2
		from testdata
		group by customer_ID
	    )
	having sum= max(sum);
	quit;&lt;/PRE&gt;</description>
      <pubDate>Sun, 29 Jul 2018 01:17:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-find-the-max-variable-in-each-year/m-p/482217#M286837</guid>
      <dc:creator>mjabed600</dc:creator>
      <dc:date>2018-07-29T01:17:09Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql to find the max(variable) in each year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-find-the-max-variable-in-each-year/m-p/482222#M286838</link>
      <description>&lt;P&gt;I think with "real" SQL this would be simple because you could use the RANK OVER / PARTITION BY functions that PROC SQL lacks (I think?) - otherwise might be awkward.&amp;nbsp; Here's a data step solution that assigns an integer to each customer ID (which I guessed was a character variable, length $20 - change as appropriate) for the purpose of gathering up data, then back-converts to customer ID at the end.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select count(distinct customer_id) into :ncust from testdata;
select min(year(dt)), max(year(dt)) into :firstyr, :lastyr from testdata;

create table CN as select "cust2num" as fmtname, "C" as type, 
customer_id as start, customer_id as end, monotonic() as label 
from 
	(select distinct customer_id from testdata);

create table NC as select "num2cust" as fmtname, "N" as type,
monotonic() as start, monotonic() as end, customer_id as label
from
	(select distinct customer_id from testdata);
quit;

proc format cntlin=CN; run;
proc format cntlin=NC; run;

data top_customer_by_year (keep=year customer total);
set testdata END=LAST;
length year 3 customer $20 total 8;
array T {&amp;amp;ncust,&amp;amp;firstyr:&amp;amp;lastyr} _temporary_;
T[put(customer_id,$cust2num.)*1,year(dt)]+total_retail_price;
if last then do;
	do year=lbound(T,2) to hbound(T,2);
		customer = '';
		total = .;
		do c=1 to dim(T,1);
			if T[c,year]&amp;gt;total then do;
				customer=put(c,num2cust.);
				total=T[c,year];
			end;
		end;
		output;
	end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 29 Jul 2018 02:18:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-find-the-max-variable-in-each-year/m-p/482222#M286838</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2018-07-29T02:18:32Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql to find the max(variable) in each year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-find-the-max-variable-in-each-year/m-p/482223#M286839</link>
      <description>&lt;P&gt;can you please provide a sample to work with and your expected output from the sample?&lt;/P&gt;</description>
      <pubDate>Sun, 29 Jul 2018 02:19:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-find-the-max-variable-in-each-year/m-p/482223#M286839</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-07-29T02:19:19Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql to find the max(variable) in each year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-find-the-max-variable-in-each-year/m-p/482233#M286840</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select 
    Customer_ID, 
    year format=year4., 
    sum label= "Total Revenue" format= dollar10.2
from(
    select 
        Customer_ID, 
        intnx("year", date, 0) as year,
        sum(total_retail_Price) as Sum 
    from testdata
    group by customer_ID, calculated year
)
group by year
having sum= max(sum);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;this assumes that your dates are SAS dates. If they are SAS datetimes, replace &lt;STRONG&gt;date&lt;/STRONG&gt; with &lt;STRONG&gt;datepart(date)&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the result set, &lt;STRONG&gt;year&lt;/STRONG&gt; is&amp;nbsp;the SAS date of the first day of the year, formatted as an integer. If you prefer a simple number variable, use &lt;STRONG&gt;year(year) as year format=4.0&lt;/STRONG&gt;.&lt;/P&gt;</description>
      <pubDate>Sun, 29 Jul 2018 03:39:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-find-the-max-variable-in-each-year/m-p/482233#M286840</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-07-29T03:39:23Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql to find the max(variable) in each year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-find-the-max-variable-in-each-year/m-p/482238#M286841</link>
      <description>&lt;P&gt;You just need to use GROUP BY.&amp;nbsp; You might also need to transform your date variable to have just the YEAR value.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
create table want as
  select
    year(date) as year
   ,max(volume) as max_volume
   ,mean(volume) as mean_volume
  from sashelp.stocks
  group by 1
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 29 Jul 2018 04:45:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-find-the-max-variable-in-each-year/m-p/482238#M286841</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-07-29T04:45:35Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql to find the max(variable) in each year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-find-the-max-variable-in-each-year/m-p/482303#M286842</link>
      <description>&lt;P&gt;This is pretty simple using two PROC SUMMARY calls. The first PROC SUMMARY computes the sum of the sales by customer_id and year; the second takes the results of the first and finds the maximum values per year and the customer_id associated with the maximum value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 29 Jul 2018 18:37:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-find-the-max-variable-in-each-year/m-p/482303#M286842</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-07-29T18:37:10Z</dc:date>
    </item>
  </channel>
</rss>

