BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Konkordanz
Pyrite | Level 9

Hi,

with sql I want to create a new dataset that creates also a summarized row. The following code shows what I mean and where my problem is: When I execute this code once, it works. But if I execute it twice or more, the summarized row expands with every repetition. Do you have a clue why this is happen and which way could be the better one?

 

proc sql;
create table Kategorietest as select
 	sex, 
  	sum(case when age>=13 then weight end) as Teen,
   	sum(case when age<13 then weight end) as Kid,
	count(case when age <13 then age end) as Anzahl
from sashelp.class
group by sex
union all
select "Total",sum(Teen),sum(kid),Sum(anzahl) from Kategorietest;
quit;
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

It is always a bad idea to use the same dataset in CREATE TABLE and FROM, PROC SQL should write a message to the log.

Create both parts from the source dataset:

proc sql;
create table Kategorietest as select
  sex, 
  sum(case when age>=13 then weight end) as Teen,
  sum(case when age<13 then weight end) as Kid,
  count(case when age <13 then age end) as Anzahl
from sashelp.class
group by sex
union all
select
  "Total",
  sum(case when age>=13 then weight end) as Teen,
  sum(case when age<13 then weight end) as Kid,
  count(case when age <13 then age end) as Anzahl
from sashelp.class;
quit;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

It is always a bad idea to use the same dataset in CREATE TABLE and FROM, PROC SQL should write a message to the log.

Create both parts from the source dataset:

proc sql;
create table Kategorietest as select
  sex, 
  sum(case when age>=13 then weight end) as Teen,
  sum(case when age<13 then weight end) as Kid,
  count(case when age <13 then age end) as Anzahl
from sashelp.class
group by sex
union all
select
  "Total",
  sum(case when age>=13 then weight end) as Teen,
  sum(case when age<13 then weight end) as Kid,
  count(case when age <13 then age end) as Anzahl
from sashelp.class;
quit;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 2 replies
  • 666 views
  • 2 likes
  • 2 in conversation