DATA Step, Macro, Functions and more

Proc SQL Left join and order by

Reply
Super Contributor
Posts: 371

Proc SQL Left join and order by

Hello,

I want to write this

 

proc sql;

  create table test as

  select  t1.*, t2.v

  from  t1 left join t2

  on t1.w=t2.w;

quit;

 

Is there any mather , if I do not use " order by ...", an other mean, sas can give any false result ?

if we do not write

proc sql;

  create table test as

  select  t1.*, t2.v

  from  t1 left join t2

  on t1.w=t2.w

  order by m1,

quit;

 

If yes, please can you give some exemples ?

 

 

 

Contributor hbi
Contributor
Posts: 66

Re: Proc SQL Left join and order by

The presence or lack of an "order by" statement in SQL will not affect the number of observations in the output dataset.

 

The above statement would be true, in a join even if you "ORDER BY" a variable that is not in the output dataset.

 

Even if you added something weird, like " ORDER BY monotonic(), ranuni(123) ", it should not affect the number of observations in the output dataset. 

Super User
Posts: 11,343

Re: Proc SQL Left join and order by

I am not sure that I understand what you mean by "false result";

 

Currently the implementation of proc sql without an order by variable will tend to order data by the values of the varaibles as they appear on the Select clause. The order of set T1 in your example may not be the order it had going in.

Super Contributor
Posts: 371

Re: Proc SQL Left join and order by

you say "Currently the implementation of proc sql without an order by variable will tend to order data by the values of the varaibles as they appear on the Select clause"

 

You mean that : sas will order the final data by the variables in select clause ?

Thank you

PROC Star
Posts: 7,468

Re: Proc SQL Left join and order by

Your code works as is! The only problem might be if v and m1 are in both datasets. However, the result of either run wouldn't be false in such cases, just not what you might have expected.

 

 

Respected Advisor
Posts: 4,920

Re: Proc SQL Left join and order by

It might only be a transcription typo but the comma after m1 should be a semicolon. Otherwise, proc SQL will be looking for a variable named quit.

PG
Ask a Question
Discussion stats
  • 5 replies
  • 366 views
  • 0 likes
  • 5 in conversation