Data visualization with SAS programming

Need assistance replicating a bar chart

Accepted Solution Solved
Reply
Occasional Contributor yo1
Occasional Contributor
Posts: 19
Accepted Solution

Need assistance replicating a bar chart

[ Edited ]

Hello everyone,

 

I wanted to ask for some help with graphs. Specifically bar charts. I am trying to replicate a chart that is normally done in excel. I have made some efforts using PROC GCHART but I did not come close, only in shape of the bar chart and grouping.

The image I am trying to replicate is attached and is called ORGINAL1.jpg

 

 

Here is the table I am using

data chartexample;

input year $9. FCPIXG FMIXG FOIXG FOEXG;

datalines;

1987-2015 16.5 6.9 2.7 2.3

1987-1990 18.7 5.7 2.6 1.3

1990-1995 15.8 5.2 3.7 2.1

1995-2000 40.7 10.9 3.4 4.5

2000-2007 13.7 8.5 2.6 2.7

2007-2015 5.4 4.6 1.6 1.2

;

run;

 

This is the code I used to attempt the replication.

proc sort data=chart4 out=yo;

by year; run;

 

 

proc transpose data=yo out=chrto;

by year;

var FCPIXG FMIXG FOIXG FOEXG;

run;

title 'Chart 4. Types of capital equipment in the private nonfarm business sector, 1987- 20XX ';

proc gchart data=chrto;                                                                                                                

   vbar _NAME_ / noframe sumvar=col1 group=year                                                                            

            

legend = legend1

coutline=same ;

run ;

quit ;  

 

The output graph is attached and is called IMAGE1.jpg

 

Any assistance (links, websites) would be greatly appreciated.

 

Best,

 

Attachment
Attachment

Accepted Solutions
Solution
3 weeks ago
Super Contributor
Posts: 283

Re: Need assistance replicating a bar chart

I'm not clear on which elements you are looking to replicate and how exactly you want to replicate them, but the SGPLOT below gets you the general type of graph you want. Also, I did not know how to map your variables to the descriptions, so you'll want to make sure that's correct. Otherwise, take a look and then see what elements need to be fixed. If you cannot figure out how to fix them, I'll be happy to see if I know how.

 

 

data chartexample;
input year $9. FCPIXG FMIXG FOIXG FOEXG;
datalines;
1987-2015 16.5 6.9 2.7 2.3
1987-1990 18.7 5.7 2.6 1.3
1990-1995 15.8 5.2 3.7 2.1
1995-2000 40.7 10.9 3.4 4.5
2000-2007 13.7 8.5 2.6 2.7
2007-2015 5.4 4.6 1.6 1.2
;

proc sort data=chartexample out = chartexample2;
    by year; 
run;

proc transpose data=chartexample2 out=chartexample3(rename=(_name_ = eqType)) ;
    by year;
    var FCPIXG FMIXG FOIXG FOEXG;
run;

proc format;
    value $eq
    'FCPIXG' = 'Computers & related...'
    'FMIXG' = 'Communication...'
    'FOIXG' = 'Other information...'
    'FOEXG' = 'All other...';
run;

proc sgplot data = chartexample3 noautolegend;
    title 'Chart 4. Types of capital equipment in the private nonfarm business sector, 1987- 20XX ';
    format eqType $eq.;
    vbar year / group = eqType response = col1 groupdisplay = cluster name = 'yr';
    xaxis display=(nolabel);
    yaxis display=(nolabel);
    keylegend 'yr' / noborder across = 1;
run;

View solution in original post


All Replies
Solution
3 weeks ago
Super Contributor
Posts: 283

Re: Need assistance replicating a bar chart

I'm not clear on which elements you are looking to replicate and how exactly you want to replicate them, but the SGPLOT below gets you the general type of graph you want. Also, I did not know how to map your variables to the descriptions, so you'll want to make sure that's correct. Otherwise, take a look and then see what elements need to be fixed. If you cannot figure out how to fix them, I'll be happy to see if I know how.

 

 

