STYLE ON THE CLASS AND CLASSLEV STATEMENTS:

by Irina 18. January 2008 10:06

Sometimes we want to color the same id (or something else) in the same color.
Although not strictly a reporting procedure, one of the most important procedures in the would-be traffic lighter’s toolkit is PROC FORMAT. User defined formats allow the SAS® programmer to define foreground (font) and background color.

PROC TABULATE

 In PROC TABULATE, background and foreground colors via a user-defined format can be applied in the various style statements (for example, the PROC statement, the VAR statement(s), CLASS statement(s), the CLASSLEV statements(s), the TABLES statement(s), and the BOX statement.) They can also be applied in a user-defined style template. Colors can be assigned directly in the style statement, or with a user-defined format.
STYLE ON THE PROC STATEMENT:
In general, you would not use style on the PROC TABULATE statement for the purposes of traffic lighting, as it applies an overall style to the table rather than highlights particular values. Nonetheless, it’s fun to play with while formatting your reports.
Syntax:
Proc tabulate data=yourdataset style={background=lightblue}; This would create a light blue background for your table regardless of what overall style you use.

We can use the original table(list) in order to make the format
proc sort nodupkey data=all_r out=list_value;
by id;
run ;
proc sql;
create table formats as 
select  a.id as start,
        a.id as end ,
        color as label,
        'color' as fmtname
from 
(select a.* ,monotonic() as misp
from list_value a ) a,
 color1 b
where a.misp=(b.misp-5);
quit;
 I calculate misp-5 in order to remove the black color.

File with colors: color.csv (557.00bytes)

 
data all_r1;
set all_r;
code=id;
run;


proc format cntlin=formats ;
run;




proc sql;
create table all_r1 as
select a.* ,monotonic() as misp
from all_r1 a;
quit;



ods html file = 'c:\print_results.html';

Proc tabulate data=all_r1 ;
class misp code;
Var id  new_new_id exposure;
table misp*code ,  
id=' '*(  min= 'id' *f=comma10. )*[style=[background=color. font_weight=bold]]
exposure=' '*(  mean= 'exposure' *f=comma10. )*[style=[  font_weight=bold]];
run;
The result:
The SAS System

 idexposure
mispcode15131
115
21515141
31515151
41515161
51616141
61717131
71717178
81818146
91818148
101919131

CREATING MULTI-LABEL FORMATS

by Irina 8. May 2007 13:28

Multi-label formats can be used in PROC SUMMARY and PROC TABULATE.PROC FORMAT with its new MULTILABEL option could make overlapping.

Some comments on this format:

1. Note that we must specify the (multilabel) option when generating the format.
2. We can preserve the order of formatted values on tabulate output by specifying the NOTSORTED option
in the generation of the FORMAT and PRELOADFMT ORDER=DATA options
; the CLASS specification in TABULATE.
3. Note that we have labels for each of the 5 decision codes. We also map all codes beginning with the
letter
a into APPROVE TOTALS and all those beginning with the letter d into DECLINE TOTALS
4. The CLASS statement in TABULATE or SUMMARY ( 􀀆 ) must include the MLF option to generate the

Example1.(proc means)

data f;
input
Obs  name $ age;
cards;
1 mary 10
2 fred 28
3 john 7
4 erica 29
5 tim 5
6 susan 13
7 andrew 47
8 peter 37
9 cindy 16
10 thea 21
11 joe 20
12 tilly 58
13 ruth 75
14 rick 8
15 richard 19
16 helen 26
17 alexa 10
18 heather 2
;
run;
proc format;
value mlf1_fmt(multilabel)
0-12='child'
13-19='adolescent'
0-19='children and adolescents'
20-21='young adult'
low-21='children,adolescents and young adults'
22-high='all adults'
low-high='all patients';
run;


ods listing close;
ods rtf file='c:\result.rtf' bodytitle style=journal;
proc means data=f mean  median maxdec=2;
format age mlf1_fmt.;
class age /mlf order=data preloadfmt;
var age;
run;
ods rtf close;
ods listing;


proc format;
value mlf2_fmt(multilabel notsorted)
0-12='child'
13-19='adolescent'
0-19='children and adolescents'
20-21='young adult'
low-21='children,adolescents and young adults'
22-high='all adults'
low-high='all patients';
run;

ods listing close;
ods rtf file='c:\result1.rtf' bodytitle style=journal;
proc means data=f mean  median maxdec=2;
format age mlf2_fmt.;
class age /mlf order=data preloadfmt;
var age;
run;
ods rtf close;
ods listing;

