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

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.*/
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Why not simply

 

proc sql;
    create table tbl as
    select *, 
           sum(sales) as total_sales,
           count(*) as Total_count
    from sashelp.shoes;
quit;

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

Why not simply

 

proc sql;
    create table tbl as
    select *, 
           sum(sales) as total_sales,
           count(*) as Total_count
    from sashelp.shoes;
quit;
DaveStar
Obsidian | Level 7
He wants to merge summary statistics (sum of sales and count of rows) with the original raw data ( sashelp.shoes).
What you did is just calculate summary statistics without merge it back with raw data!
DaveStar
Obsidian | Level 7
Sorry. I think you are right and what you wrote is remerging .Dave
s_lassen
Meteorite | Level 14

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!

Ronein
Meteorite | Level 14

Thank you so much!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 663 views
  • 2 likes
  • 4 in conversation