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