Hello
I want to remerging summary statistics with raw data.
What is wrong here?
PROC SQL;
create table tbl as
select a.*,
(select sum(sales) as total_sales,
count(* ) as Total_count
from sashelp.shoes)
from sashelp.shoes as a
;
quit;
/*ERROR: A Composite expression (usually a subquery) is used incorrectly in an expression.*/
Why not simply
proc sql;
create table tbl as
select *,
sum(sales) as total_sales,
count(*) as Total_count
from sashelp.shoes;
quit;
Why not simply
proc sql;
create table tbl as
select *,
sum(sales) as total_sales,
count(*) as Total_count
from sashelp.shoes;
quit;
The answer you got from @PeterClemmensen is probably the solution I would suggest for this exact query. But there may be other situations where you want to use a subquery, you can do it like this:
PROC SQL;
create table tbl as
select detail.*,sum.*
from sashelp.shoes as detail,
(select sum(sales) as total_sales,
count(* ) as Total_count
from sashelp.shoes) as sum
;
quit;
Note that as your queries get larger and more complicated, it makes thing a lot easier in the long run if you use more describing aliases than "a" and "b", in this case "detail" and "sum" seem better. I know, everybody (except yours truly) use short and cryptic aliases, but they really should not!
Thank you so much!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.