Solved
Contributor
Posts: 23

# Question about bar graph ?

Hi everyone,

I've been trying to plot some bar graphs with the following data :

 Year Age Immigrants Non-Immigrants 1991 < 1 420 96430 1991 1 - 4 5410 341025 1991 5 - 9 13690 432140 1991 10 - 14 19275 462460 1991 15 - 19 25120 424565 1991 20 - 29 80015 973205 1991 30 - 39 109120 1109270 1991 40 - 49 116935 870385 1991 50 - 59 89810 585945 1991 60 - 69 74815 498835 1991 70 - 79 37615 288155 1991 80 - 89 16860 85070 1991 >= 90 2120 7635

I want to plot 2 bar graphs (should look likes histograms) in the same figure to show the difference between 2 groups. Here are the codes with PROC SGSCATTER:

proc sgscatter data = population;

plot (Immigrants Non_immigrants)* Age / rows=2 columns=1 uniscale=all;

where year = 1991;

title "Scatter plots of age for Immigrants vs. Non-immigrants";

run;

I'd really appreciate any help/suggestion. Thanks in advance.

Accepted Solutions
Solution
‎11-08-2014 10:23 AM
Super Contributor
Posts: 543

## Re: Question about bar graph ?

W/o the statistics...

proc template;

define statgraph two_bars_again;

dynamic _panelnumber_;

begingraph / designwidth=1222 designheight=845;

layout datalattice rowvar=GROUP / cellwidthmin=1 cellheightmin=1 rowgutter=3 columngutter=3 rowdatarange=unionall row2datarange=unionall columndatarange=unionall column2datarange=unionall headerlabeldisplay=value;

layout prototype / ;

barchart x=AGE y=POPULATION / name='bar' barlabel=true barwidth=0.85 clusterwidth=0.85;

endlayout;

endlayout;

endgraph;

end;

run;

proc sgrender data=WORK.QUEBEC_POP template=two_bars_again;

run;

I stop here.

All Replies
SAS Super FREQ
Posts: 4,242

## Re: Question about bar graph ?

First transpose the data from wide to long so that there is a new column named "Group" with the values "Immigrants" and "Non-Immigrants." Then use PROC SGPLOT and specify

vbar age / group=Group  groupdisplay=cluster;

Contributor
Posts: 23

## Re: Question about bar graph ?

Thanks for your suggestion.

I did transpose the data into long format :

 Year Age Population Group 1991 < 1 420 Immigrants 1991 1 - 4 5410 Immigrants 1991 5 - 9 13690 Immigrants 1991 10 - 14 19275 Immigrants 1991 15 - 19 25120 Immigrants 1991 20 - 29 80015 Immigrants 1991 30 - 39 109120 Immigrants 1991 40 - 49 116935 Immigrants 1991 50 - 59 89810 Immigrants 1991 60 - 69 74815 Immigrants 1991 70 - 79 37615 Immigrants 1991 80 - 89 16860 Immigrants 1991 >= 90 2120 Immigrants 1991 < 1 96430 Non-immigrants 1991 1 - 4 341025 Non-immigrants 1991 5 - 9 432140 Non-immigrants 1991 10 - 14 462460 Non-immigrants 1991 15 - 19 424565 Non-immigrants 1991 20 - 29 973205 Non-immigrants 1991 30 - 39 1109270 Non-immigrants 1991 40 - 49 870385 Non-immigrants 1991 50 - 59 585945 Non-immigrants 1991 60 - 69 498835 Non-immigrants 1991 70 - 79 288155 Non-immigrants 1991 80 - 89 85070 Non-immigrants 1991 >= 90 7635 Non-immigrants

But when I use your code:

title "Bar graphs of age for Immigrants vs. Non-immigrants";

proc sgplot data = quebec_pop;

vbar Age / group=Group groupdisplay=cluster;

where year = 1991;

run;

it didn't really work. Here is the result:

"<1" should be in the first column and I don't want 2 bar graphs together, I want 2 separated graph as in the example code I provided in OP (rows=2 columns=1).

Thanks.

Super Contributor
Posts: 543

## Re: Question about bar graph ?

Hi,

How about a little GTL? I think it may accomplish what you need:

data population;

input Year 1-4    Age \$9-16     Immigrants    Non_Immigrants;

datalines;

1991    < 1        420    96430

1991    1 - 4    5410    341025

1991    5 - 9    13690    432140

1991    10 - 14    19275    462460

1991    15 - 19    25120    424565

1991    20 - 29    80015    973205

1991    30 - 39    109120    1109270

1991    40 - 49    116935    870385

1991    50 - 59    89810    585945

1991    60 - 69    74815    498835

1991    70 - 79    37615    288155

1991    80 - 89    16860    85070

1991    >= 90    2120    7635

;

proc template;

define statgraph twoBars;

begingraph / designwidth=1428 designheight=936;

layout lattice / rowdatarange=data columndatarange=data rowgutter=10 columngutter=10;

layout overlay / xaxisopts=( discreteopts=( tickvaluefitpolicy=splitrotate)) yaxisopts=( linearopts=( viewmin=0.0 viewmax=1200000.0)) y2axisopts=( linearopts=( viewmin=0.0 viewmax=1200000.0)); *-- to mbe the same, so you ake the two groups comparable;

