Help using Base SAS procedures

Proc sql

Reply
Contributor
Posts: 39

Proc sql

Hi All, can you please explain crisply how the below query is working,

i wanted to know how where confition checking the each observtion of table b with every observation of table a.

 

 

/* Quesry is used to find the ranks*/

 

data a;
input id sal;
cards;
1 100
2 200
3 300
9 78
10 890
11 890
;

proc sql;
select a.*,(select count(distinct sal) from a where b.sal le a.sal) as r from a b;
quit;

 

Super Contributor
Posts: 440

Re: Proc sql

Posted in reply to shivamarrora0

Quite simply a and b are the same table which you are joining with itself - b is just an alias for a which is set up by the "from a b" portion of the query.

Respected Advisor
Posts: 4,925

Re: Proc sql

Posted in reply to shivamarrora0

Conceptually, SAS is running a subquery for every observation of table a. Since that subquery also involves table a, you must give the table an alias name (b) so as to allow an expression like "b.sal le a.sal". I think the query should be written as

 

proc sql;
select 
	b.*,
	(	select 
			count(distinct id) 
		from a 
		where b.sal le a.sal	) as r 
from a as b;
quit;
PG
Contributor
Posts: 39

Re: Proc sql

Hi, I wanted to know the reason why b.sal le a.sal
comparing 1 observation of B table to every observation of A table .. ?
Super User
Posts: 11,343

Re: Proc sql

Posted in reply to shivamarrora0

shivamarrora0 wrote:
Hi, I wanted to know the reason why b.sal le a.sal
comparing 1 observation of B table to every observation of A table .. ?

 

This would one approach to "how many salary values are the same or more than the person with the givend ID, including that person".

Is a "what" answer.  Why is up to your rules.

Super User
Posts: 5,429

Re: Proc sql

Posted in reply to shivamarrora0
Well, it's your program and your data, so basically you should know better than us what this program's task is.
Data never sleeps
Ask a Question
Discussion stats
  • 5 replies
  • 351 views
  • 1 like
  • 5 in conversation