DATA Step, Macro, Functions and more

MIN function not working

Accepted Solution Solved
Reply
Contributor
Posts: 70
Accepted Solution

MIN function not working

I use max function in Proc SQL its working fine.
MIN function not working properly giving wrong results. Some of them giving me 0 instead of values.

 

example:

 

min(case when region = 'SOU' then amt else 0 end) as MIN_sou, 
min(case when region = 'NOR' then amt else 0 end) as MIN_nor
,


Accepted Solutions
Solution
‎07-31-2017 04:28 PM
Super User
Super User
Posts: 7,042

Re: MIN function not working

[ Edited ]
Posted in reply to Kalai2008

It is just doing what you asked.  

Consider this two observation dataset.

data have ;
  input regsion $ amt ;
cards;
SOU 100
NOR 150
;

The first CASE will return 0 for the second row and the second CASE will return 0 for the first row.

You should use missing value instead of 0 when the region does not match the target minimum.

,min(case when region = 'SOU' then amt else . end) as MIN_sou
,min(case when region = 'NOR' then amt else . end) as MIN_nor

You probably should have done the same for your MAX() calculations, but perhaps you didn't have any regions where the actual maximum amount was negative you didn't notice it wasn't working.

View solution in original post


All Replies
Trusted Advisor
Posts: 1,913

Re: MIN function not working

Posted in reply to Kalai2008

It could be that 0 is exactly what you asked for with your code (although it may not be what you wanted to get).

 

But ... we don't know what's in your data, and we don't really know what you want to get as a result ... so could you please show us a representative portion of your data, and explain exactly what result you want to get?

Solution
‎07-31-2017 04:28 PM
Super User
Super User
Posts: 7,042

Re: MIN function not working

[ Edited ]
Posted in reply to Kalai2008

It is just doing what you asked.  

Consider this two observation dataset.

data have ;
  input regsion $ amt ;
cards;
SOU 100
NOR 150
;

The first CASE will return 0 for the second row and the second CASE will return 0 for the first row.

You should use missing value instead of 0 when the region does not match the target minimum.

,min(case when region = 'SOU' then amt else . end) as MIN_sou
,min(case when region = 'NOR' then amt else . end) as MIN_nor

You probably should have done the same for your MAX() calculations, but perhaps you didn't have any regions where the actual maximum amount was negative you didn't notice it wasn't working.

Contributor
Posts: 70

Re: MIN function not working

Thank you for the reply!..

 

Here is my data look like:


acct  date    region    amt
1     5/21      sou        20
1     5/21      nor         21
1     6/22      sou        30
1     6/22      sou        50

2     5/21      nor         15


Output want:

min_sou min_nor
20               21

Super User
Posts: 19,789

Re: MIN function not working

Posted in reply to Kalai2008

I think @Tom hit the nail on the head, if you set it to 0 if missing, that will always be the minimum. Set it to missing instead.

Contributor
Posts: 70

Re: MIN function not working

Thank you..It worked!

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 189 views
  • 2 likes
  • 4 in conversation