Hello everyone! Is there any way to insert a row at the end of the data set with the totals? Here it is a sample data:
DATA | COUNT |
DATA 1 | 23 |
DATA 2 | 35 |
DATA 3 | 27 |
DATA 4 | 24 |
DATA 5 | 25 |
DATA 6 | 22 |
DATA 7 | 29 |
And here it is what I want to do:
DATA | COUNT |
DATA 1 | 23 |
DATA 2 | 35 |
DATA 3 | 27 |
DATA 4 | 24 |
DATA 5 | 25 |
DATA 6 | 22 |
DATA 7 | 29 |
Total | 185 |
I will appreciate any kind of help!
Greetings,
Milenko
Editor's Note: Thanks everyone for all of the answers. The SQL one works well. This solution gives a little more flexibility for the number of variables that need to be summed and inserting text for the summary row.
Using PROC SUMMARY to generate the total row is the most "SAS like" way to do it.
If you want to put the string "TOTAL" into one of your character variables so that you can tell which row is the total and which rows are the real data then you would need to add some extra code.
For example if we assume that SASHELP.CLASS is your input data then these two steps will generate a new dataset named WANT with the totals appended.
proc means noprint data=sashelp.class ;
output out=summary sum=;
run;
data want ;
set sashelp.class summary (in=in2);
if in2 then name='TOTAL';
run;
One example:
data have;
input var $ COUNT;
cards;
DATA1 23
DATA2 35
DATA3 27
DATA4 24
DATA5 25
DATA6 22
DATA7 29
;
data want;
set have end=last;
_ct+count;
output;
if last then
do;
var='Total';
Count=_CT;
output;
end;
drop _ct;
run;
Hai.Kuo! Thank you very much for your answer. Im new in using SAS, so i dont understand very well the code that you are giving me. I foyu could walk me a bit, it would great! I do know what you mean with data want and data have, but the rest is not really understandable for my level of knowledge, which is pretty basic. In any case, I really appreciate for your help!
Greetings!
Milenko
data want;
set have;
run;
proc sql;
insert into want
select Ttotal' as data,sum(count) as count from have;
quit;
slchen! Thank you for your answer! Im new in using SAS, and I know very little, I tried your code, and it didnt work...it says that the Ttotal, sum columns dont exist, and that the sum functions needs a numeric value. Here it is the code that Im using:
data WORK.FILA_FINAL_COUNT;
set WORK.query_for_query_final;
run;
proc sql;
insert into WORK.FILA_FINAL_COUNT
select Ttotal as data,sum(C) as count from WORK.query_for_query_final;
quit;
It would be great if you could tell me what Im doing worng.
Greetings, and thanks again!
Milenko
Sorry to miss a double quote for "Total";
data WORK.FILA_FINAL_COUNT;
set WORK.query_for_query_final;
run;
proc sql;
insert into WORK.FILA_FINAL_COUNT
select "Total" as data,sum(C) as count from WORK.query_for_query_final;
quit;
slchen, Thank you again! I tried the code and it still has problems with the C parameter:
ERROR: The SUM summary function requires a numeric argument.
ERROR: The following columns were not found in the contributing tables: C
Could you help me with this error, please!
Greetings!
Milenko
data WORK.FILA_FINAL_COUNT;
set WORK.query_for_query_final;
run;
proc sql;
insert into WORK.FILA_FINAL_COUNT
select "Total" as data,sum(count) as count from WORK.query_for_query_final;
quit;
It worked perfectly! Thank you so much slchen!
Greetings!
Milenko
slchen,
Your code worked great! Thanks! But I have one more doubt. Is there a way to modify your code in order to count more than one column? I mean, becuase I have three more columns with numbers and I want to have a row in the end with the totals of each one of those columns. Can it be done?
Milenko
Why?
If it is just for a report then just have the report generate the totals.
PROC PRINT ...;
VAR DATA COUNT ;
SUM COUNT;
RUN:
You can use PROC SUMMARY (means) to generate totals and PROC APPEND to attach to an existing file.
data class;
set sashelp.class ;
run;
proc means noprint data=class ;
output out=summary sum=;
run;
proc append data=summary base=class force;
run;
proc print data=class ;
run;
Tom! Thank you very much! But I dont want to do a report, I want create another data set. I want to create another data set, and i will keep working with it, applying functions, etc.In any case, I really appreciate your help.!
Greetings!
Milenko
Editor's Note: Thanks everyone for all of the answers. The SQL one works well. This solution gives a little more flexibility for the number of variables that need to be summed and inserting text for the summary row.
Using PROC SUMMARY to generate the total row is the most "SAS like" way to do it.
If you want to put the string "TOTAL" into one of your character variables so that you can tell which row is the total and which rows are the real data then you would need to add some extra code.
For example if we assume that SASHELP.CLASS is your input data then these two steps will generate a new dataset named WANT with the totals appended.
proc means noprint data=sashelp.class ;
output out=summary sum=;
run;
data want ;
set sashelp.class summary (in=in2);
if in2 then name='TOTAL';
run;
Hey Tom! Thank you so much again! It is really helpful! I tried your code and it worked! But im almost there. The result that I get is this:
DATA | COUNT | _TYPE_ | _FREQ_ | name |
DATA 1 | 23 | |||
DATA 2 | 35 | |||
DATA 3 | 27 | |||
DATA 4 | 24 | |||
DATA 5 | 25 | |||
DATA 6 | 22 | |||
DATA 7 | 29 | |||
- | 185 | 0 | 7 | Total |
And the result that Im looking foward is this:
DATA | COUNT |
DATA 1 | 23 |
DATA 2 | 35 |
DATA 3 | 27 |
DATA 4 | 24 |
DATA 5 | 25 |
DATA 6 | 22 |
DATA 7 | 29 |
Total | 185 |
Is there a way to tweak your code to make it happen?
In any case you already have helped me a lot! Thank you!
Greetings!
Milenko
The extra variables _TYPE_ and _FREQ_ are generated by PROC MEANS. You can remove them with a DROP statement.
The extra variable NAME is because you did not adjust the program to match the variables in your dataset. From your output you want to assign 'Total' to the variable DATA instead of the variable NAME.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.