Contributor
Posts: 33

# PROC PRINT sum A by B

I am trying to use PROC PRINT to output sum variable A by variable B with an external printto reference. There are many distinct values of A for each B.

I have attempted to use:
proc print data=x;
sum A;
by B;
run;

Does anyone have insight of a better way to achieve the goal of outputting sum A by B?
Posts: 1,318

## Re: PROC PRINT sum A by B

Can you provide sample inbound and outbound data sets so that we can better determine your question.
Contributor
Posts: 33

## Re: PROC PRINT sum A by B

A mockup of data x is below. I am trying to suppress the observations in the output so sum values print for A, B and C. If I use noobs after proc print the obs. record number is suppressed but the list of obs. still posts.

I am just looking for sums.

I would like to generate
sum A = 41
sum B = 45
sum C = ..

data x:
A B
1 12
1 14
1 15
2 3
2 9
2 10
2 12
2 11
3 66
3 71
3 23
3 8
3 9

What I get is:

12
14
15
__A SUM__
41
Valued Guide
Posts: 2,191

## Re: PROC PRINT sum A by B

> data x:
> A B
> 1 12
> 1 14
> 1 15
> 2 3
> 2 9
> 2 10
> 2 12
> 2 11
> 3 66
> 3 71
> 3 23
> 3 8
> 3 9

proc means data= x ;
class a ;
var b ;
output sum= out= x_summary ;
run;
then choose what to do with the summary totals in data set x_summary.
Output data set x_summary will hold the total value of B for each value of A.
The variables will be:
A
B (being the sum of B within A )
_freq_ (the number of rows in X for the current value of A)
_type_ (=0 for overall total and 1 for the total lines of each A)
better documentation can be found at
http://support.sas.com/documentation/cdl/en/allprodsproc/63875/HTML/default/a003135046.htm#a00314568...

good luck
peterC
SAS Super FREQ
Posts: 9,366

## Re: PROC PRINT sum A by B

Hi:
It seems to me that you do not want a DETAIL report (where you see 1 report row for every observation in the original input data). You describe a SUMMARY report (where every report row represents the information for a group of observations).

SAS has many, many summary-level procedures: PROC SQL, PROC MEANS, PROC TABULATE, PROC REPORT.

Depending on what you want to do (generate output dataset or generate an HTML, RTF or PDF report), you might try starting with PROC MEANS and then graduate to TABULATE or REPORT if MEANS isn't what you want.

cynthia

[pre]
ods html file='c:\temp\examp_means.html' style=sasweb;
proc means data=x sum maxdec=0;
var a b;
run;
ods html close;
[/pre]
Valued Guide
Posts: 2,191

## Re: PROC PRINT sum A by B

wow
5 minutes and 3 substantial responses
what a channel!
Posts: 1,318

## Re: PROC PRINT sum A by B

data x;
infile datalines dlm='09'x;
input a b :8.;
datalines;
1 12
1 14
1 15
2 3
2 9
2 10
2 12
2 11
3 66
3 71
3 23
3 8
3 9
;
run;

proc tabulate data=x out=y;
var a b;
table a*sum b*sum;
run;

proc transpose data=y(keep=a_sum b_sum) out=z; run;

proc print data=z noobs; run;

Output:

a_Sum 28
b_Sum 263
Super Contributor
Posts: 366

## Re: PROC PRINT sum A by B

Hello Jdub,

I guess that you need something like this:
[pre]
proc means data=x nway noprint;
output out=sum(drop=_ sum=;
var b;
class a;
run;
proc print data=sum;
id a;
var b;
run;
[/pre]
Sincerely,
SPR
Discussion stats
• 7 replies
• 232 views
• 0 likes
• 5 in conversation