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!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.