## MAX Function

Solved
Super Contributor
Posts: 1,041

# MAX Function

Hi,

Which of the two usages of MAX function is the correct one???

Thanks

proc sql;

create table maxlos AS

select

pat_id;

count(*) as nstays,

max(disdate - admitdate +1) as MAXLOS

from table1;

run;

proc sql;

create table maxlos AS

select

pat_id;

count(*) as nstays,

max(disdate - admitdate) +1  as MAXLOS

from table1;

run;

Accepted Solutions
Solution
‎05-15-2015 06:54 PM
Super User
Posts: 23,754

## Re: MAX Function

I can see the difference in the code. Mathematically they are the same, which you can easily verify:

data have;

do i=1 to 1000;

date1=floor(rand("Uniform")*1000+1);

date2=floor(500+rand("uniform")*1000);

LOS=date2-date1+1;

if date2>date1 then output;

end;

format date1 date2 date9.;

run;

proc sql;

create table want as

select max(date2-date1+1) as max1,

max(date2-date1)+1 as max2

from have;

quit;

proc means data=have n max;

var LOS;

run;

proc print data=want;

run;

All Replies
Super User
Posts: 23,754

## Re: MAX Function

Aren't they the same?

Super Contributor
Posts: 1,041

## Re: MAX Function

hi,

the first one has everything in braces. The second has +1 outside of the braces.

Regards

Solution
‎05-15-2015 06:54 PM
Super User
Posts: 23,754

## Re: MAX Function

I can see the difference in the code. Mathematically they are the same, which you can easily verify:

data have;

do i=1 to 1000;

date1=floor(rand("Uniform")*1000+1);

date2=floor(500+rand("uniform")*1000);

LOS=date2-date1+1;

if date2>date1 then output;

end;

format date1 date2 date9.;

run;

proc sql;

create table want as

select max(date2-date1+1) as max1,

max(date2-date1)+1 as max2

from have;

quit;

proc means data=have n max;

var LOS;

run;

proc print data=want;

run;

Super User
Posts: 6,781

## Re: MAX Function

Neither is correct.  You are using the MAX function with only one argument, so the function does nothing.  For the MAX function to do anything, you have to have more than one argument separated by commas, for example:

The correct set of arguments depend on what result you are looking for, but I would expect both of your original programs give you the same result as if you had omitted the function entirely.

Good luck.

Super User
Posts: 8,115

## Re: MAX Function

Two different things with the same name.

The SQL MAX() aggregate function operates over multiple observations.

The SAS MAX() function operates just over its list of arguments.

🔒 This topic is solved and locked.