Good morning,
I would like to recreate a bar graph. The bar graph I would like to recreate was found at this sas website:
https://support.sas.com/kb/35/774.html. You can see the full code and results by clicking the tab at the top.
In the code below I have replaced the data with my own and when trying to recreate the graph I get the following error:
/* Set the graphics environment */
goptions reset=all cback=white border htitle=12pt htext=10pt;
data ds1;
input year $ count cum;
datalines;
2014 28683 28683
2015 28219 56902
2016 40644 97546
2017 28179 125725
2018 39876 165601
2019 27201 192802
2020 31505 224307
;
run;
data ds2;
set ds1;
n=_n_;
run;
data _null_;
set ds2 end=eof;
if eof then call symput('skip',left(n));
run;
data ds2;
set ds2;
resp=count; mid='cum'; output;
resp=count; mid='cum'; output;
run;
proc sort;
by year mid;
run;
data anno;
set ds2;
by year mid;
length function color $8 text $20 style $ 20;
/* Populate the table */
if first.year then do;
function='move'; xsys='2'; ysys='1';
midpoint=mid; group=year; y=0;
output;
function='cntl2txt'; output;
function='label'; xsys='A'; ysys='3';
x=+4; y=13;
text=trim(left(put(exp,8.1)));
color='black'; position='+'; when='a';
output;
function='move'; xsys='2'; ysys='1';
midpoint=mid; group=year; y=0;
output;
function='cntl2txt'; output;
function='label'; xsys='A'; ysys='3';
x=+4; y=9;
text=trim(left(put(rev,8.1)));
color='black'; position='+'; when='a';
output;
end;
/* Generate the table frame */
function='move'; xsys='3'; ysys='3';
x=3; y=6;
output;
function='bar'; xsys='1'; ysys='3';
x=100; y=15;
style='empty'; color='black'; line=0;
output;
/* Generate the row headers */
function='label'; xsys='3'; ysys='3';
style='marker'; text='U'; color='cx7c95ca';
x=4; y=13; position='6';
output;
function='label'; xsys='3'; ysys='3';
style='"Albany AMT"'; text='Participant'; color='black';
x=7; y=13; position='6';
output;
function='label'; xsys='3'; ysys='3';
style='marker'; text='U'; color='cxde7e6f';
x=4; y=8.5; position='6';
output;
function='label'; xsys='3'; ysys='3';
style='"Albany AMT"'; text='Cumulative'; color='black';
x=7; y=9; position='6';
output;
/* Generate the vertical lines in the table */
function='move'; xsys='1'; ysys='1';
x=0; y=0;
output;
function='draw'; xsys='1'; ysys='3';
x=0; y=6;
line=1; color='black';
output;
function='move'; xsys='1'; ysys='1';
x=100; y=0;
output;
function='draw'; xsys='1'; ysys='3';
x=100; y=6;
line=1; color='black';
output;
if first.year and n ^=&skip then do;
function='move'; xsys='2'; ysys='1';
midpoint=mid; y=0; group=year;
output;
function='move'; xsys='A'; ysys='1';
x=+15.5; x=+12; y=0;
output;
function='draw'; xsys='A'; ysys='3';
x=+0; y=6;
color='black'; line=1;
output;
end;
/* Generate the horizontal line in the table */
function='move'; xsys='3'; ysys='3';
x=3; y=10.5;
output;
function='draw'; xsys='1'; ysys='3';
x=100; y=10.5;
line=1; color='black';
output;
run;
title1 'Participants';
axis1 label=none
order=(0 to 250000 by 50000)
minor=none;
axis2 label=none
value=none
origin=(20pct,22pct)
offset=(4pct,4pct);
axis3 label=none;
footnote1 'Fiscal Year';
footnote2 h=.5 ' ';
pattern1 value=solid color=cx7c95ca;
pattern2 value=solid color=cxde7e6f;
proc gchart data=ds2;
vbar mid / sumvar=resp group=year
coutline=black patternid=midpoint
space=0 gspace=5 width=4
cframe=white autoref clipref
raxis=axis1 maxis=axis2 gaxis=axis3
annotate=anno;
run;
quit;
Your specific problem is the combination of
gspace=5 width=4
Pick one.
Or perhaps move to the more recent procedures that will do a lot of what you want with a lot fewer headaches.
Consider
proc sgplot data=ds1; vbar year / response=count ; xaxistable count / label='Participants' position=bottom location=outside; xaxistable cum /label='Cumulative' position=bottom location=outside; run;
Your specific problem is the combination of
gspace=5 width=4
Pick one.
Or perhaps move to the more recent procedures that will do a lot of what you want with a lot fewer headaches.
Consider
proc sgplot data=ds1; vbar year / response=count ; xaxistable count / label='Participants' position=bottom location=outside; xaxistable cum /label='Cumulative' position=bottom location=outside; run;
Thank you! This works great with less work.
I would highly recommend not using GCHART or GPLOT, use SGPLOT instead. They're more modern procs that generate better quality graphics and most importantly it's much easier to use.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.