data chartexample;
input year $9. FCPIXG FMIXG FOIXG FOEXG;
datalines;
1987-2015 16.5 6.9 2.7 2.3
1987-1990 18.7 5.7 2.6 1.3
1990-1995 15.8 5.2 3.7 2.1
1995-2000 40.7 10.9 3.4 4.5
2000-2007 13.7 8.5 2.6 2.7
2007-2015 5.4 4.6 1.6 1.2
;

proc sort data=chartexample out = chartexample2;
    by year; 
run;

proc transpose data=chartexample2 out=chartexample3(rename=(_name_ = eqType)) ;
    by year;
    var FCPIXG FMIXG FOIXG FOEXG;
run;

proc format;
    value $eq
    'FCPIXG' = 'Computers & related...'
    'FMIXG' = 'Communication...'
    'FOIXG' = 'Other information...'
    'FOEXG' = 'All other...';
run;

proc sgplot data = chartexample3 noautolegend;
    title 'Chart 4. Types of capital equipment in the private nonfarm business sector, 1987- 20XX ';
    format eqType $eq.;
    vbar year / group = eqType response = col1 groupdisplay = cluster name = 'yr';
    xaxis display=(nolabel);
    yaxis display=(nolabel);
    keylegend 'yr' / noborder across = 1;
run;
Occasional Contributor yo1
Occasional Contributor
Posts: 19

Re: Need assistance replicating a bar chart

Hi collinelliot,

Thank you for responding. Your code produced the answer I was looking for. I haven’t had much experience with SGPLOT but seeing the output generated is enough for me to start exploring. I need to do some additional formatting, (add the gridlines) but that shouldn’t be too much.

Thanks
SAS Employee
Posts: 967

Re: Need assistance replicating a bar chart

You were on the right track - you just needed to fine-tune it with a few more options ...

 

data chart4;
input year $9. FCPIXG FMIXG FOIXG FOEXG;
datalines;
1987-2015 16.5 6.9 2.7 2.3
1987-1990 18.7 5.7 2.6 1.3
1990-1995 15.8 5.2 3.7 2.1
1995-2000 40.7 10.9 3.4 4.5
2000-2007 13.7 8.5 2.6 2.7
2007-2015 5.4 4.6 1.6 1.2
;
run;

proc sort data=chart4 out=yo;
by year; run;


proc transpose data=yo out=chrto;
by year;
var FCPIXG FMIXG FOIXG FOEXG;
run;

 

axis1 label=none value=none;
axis2 label=none value=(angle=45) offset=(3,3);
axis3 label=none order=(0 to 45 by 4) major=none minor=none;
legend1 label=none across=1 shape=bar(.15in,.15in);
pattern1 v=s c=cx4f81be;
pattern2 v=s c=cxc5daef;
pattern3 v=s c=cx1f487c;
pattern4 v=s c=cx8cb4e5;

 

title1 j=l h=3pct 'Chart 4. Types of capital equipment in the';
title2 j=l h=3pct 'private nonfarm business sector, 1987- 20XX ';
title3 ' ';
title4 j=l move=(+4,+0) 'Average annual growth rates';
proc gchart data=chrto;
vbar _NAME_ / subgroup=_name_
noframe sumvar=col1 group=year
legend = legend1
maxis=axis1 gaxis=axis2 raxis=axis3 space=0
legend=legend1
autoref clipref cref=graydd
coutline=gray55;
run ;
quit ;

 

bar_grouped.png

Occasional Contributor yo1
Occasional Contributor
Posts: 19

Re: Need assistance replicating a bar chart

 

Hi Robert,

 

Thank you for responding. Your code produced the answer I was looking for.   I am actually a big fan of yours when it comes to creating geospatial heat maps. I usually go to your site(http://robslink.com/SAS/Home.htm) often for clues for the next great visualization project in SAS…

 

Many thanks.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 144 views
  • 2 likes
  • 3 in conversation