barchart category=AGE response=IMMIGRANTS / name='bar' stat=mean barwidth=0.5 groupdisplay=Cluster clusterwidth=1.0 fillattrs=(color=CX8CA6CE );

barchart category=AGE response=NON_IMMIGRANTS / name='bar2' yaxis=Y2 stat=mean barwidth=0.5 discreteoffset=0.5 groupdisplay=Cluster clusterwidth=1.0 grouporder=data;

discretelegend 'bar' 'bar2' / opaque=false border=true halign=left valign=top displayclipped=true across=1 order=rowmajor location=inside;

endlayout;

endlayout;

endgraph;

end;

run;

proc sgrender data = POPULATION template=twoBars;

run;

Good luck!

Anca.

Message was edited by: Anca tilea I added y-axis range of values to be the same, so you can compare the groups.

Contributor
Posts: 23

## Re: Question about bar graph ?

Posted in reply to AncaTilea

Thansk very much Anca !!!

I tried your code but I have to change a few minor things to make it work :

• change tickvaluefitpolicy=splitrotate => tickvaluefitpolicy=rotate;
• change category=AGE response=IMMIGRANTS => x=AGE y=IMMIGRANTS;
• category=AGE response=NON_IMMIGRANTS   => x=AGE y=NON_IMMIGRANTS.

As I said in previous post, is it possible to divide them into 2 separated bar graphs (2 rows, 1 column) ? This example of histogram below is what I've been trying to accomplish (of course without the descriptive statitstics):

Another thought, is it possible to display count/frequency on top of each bar as shown in the histogram ? Thanks.

Super Contributor
Posts: 543

## Re: Question about bar graph ?

ok.

(this is using the long dataset, with the group variable)

(but this is not what you want, either)

(or maybe it is)

proc capability data = quebec_pop noprint;

comphist Population / class = group  barlabel = count

nrows = 2

cfill = greydd

normal(color = red)

;

inset min max mean/ format=6.2 height = 2.5 position = NE;

run;

Solution
‎11-08-2014 10:23 AM
Super Contributor
Posts: 543

## Re: Question about bar graph ?

W/o the statistics...

proc template;

define statgraph two_bars_again;

dynamic _panelnumber_;

begingraph / designwidth=1222 designheight=845;

layout datalattice rowvar=GROUP / cellwidthmin=1 cellheightmin=1 rowgutter=3 columngutter=3 rowdatarange=unionall row2datarange=unionall columndatarange=unionall column2datarange=unionall headerlabeldisplay=value;

layout prototype / ;

barchart x=AGE y=POPULATION / name='bar' barlabel=true barwidth=0.85 clusterwidth=0.85;

endlayout;

endlayout;

endgraph;

end;

run;

proc sgrender data=WORK.QUEBEC_POP template=two_bars_again;

run;

I stop here.

SAS Super FREQ
Posts: 1,278

## Re: Question about bar graph ?

Use SGPLOT for cluster bar chart.  Use SGPANEL for panel.

In my opinion, side-by-side comparisons is easier with cluster bar chart.

data population_grp;

drop Immigrants    Non_Immigrants;

input Year 1-4    Age \$9-16     Immigrants    Non_Immigrants;

Group='Immigrants'; Population=Immigrants; output;

Group='Non_Immigrants'; Population=Non_Immigrants;  output;

datalines;

1991    < 1        420    96430

1991    1 - 4    5410    341025

1991    5 - 9    13690    432140

1991    10 - 14    19275    462460

1991    15 - 19    25120    424565

1991    20 - 29    80015    973205

1991    30 - 39    109120    1109270

1991    40 - 49    116935    870385

1991    50 - 59    89810    585945

1991    60 - 69    74815    498835

1991    70 - 79    37615    288155

1991    80 - 89    16860    85070

1991    >= 90    2120    7635

;

run;

title "Bar graphs of age for Immigrants vs. Non-immigrants";

ods graphics / reset width=6in height=4in;

proc sgplot data = population_grp;

vbar Age / group=group response=Population groupdisplay=cluster nostatlabel;

xaxis fitpolicy=none discreteorder=data valueattrs=(size=8);

run;

ods graphics / reset width=6in height=4in;

title "Bar graphs of age for Immigrants vs. Non-immigrants";

proc sgpanel data = population_grp;

panelby group / layout=rowlattice rows=2 onepanel novarname;

vbar Age / response=Population groupdisplay=cluster nostatlabel;

colaxis fitpolicy=none discreteorder=data;

rowaxis offsetmin=0;

run;

Contributor
Posts: 23

## Re: Question about bar graph ?

Posted in reply to Sanjay_SAS

Sorry for the late post. I've been busy these days.

Thanks Anca and Sanjay for your answers. For me, Sanjay's suggestion is easier to understand and execute. But in another hand, proc template + proc sgrender by Anca is more useful if we want to mix our graph with different types of panel.

Many thanks.

Super Contributor
Posts: 543

## Re: Question about bar graph ?

One more thought,

To make suggestion work, you need to add a response variable.

proc sgplot data = quebec_pop;

vbar Age / group=Group groupdisplay=cluster  response = population;

where year = 1991;

run;

Anca.

SAS Employee
Posts: 1,049

## Re: Question about bar graph ?

🔒 This topic is solved and locked.

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

Discussion stats
• 10 replies
• 535 views
• 6 likes
• 5 in conversation