Example2.(proc tabulate)

proc format;
value key low - 0.20 = 'a1'
0.20 < - 0.25 = 'a2'
0.25 < - 0.35 = 'a4'
0.35 < - 0.80 = 'd1'
0.80 < - high = 'd6'
;
picture p8r (round)
low - < 0 = '0009.99%' (prefix='-')
0 - high = '0009.99%'
;
value $deccode (multilabel notsorted) ..
'a0' - 'a9' = 'APPROVE TOTALS'
'a1' = ' a1: Approval'
'a2' = ' a2: Weak Approval'
'a4' = ' a4: Approved Alternate Product'
'd0' - 'd9' = 'DECLINE TOTALS'
'd1' = ' d1: Decline for Credit'
'd6' = ' d6: Decline Other'
run;
data decision;
do id = 1 to 1000;
decision = put(ranuni(7),key.);
output;
end;
run;
proc tabulate data=decision noseps formchar=' ';
class decision/mlf preloadfmt order=data;
format decision $deccode.;
table (decision all)
,n*f=comma5.
pctn='%'*f=p8r.
/rts=33 row=float misstext=' ';
run;

 

PROC TABULATE

by Irina 6. May 2007 13:57
by Jonas V. Bilenas, JP Morgan Chase, Wilmington, DE :

The data used for this paper was simulated with random variable functions. It represents results of a mailed Balance Transfer offer to existing customers of a consumer credit card. Code that generated the data is shown here:proc format;
value offer low -0.45 ='A' /* hypothetical BALANCE TRANSFER Offers */
0.45<-high = 'B' ;
value $grroff 'A' = 'aoff' 'B' = 'boff'
;
value aoff low-0.10 = '1' other = '0' /* Response rates per offer */
;
value $mline 'A' = '6500' 'B' = '5400' /* Average Balance Transfer */
;
data test;
do campaign = '2004/3', '2004/4'; /* campaign quarters */
do i = 1 to 10000;
mailed=1;
offer=put(ranuni(12),offer.);
fmtuse=put(offer,$grroff.);
respond=input(putn(ranuni(14),fmtuse),best12.);
if respond then baltran=rannor(15)*500+input(put(offer,$mline.),best12.);
else baltran=.;
output;
end;
end;
run;
For the response rate, we can use the MEAN statistic since the respond variable is binary. For balance transfer per responder we can use the MEAN function as well since the non-responders received a missing value for baltran variable. Note that we format the response rate with a percentw.d format and use a dollarw.d format for the balance transfer amount.To calculate the average balance transfer for all accounts mailed is a bit tricky. We can use the PCTSUM statistic which takes the variable and divides by a denominator variable defined in “<>”. The result is multiplied by 100, which we do not require here but can correct for with a PICTURE format. Code and output is illustrated

 

proc format;

picture btm (round) low - high = "0,000,009" (prefix="$" mult=.01) ;

picture pct (round) low - < 0 = "0009.99%" (prefix="-")

0 - high = "0009.99%"; run;


ODS Listing CLOSE;
ODS html file="c:\TAB1.html";
proc tabulate data=test noseps;
class offer campaign;
var respond baltran mailed;
keylabel n=" " sum=" " mean=" " pctn=" " pctsum=" "; 
table campaign*(offer all="TOTAL")
,
n="Mailed"*f=comma9.
pctn="%"*f=pct. 
respond="Responders"*f=comma10. 
respond="Response Rate"*mean*f=percent9.2
baltran="Balance Transfer per respond"*mean*f=dollar9.0
baltran=" Balance Transfer per mailed"*pctsum*f=btm.
/rts=19 row=float misstext=" " box="SUGI";
run;
ODS html close;
ODS Listing;

 

SUGIMailed%RespondersResponse RateBalance Transfer per respondBalance Transfer per mailed
campaignoffer4,52345.23%47010.39% $6,511$677
2004/3A
B5,47754.77%    
TOTAL10,000100.00%47010.39% $6,511$306
2004/4offer4,48144.81%47710.64% $6,493$691
A
B5,51955.19%    
TOTAL10,000100.00%47710.64% $6,493$310

Tags: tabulate

SAS

About the author

Irina Spivak Irina Spivak
Team Leader at G-Stat. More...


Send mail Email

Blogroll

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2013

    Sign in

    eXTReMe Tracker