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

Hi community,

Are the results of a PROC SQL's "Select distinct" always given in sorted order?

Or are there data arrangements (maybe with indexes?) that would/could affect the order of the results?

 

I can't find any documentation where the operations behind "Distinct" are explicitely described

I've performed some tests on indexes datasets but it seems that the results are always sorted.

(I've never questionned that, but I am doing now)

I'm not using any groups and the order variable I need is always the "distinct" variable.

 

data class(index=(sex));
set sashelp.class;
run;
proc sort data=class force;by sex name;run;

proc sql;
select distinct age
from sashelp.class
;
*same results as;
select distinct age
from class
;
quit;

I'd be grateful if you could share some piece of information on this.

Thanks

________________________

- Cheers -

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

Probably, but it's also not a good idea to assume that it always will.

 

Generally, SQL is a set language (as in logical sets), and order doesn't matter for sets.  So the same query will sometimes reorder data and sometimes will not, depending on how the SQL optimizer decides to execute the query.

 

If you care about the order of the output data, you should always add an ORDER BY clause.

The Boston Area SAS Users Group is hosting free webinars!
Next up: Bart Jablonski and I present 53 (+3) ways to do a table lookup on Wednesday Sep 18.
Register now at https://www.basug.org/events.

View solution in original post

1 REPLY 1
Quentin
Super User

Probably, but it's also not a good idea to assume that it always will.

 

Generally, SQL is a set language (as in logical sets), and order doesn't matter for sets.  So the same query will sometimes reorder data and sometimes will not, depending on how the SQL optimizer decides to execute the query.

 

If you care about the order of the output data, you should always add an ORDER BY clause.

The Boston Area SAS Users Group is hosting free webinars!
Next up: Bart Jablonski and I present 53 (+3) ways to do a table lookup on Wednesday Sep 18.
Register now at https://www.basug.org/events.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 813 views
  • 2 likes
  • 2 in conversation