Search notes:
SAS: proc report
Simple proc report example
data tq84_report;
length txt $3 val 4.;
input txt val ;
datalines;
foo 42
bar .
foo 18
baz 332
bar 7
foo 219
foo .
bar 153
bar 22
quit;
proc report data=tq84_report;
where val > 20;
/* The column statement identifies all variables
that are used in the report: */
column txt val val_sq;
define txt / display;
define val / display;
compute val_sq;
val_sq = val ** 2;
endcomp;
run;
define order
The define order
statement defines which column the report is ordered on:
data tq84_report;
length txt $3 val 4.;
input txt val ;
datalines;
foo 42
bar 61
foo 18
baz 332
bar .
foo 219
foo .
bar 153
bar 22
quit;
proc report data=tq84_report;
column txt val;
define txt / order; /* Order by txt */
define val / display;
run;
define group
The define / group
statement by itself is not very useful. It's usually combined with an define / analysis
statement:
data tq84_report;
length txt1 $3
txt2 $3
txt3 $2
val 4.;
input txt1 txt2 txt3 val;
datalines;
abc jkl uv 13
ghi mno uv 288
ghi pqr wx 7
abc mno yz 15
def pqr uv 3
abc jkl uv .
ghi jkl wx 96
ghi mno yz 75
abc pqr yz 111
abc jkl uv 86
def pqr uv 39
ghi jkl yz 22
abc pqr wx .
ghi mno uv 41
def pqr yz 52
quit;
proc report data=tq84_report;
column txt1 txt2 txt3 val;
define txt1 / group;
define txt2 / group;
define txt3 / group;
define val / analysis sum;
run;
define analysis
With define / analysis
, it's possible to create sums, averages, totals etc:
data tq84_report;
length txt1 $3
txt2 $3
txt3 $2
val 4.;
input txt1 txt2 txt3 val;
datalines;
abc jkl uv 13
ghi mno uv 288
ghi pqr wx 7
def jkl uv 3
abc jkl uv .
ghi jkl wx 96
ghi mno uv 75
abc pqr wx 111
abc jkl uv 86
def pqr yz 39
abc jkl uv 15
ghi jkl wx 22
abc pqr wx .
ghi mno uv 41
def pqr yz 52
quit;
proc report data=tq84_report;
column txt1 txt2 txt3
/* val is used for multiple statistics per
group. Therefore, we create an alias
for each statistics: */
val = val_sum
val = val_avg
val = val_min
val = val_max
val = val_cnt;
define txt1 / group;
define txt2 / group;
define txt3 / group;
define val_sum / analysis sum 'Total';
define val_avg / analysis mean format=3.1 'Avg.' ;
define val_min / analysis min 'Min.' ;
define val_max / analysis max 'Max.' ;
define val_cnt / analysis n 'Count';
run;
define across
define / across
is used to create pivot tables :
data tq84_report;
length txt1 $3
txt2 $3
txt3 $2
val 4.;
input txt1 txt2 txt3 val;
datalines;
abc jkl uv 13
ghi mno uv 288
ghi pqr wx 7
def jkl uv 3
abc jkl uv .
ghi jkl wx 96
ghi mno uv 75
abc pqr wx 111
abc jkl uv 86
def pqr yz 39
abc jkl uv 15
ghi jkl wx 22
abc pqr wx .
ghi mno uv 41
def pqr yz 52
quit;
proc report data=tq84_report;
column txt1 txt2 txt3 val;
define txt1 / group 'txt one';
define txt2 / group 'txt two';
define txt3 / across 'txt three';
define val / analysis sum 'Total';
run;
compute
compute
can be used to add addtional text before and after groups:
data tq84_report;
length txt1 $3
txt2 $3
txt3 $2
val 4.;
input txt1 txt2 txt3 val;
datalines;
abc jkl uv 13
ghi mno uv 288
ghi pqr wx 7
def jkl uv 3
abc jkl uv .
ghi jkl wx 96
ghi mno uv 75
abc pqr wx 111
abc jkl uv 86
def pqr yz 39
abc jkl uv 15
ghi jkl wx 22
abc pqr wx .
ghi mno uv 41
def pqr yz 52
quit;
proc report data=tq84_report;
column txt1 txt2 txt3 val;
define txt1 / group 'txt one';
define txt2 / group 'txt two';
define txt3 / across 'twt three';
define val / analysis sum 'Total';
compute before txt1;
line @1 "Values for " txt1 $20.;
endcomp;
compute after txt1;
length group_txt $20;
if txt1 = 'abc' then group_txt = 'Computed text foo'; else
if txt1 = 'def' then group_txt = 'Computed text bar'; else
if txt1 = 'ghi' then group_txt = 'Computed text baz'; else
group_txt = '???';
line @1 group_txt $20.;
endcomp;
run;
Using the list option
The list
options directs proc report
to print what it actually executes. This is useful because proc report
uses defaults for statements that are not explicitly stated:
data tq84_dat;
length txt_one $10
txt_two $10
num_one 8.
num_two 8.;
input txt_one
txt_two
num_one
num_two;
datalines;
foo abc 2 40
run;
proc report
data=tq84_dat
list;
run;
/*
PROC REPORT DATA=WORK.TQ84_DAT LS=132 PS=60 SPLIT="/" NOCENTER ;
COLUMN txt_one txt_two num_one num_two;
DEFINE txt_one / DISPLAY FORMAT= $10. WIDTH=10 SPACING=2 LEFT "txt_one" ;
DEFINE txt_two / DISPLAY FORMAT= $10. WIDTH=10 SPACING=2 LEFT "txt_two" ;
DEFINE num_one / SUM FORMAT= BEST9. WIDTH=9 SPACING=2 RIGHT "num_one" ;
DEFINE num_two / SUM FORMAT= BEST9. WIDTH=9 SPACING=2 RIGHT "num_two" ;
RUN;
*/