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

Could someone please help me with how SAS/SQL processes nested queries or subqueries. Would it do the inner select, "select sum(price) from MasterFile" only once or with each iteration of the outer query?

proc sql; 
create table categorySpend as 
select categoryid, sum (price) as CategoryRevenue, (select sum(price) from MasterFile)as TotalRevenue 
from MasterFile 
group by categoryid; 
quit;

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Good question!

 

Here is a test I just ran:

 

data masterfile;
  input categoryid price;
  do i=1 to 1000000;
    output;
  end;
cards;
1 10
2 20
3 30
;

proc sql; 
  select categoryid, 
         sum (price) as CategoryRevenue,
         (select sum(price) from MasterFile)as TotalRevenue 
    from MasterFile 
      group by categoryid
  ; 
quit;

proc sql; 
  select categoryid, 
         sum (price) as CategoryRevenue
    from MasterFile 
      group by categoryid
  ; 
  select sum(price) as TotalRevenue
    from MasterFile
  ;
quit;

Both sets of code select the same things. When I first ran the test it appeared that the embedded select ran for each subgroup. However, when I set up the code to run about 100 times, on average both sets took the same time to run.

 

As such, I'd have to guess that it is only run once.

 

HTH,

Art, CEO, AnalystFinder.com

 

 

 

View solution in original post

3 REPLIES 3
art297
Opal | Level 21

Good question!

 

Here is a test I just ran:

 

data masterfile;
  input categoryid price;
  do i=1 to 1000000;
    output;
  end;
cards;
1 10
2 20
3 30
;

proc sql; 
  select categoryid, 
         sum (price) as CategoryRevenue,
         (select sum(price) from MasterFile)as TotalRevenue 
    from MasterFile 
      group by categoryid
  ; 
quit;

proc sql; 
  select categoryid, 
         sum (price) as CategoryRevenue
    from MasterFile 
      group by categoryid
  ; 
  select sum(price) as TotalRevenue
    from MasterFile
  ;
quit;

Both sets of code select the same things. When I first ran the test it appeared that the embedded select ran for each subgroup. However, when I set up the code to run about 100 times, on average both sets took the same time to run.

 

As such, I'd have to guess that it is only run once.

 

HTH,

Art, CEO, AnalystFinder.com

 

 

 

gargi01
Calcite | Level 5
^ Thanks a ton 🙂
Kurt_Bremser
Super User

Just to add: the probably fastest method might be this:

proc summary data=masterfile;
var price;
class categoryid;
output out=sum1 (drop=_type_ _freq_ rename=(price=categoryrevenue)) sum=;
run;

data sum2;
set sum1;
retain totalrevenue;
if categoryid = .
then do;
  totalrevenue = categoryrevenue;
  delete;
end;
run;

As this really does only one pass through the original dataset.

With 10 times as many observations as used by @art297, these steps outperformed the SQL 8:16 seconds to 44.53 seconds on my 2-CPU IBM p520.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 3 replies
  • 860 views
  • 0 likes
  • 3 in conversation