Solved
Contributor
Posts: 26

# Find the maximum of an arrayed variable

I am trying to find the maximum of several variables (ml1-ml9), which are broken up into a before and after group, defined by the value in variable 'before'.

If before=3 then find the maximum of ml1-ml3 and then of ml4-ml9.

If before=6 then find the maximum of ml1-ml6 and then of ml6-ml9.

I tried to do this inside of an array, but unless I'm writing the do loop or array incorrectly, the max function doesn't appear to work in this context:

data amt;  set dom;  bef=0;

array ml(9) ml1-ml9;

do j=1 to before;

bef+ml(j);

maxbef=max(of ml(j));

---

71

ERROR 71-185: The MAX function call does not have enough arguments.

What better ways might there be to do this?  My other thought was subsetting the data into Before and After datasets with an array by deleting irrelevant data.

Accepted Solutions
Solution
‎04-28-2013 06:51 PM
Super User
Posts: 8,127

## Re: Find the maximum of an arrayed variable

I think you are just going to have to get loopy to meet your requirements.

data amt;

set dom;

array ml(9) ml1-ml9;

do j=1 to before;

MAXBEF = MAX(0,MAXBEF,ml(j));

end;

do j=before+1 to dim(ml);

MAXAFTER = MAX(0,MAXAFTER,ml(j));

end;

run;

All Replies
Solution
‎04-28-2013 06:51 PM
Super User
Posts: 8,127

## Re: Find the maximum of an arrayed variable

I think you are just going to have to get loopy to meet your requirements.

data amt;

set dom;

array ml(9) ml1-ml9;

do j=1 to before;

MAXBEF = MAX(0,MAXBEF,ml(j));

end;

do j=before+1 to dim(ml);

MAXAFTER = MAX(0,MAXAFTER,ml(j));

end;

run;

Contributor
Posts: 26

## Re: Find the maximum of an arrayed variable

Brilliant, thanks!

Super User
Posts: 10,788

## Re: Find the maximum of an arrayed variable

Why not use two simple statements ?

MAXBEF = MAX(of ml1-ml4);

MAXAFTER = MAX(of ml4-ml9);

Respected Advisor
Posts: 3,167

## Re: Find the maximum of an arrayed variable

I think 'Before' is a variable whose values may change across obs. Tom's approach is more robust, no more "if ... then" needed.

Haikuo

Super User
Posts: 10,788

## Re: Find the maximum of an arrayed variable

Agree. I need take a look closer .

🔒 This topic is solved and locked.

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

Discussion stats
• 5 replies
• 1336 views
• 0 likes
• 4 in conversation