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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.