Insert a Row at the end of the data with totals

Reply
Occasional Contributor
Posts: 18

Insert a Row at the end of the data with totals

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:

DATACOUNT
DATA 123
DATA 235
DATA 327
DATA 424
DATA 525
DATA 622
DATA 729

And here it is what I want to do:

DATACOUNT
DATA 123
DATA 235
DATA 327
DATA 424
DATA 525
DATA 622
DATA 729
Total185

I will appreciate any kind of help!

Greetings,

Milenko

Respected Advisor
Posts: 3,124

Re: Insert a Row at the end of the data with totals

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;

Occasional Contributor
Posts: 18

Re: Insert a Row at the end of the data with totals

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

Super Contributor
Posts: 275

Re: Insert a Row at the end of the data with totals

data want;

set have;

run;

proc sql;

  insert into want

  select Ttotal' as data,sum(count) as count from have;

  quit;

Occasional Contributor
Posts: 18

Re: Insert a Row at the end of the data with totals

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

Super Contributor
Posts: 275

Re: Insert a Row at the end of the data with totals

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;

Occasional Contributor
Posts: 18

Re: Insert a Row at the end of the data with totals

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

Super Contributor
Posts: 275

Re: Insert a Row at the end of the data with totals

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;

Occasional Contributor
Posts: 18

Re: Insert a Row at the end of the data with totals

It worked perfectly! Thank you so much slchen!

Greetings!

Milenko

Occasional Contributor
Posts: 18

Re: Insert a Row at the end of the data with totals

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

Super User
Super User
Posts: 6,330

Re: Insert a Row at the end of the data with totals

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;

Occasional Contributor
Posts: 18

Re: Insert a Row at the end of the data with totals

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

Super User
Super User
Posts: 6,330

Re: Insert a Row at the end of the data with totals

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;

Occasional Contributor
Posts: 18

Re: Insert a Row at the end of the data with totals

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:

DATACOUNT_TYPE__FREQ_name
DATA 123
DATA 235
DATA 327
DATA 424
DATA 525
DATA 622
DATA 729
-18507Total

And the result that Im looking foward is this:

DATACOUNT
DATA 123
DATA 235
DATA 327
DATA 424
DATA 525
DATA 622
DATA 729
Total185

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

Super User
Super User
Posts: 6,330

Re: Insert a Row at the end of the data with totals

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.

Ask a Question
Discussion stats
  • 22 replies
  • 8265 views
  • 12 likes
  • 8 in conversation