Data visualization with SAS programming

How to code a graph with sas

Reply
Contributor
Posts: 74

How to code a graph with sas

Hello,

 

I tried all my best to search for the code that I could deal with to generate the graph as attached, but failed, or wont sas generate the graph like that? could anybody help out?

 

Thanks!

 

Da

SAS Super FREQ
Posts: 3,225

Re: How to code a graph with sas

Try looking at the doc for the SGPLOT proceduire. The key statements are

VBAR and VLINE.

 

It's not clear whether you wanted the table below. If so, see the doc for the XAXISTABLE statement. Also search Sanjay's blog for 'XAXISTABLE' to find examples like this one.

 

To get you started, use this code as an example.  After you've worked on it for a while, write back with any remaining questions.

 

data a;
input year class $ dist ar;
datalines;
2015 A 0.090 0.02
2015 B 0.304 0.00
2015 C 0.128 0.22
2015 D 0.128 0.50
2015 E 0.123 0.68
2015 F 0.127 0.81
2015 G 0.156 0.92
2016 A 0.090 0.02
2016 B 0.315 0.00
2016 C 0.128 0.25
2016 D 0.121 0.511
2016 E 0.121 0.701
2016 F 0.121 0.87
2016 G 0.153 0.91
;

proc sgplot data=a;
vbar class / response=dist group=year groupdisplay=cluster;
vline class / response=ar group=year y2axis;
yaxis label="Distribution" min=0 max=1;
y2axis label="Rate" min=0 max=1;
run;
Contributor
Posts: 74

Re: How to code a graph with sas

Hello,

The attached is exactly what I need, which was done with excel, however I
want it to be done with sas code, the final one needs to combine the bar
and the curve, also include data box as showed in the attached. Would you
help me? Thank you so much!
Contributor
Posts: 74

Re: How to code a graph with sas

Hello,

The attached is exactly what I need, which was done with excel, however I
want it to be done with sas code, the final one needs to combine the bar
and the curve, also include data box as showed in the attached. Would you
help me? Thank you so much!

SAS Super FREQ
Posts: 1,044

Re: How to code a graph with sas

Please attach data (real or made up) and what ever code you have tried.

What release of SAS do you have?

Contributor
Posts: 74

Re: How to code a graph with sas

Hello,

my code for generating the bar graph as below, but I want an additional curve added into the bar graph, please see the attachment where the first graph is all I want and the second graph is what I had now. Thanks!

goptions reset=all cback=white border htitle=12pt htext=10pt;

 

data ds1;

input year $ mid $ resp exp rev;

datalines;

null d_2016 0.45 0.45 0.35

null d_2015 0.35 0.45 0.35

s_300 d_2016 0.65 0.65 0.55

s_300 d_2015 0.55 0.65 0.55

s_640 d_2016 0.42 0.42 0.65

s_640 d_2015 0.65 0.42 0.65

s_670 d_2016 0.35 0.35 0.75

s_670 d_2015 0.75 0.35 0.75

s_700 d_2016 0.83 0.83 0.47

s_700 d_2015 0.47 0.83 0.47

s_730 d_2016 0.48 0.48 0.58

s_730 d_2015 0.58 0.48 0.58

s_760 d_2016 0.67 0.67 0.69

s_760 d_2015 0.69 0.67 0.69

;

run;

 

 

data ds2;

set ds1;

n=_n_;

run;

 

data _null_;

set ds2 end=eof;

if eof then call symput('skip',left(n));

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=+2; 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=+2; 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=3; y=13; position='6';

output;

function='label'; xsys='3'; ysys='3';

style='"Albany AMT"'; text='d_2015'; color='black';

x=6; y=13; position='6';

output;

function='label'; xsys='3'; ysys='3';

style='marker'; text='U'; color='cxde7e6f';

x=3; y=8.5; position='6';

output;

function='label'; xsys='3'; ysys='3';

style='"Albany AMT"'; text='d_2016'; color='black';

x=6; 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=+8; 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 'Score distribution';

 

axis1 label=(a=90 'Percentegy')

order=(0 to 1 by 0.1)

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=0 width=5

cframe=ltgray autoref clipref

raxis=axis1 maxis=axis2 gaxis=axis3

annotate=anno;

run;

quit;

SAS Super FREQ
Posts: 1,044

Re: How to code a graph with sas

[ Edited ]

It was not very clear which columns you wanted to display for the lines,  I just took a guess.  They are grouped, but both groups have the same value.  Graph and code are attached.  You can add more rows to the table at the bottom.  Your data looks different from the Excel graph.

 

BarLineTable2.png

 

/*--Draw Graph--*/
ods graphics / reset width=5in height=3in attrpriority=color;
title 'Overall Distribution';
proc sgplot data=ds1 noborder noautolegend;
styleattrs wallcolor=cxf0f0f0;
vbarparm category=year response=resp / group=mid groupdisplay=cluster
clusterwidth=0.6 outlineattrs=(color=black)
baselineattrs=(thickness=0);
series x=year y=exp / group=mid lineattrs=(color=gold thickness=2) y2axis;
series x=year y=rev / group=mid lineattrs=(color=green thickness=2) y2axis;
xaxistable resp / class=mid colorgroup=mid;
xaxis display=(noline noticks nolabel) ;
yaxis display=(noline noticks) grid gridattrs=(color=white thickness=1) offsetmin=0;
y2axis display=(noline noticks) min=0;
run;

Contributor
Posts: 74

Re: How to code a graph with sas

Unfortunately, I am in sas 9.3 release, therefore your code wont work here. Would you please help me with a 9.3 version code to get the graph showed in attachment, thank you so much!

 

Contributor
Posts: 74

Re: How to code a graph with sas

In addition, would you keep the four data boxes below as the attachment showed? Thank you so much!

Post a Question
Discussion Stats
  • 8 replies
  • 940 views
  • 2 likes
  • 3 in conversation