Data structure:
Price | TheMonth | TheYear |
---|---|---|
10 | 1 | 2013 |
11 | 1 | 2013 |
15 | 1 | 2012 |
12 | 2 | 2013 |
13 | 2 | 2012 |
I am struggling to compute max value of Price variable for each combination of TheMonth and TheYear
Final Result Expected:
Price | TheMonth | TheYear |
---|---|---|
11 | 1 | 2013 |
15 | 1 | 2012 |
12 | 2 | 2013 |
13 | 2 | 2012 |
I believe I cannot use MAX() function directly as it is not possible to define the array.
Will DO statement resolve it? in conjunction with BY statement after data is sorted by TheYear and TheMonth; sort of hazy on how to hash out the logic
Appreciate the feedback.
Thankfully,
Dhanashree Gadkari
Another alternative would be to use group by in SQL:
Proc sql;
create table max as
select max(price) as Mprice, themonth, theyear
from yourdata
group by themonth, theyear
;
quit;
If you dont need a dataset you can leave the table statement off and the report will output.
EJ
The simplest way is to bypass the DATA step and use PROC MEANS:
proc means data=... max;
class TheMonth TheYear;
var Price;
run;
The NWAY option only affects an output data set. For printed output, it doesn't matter:
proc means data=sashelp.cars max /* NWAY */;
class origin type;
var mpg_city;
run;
Another alternative would be to use group by in SQL:
Proc sql;
create table max as
select max(price) as Mprice, themonth, theyear
from yourdata
group by themonth, theyear
;
quit;
If you dont need a dataset you can leave the table statement off and the report will output.
EJ
That was it. You resolved it esjackso1 thanks a bunch.
Although the output of your suggestion is what I am exactly looking for, I need to restore data structure of output statistics as shown in Final data structure above for further plotting bubble charts .
So do you suggest using OUTPUT statement in proc MEANS, to capture Price, TheYear, TheMonth?
Not sure how to delete unwanted columns like _TYPE_ _FREQ_ and keep only MAX stat in proc step
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.