BookmarkSubscribeRSS Feed
shivamarrora0
Obsidian | Level 7

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;

 

5 REPLIES 5
ChrisBrooks
Ammonite | Level 13

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.

PGStats
Opal | Level 21

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
shivamarrora0
Obsidian | Level 7
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 .. ?
ballardw
Super User

@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.

LinusH
Tourmaline | Level 20
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 1001 views
  • 1 like
  • 5 in conversation