/* Date: Fri, 15 Dec 2006 12:34:51 -0500 From: "Richard A. DeVenezia" Subject: Horizontal Bar Chart ... in Proc REPORT ! To: SAS-L@LISTSERV.UGA.EDU In the spirit of the season, presented here is some sample code that uses Proc REPORT to grapically show within group percents. */ %macro create_bar_images ( cback = cxffaaaa , cfore = cxff0000 , xpixels = 100 , ypixels = 15 ); ods _all_ close; goptions reset=all gsfname=gfxpath goutmode=replace; goptions device=gif xpixels=&xpixels. ypixels=&ypixels.; ods listing; %local workpath dir gfxpath; %let workpath = %sysfunc(pathname(WORK)); %let dir = %sysfunc(dcreate(gfxparts, &workpath.)); %let gfxpath = %sysfunc(pathname(WORK))\gfxparts; filename gfxpath "&gfxpath."; %local notes; %let notes = %sysfunc(getoption(notes)); options nonotes; data _null_; W = input(getoption('xpixels'),best12.); H = input(getoption('ypixels'),best12.); rc = gset ('catalog','work','bars'); rc = ginit (); rc = gset ('WINDOW', 1, 1,1, W,H); rc = gset ('TRANSNO', 1); rc = gset ('filtype', 'solid'); do left = 0 to 100; name = cats('bar',left); rc = graph ('clear',name); rc = gset ('colrep',1,"&cback."); rc = gset ('colrep',2,"&cfore."); rc = gset('FILCOLOR',1); rc = gdraw('BAR',1,1, W,H); if left > 0 then do; rc = gset('FILCOLOR',2); rc = gdraw('BAR',1,1, left,H); end; rc = graph ('update'); end; rc = graph ('clear','blank'); rc = graph ('update'); rc = gterm (); run; options ¬es.; %mend; options mprint; /**/ %create_bar_images (cfore=cxEC9F04,cback=cxFCEED1);*,cback=cxFDCD6F); /**/ data SafariSurvey; do teamId = 1 to 4; do until (ranuni(31415) < 0.005); * animalId = floor (10 * ranuni(123)); animalId = floor (10 + rannor(126)); animalID = mod (animalId + teamId, 10); output; end; end; label teamId = 'Team' animalId = 'Animal' ; run; proc format; value creature 0 = 'Lion' 1 = 'Tiger' 2 = 'Hyena' 3 = 'Giraffe' 4 = 'Vulture' 5 = 'Alligator' 6 = 'Nanosquito' 7 = 'Zebra' 8 = 'Antelope' 9 = 'Hippo' ; run; proc sql; create table Counts as select teamId, animalId format=creature., count(*) as Count from SafariSurvey group by teamId, animalId ; quit; ods html file="%sysfunc(pathname(WORK))\report-hbar.html" style=statistical ; *ods pdf file="%sysfunc(pathname(WORK))\report-hbar.pdf" style=statistical ; ods listing; title "Safari Survey"; title2 "Animal counts"; footnote "Created with PROC REPORT"; proc report nowindows data=Counts(rename=(teamId=group animalId=subGroup)) style(report)=[borderwidth=0 cellspacing=0 rules=none cellpadding=4] style(header)=[font_size=11pt] ; columns group subgroup count pct; define group / order; define subgroup / order; define count / analysis ; define pct / computed format=percent6.0 '%' ; break before group / summarize ul ; compute count; myLineCounter + 1; if myLineCounter = 1 then count_sum = count.sum; * use for debugging, esp. if a compute before section added; * call execute(catx(' ','%put',myLineCounter,ysum,y.sum)); gfxpath = "%sysfunc(pathname(WORK))/gfxparts"; gfxpath = "gfxparts"; if myLineCounter > 1 then do; pct = round(count.sum/count_sum * 100); * y as percentage of group; if pct < 0 then pct = 0; else if pct > 100 then pct = 100; imgfile = cats (gfxpath,'/bar',pct,".gif"); gpct = pct; end; else do; imgfile = cats (gfxpath,'/',"blank.gif"); gpct = 100; end; style = 'style=[' || ' posttext=" "' || ' postimage=' || quote(trim(imgfile)) || ']' ; call define (_col_,'style',style); endcomp; compute pct; pct = gpct / 100; endcomp; compute after group; line ' '; myLineCounter = 0; endcomp; run; ods _all_ close; dm 'log' log;