Help using Base SAS procedures

Return variable with Maximum Value

Reply
New Contributor
Posts: 4

Return variable with Maximum Value

I have data of the form:

ID Sales_JAN Sales_FEB Sales_MAR etc....

I know that I can get the maximum sales value for each ID as:
MAX_SALES = MAX (OF SALESSmiley Happy;

I also want to know which month the maximum sales happened in (the variable name which corresponds to the maximum value). I cannot figure out how to do this efficiently. I'm sure SAS has a way of doing this (it has a way of doing most things). If JAN and FEB had the same sales and they were the MAX value, how are ties handled?
Respected Advisor
Posts: 3,799

Re: Return variable with Maximum Value

WHICHN Function

--------------------------------------------------------------------------------

Searches for a numeric value that is equal to the first argument, and returns the index of the first matching value.
Super Contributor
Super Contributor
Posts: 365

Re: Return variable with Maximum Value

Hello Ddeb,

You can add to you datastep writing MAX_SALES into a macro variable like this:

call SUMPUTX('Max',MAX_SALES);

and then in a separate datastep:

array{*} S sales:;
do i=1 to hbound{S};
if s[i]=&Max then Max_month=i;
end;

get month number for maximum sales.

Sincerely,
SPR
Ask a Question
Discussion stats
  • 2 replies
  • 107 views
  • 0 likes
  • 3 in conversation