BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MilenkoAndreas
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

 

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;

View solution in original post

22 REPLIES 22
Haikuo
Onyx | Level 15

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;

MilenkoAndreas
Calcite | Level 5

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

slchen
Lapis Lazuli | Level 10

data want;

set have;

run;

proc sql;

  insert into want

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

  quit;

MilenkoAndreas
Calcite | Level 5

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

slchen
Lapis Lazuli | Level 10

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;

MilenkoAndreas
Calcite | Level 5

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

slchen
Lapis Lazuli | Level 10

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;

MilenkoAndreas
Calcite | Level 5

It worked perfectly! Thank you so much slchen!

Greetings!

Milenko

MilenkoAndreas
Calcite | Level 5

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

Tom
Super User Tom
Super User

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;

MilenkoAndreas
Calcite | Level 5

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

Tom
Super User Tom
Super User

 

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;

MilenkoAndreas
Calcite | Level 5

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

Tom
Super User Tom
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 22 replies
  • 46935 views
  • 13 likes
  • 8 in conversation