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;
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;
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;
Thank you!
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.