MAX Function

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,041
Accepted Solution

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: 19,817

Re: MAX Function

Posted in reply to robertrao

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;

View solution in original post


All Replies
Super User
Posts: 19,817

Re: MAX Function

Posted in reply to robertrao

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: 19,817

Re: MAX Function

Posted in reply to robertrao

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: 5,511

Re: MAX Function

Posted in reply to robertrao

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:

max(disdate - admitdate, 1)

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
Super User
Posts: 7,059

Re: MAX Function

Posted in reply to Astounding

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.

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

Discussion stats
  • 5 replies
  • 316 views
  • 3 likes
  • 4 in conversation