Compute max values

Accepted Solution Solved
Reply
Contributor
Posts: 73
Accepted Solution

Compute max values

Data structure:

PriceTheMonthTheYear
1012013
1112013
1512012
1222013
1322012

I am struggling to compute max value of Price variable for each combination of TheMonth and TheYear

Final Result Expected:

PriceTheMonthTheYear
1112013
1512012
1222013
1322012

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


Accepted Solutions
Solution
‎07-03-2013 09:59 AM
Super Contributor
Posts: 334

Re: Compute max values

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

View solution in original post


All Replies
SAS Super FREQ
Posts: 3,752

Re: Compute max values

The simplest way is to bypass the DATA step and use PROC MEANS:

proc means data=... max;

class TheMonth TheYear;

var Price;

run;

Super User
Posts: 10,018

Re: Compute max values

there is need a option:

proc means nway ;

or use BY instead of CLASS.

Ksharp

SAS Super FREQ
Posts: 3,752

Re: Compute max values

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;

Solution
‎07-03-2013 09:59 AM
Super Contributor
Posts: 334

Re: Compute max values

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

Contributor
Posts: 73

Re: Compute max values

That was it. You resolved it esjackso1 thanks a bunch.

Contributor
Posts: 73

Re: Compute max values

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 235 views
  • 0 likes
  • 4 in conversation