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;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—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
  • 2 replies
  • 784 views
  • 2 likes
  • 2 in conversation