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 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1121 views
  • 0 likes
  • 3 in conversation