BookmarkSubscribeRSS Feed
jdub
Calcite | Level 5
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?
7 REPLIES 7
FriedEgg
SAS Employee
Can you provide sample inbound and outbound data sets so that we can better determine your question.
jdub
Calcite | Level 5
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
Peter_C
Rhodochrosite | Level 12
> 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
Cynthia_sas
Diamond | Level 26
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]
Peter_C
Rhodochrosite | Level 12
wow
5 minutes and 3 substantial responses
what a channel!
FriedEgg
SAS Employee
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
SPR
Quartz | Level 8 SPR
Quartz | Level 8
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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1854 views
  • 0 likes
  • 5 in conversation