BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DavidPhillips2
Rhodochrosite | Level 12

I am trying to select the group with the maximum of a value.  An example of how do this in Oracle is below.  SQL server also allows this using the first(column) method.  SAS SQL does not include either of these two methods.  Do I need to write a self join to do this in SAS?  I would strongly rather not write a self-join due to it being processor inefficient.

SELECT degree_level, total

    from

        (

            select count(*) as total, degree_level

            from enrollment

            where academic_period= '201410' and college = 'MD'

            group by degree_level

            order by total desc

        )

     where rownum =1;

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

This comes close if you don't have multiple same max total,

proc sql;

SELECT *

  from

  ( select count(*) as total, degree_level

  from enrollment

  where academic_period= '201410' and college = 'MD'

  group by degree_level

/* order by total desc*/

  )

/* werhe rownum=1;*/

  having total = max(total);

quit;

But to get the EXACT the same, you would have to resort to undocumented MONOTONIC().

View solution in original post

2 REPLIES 2
Haikuo
Onyx | Level 15

This comes close if you don't have multiple same max total,

proc sql;

SELECT *

  from

  ( select count(*) as total, degree_level

  from enrollment

  where academic_period= '201410' and college = 'MD'

  group by degree_level

/* order by total desc*/

  )

/* werhe rownum=1;*/

  having total = max(total);

quit;

But to get the EXACT the same, you would have to resort to undocumented MONOTONIC().

DavidPhillips2
Rhodochrosite | Level 12

Hai.kuo,

Your solution is very efficient and I all I need for this solution.

I noticed that the order by block with sub queries seems to cause errors.  I was attempting to use an into statement with the order by block.  Even if the order by solution worked in this particular case the having is much better.


When I use the into block it automatically selects the top record so if there are two records with the same total I don’t have to worry.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1789 views
  • 0 likes
  • 2 in conversation