BookmarkSubscribeRSS Feed
Calcite | Level 5

## How to make grouped bar chart

I'm new to SAS and have been struggling with learning it. Hopefully someone can help me with a task.

I'm trying to make a grouped bar chart. So I have a dataset with the following information:

idNumber: id number of the student

gradeLevel: grade in high school... takes values of freshman, sophomore, junior, and senior.

plansToGoToCollege: binary indicator indicating whether the student plans to go to college (1 if yes,0 if no).

dataset is called collegeSurvey

I'm trying to make a bar charrt plotting the 0's and 1's of the plansToGoToCollege variable segmented by gradeLevel. Something that looks kinda like the picture below (which is just a random picture I found from google).

So I'd have 4 different x values (fresh, sophomore, junior, and senior)... each x value has 2 bars (0 and 1), and the y axis would measure the frequency of the occurrence.

gradeLevel is of type character

plansToGoToCollege is numeric

12 REPLIES 12
Super User

## Re: How to make grouped histogams

I think you're looking for something similar to:

```proc sgplot data=collegesurvey;
vbar gradelevel /group=planstogotocollege groupdisplay=cluster;
run;```

You might want bookmark this as one source of graphs with pictures and example SAS code including data.

http://support.sas.com/sassamples/graphgallery/index.html

Calcite | Level 5

## Re: How to make grouped histogams

thanks! I did that and it plotted the frequencies.... I'm wondering if theres a way change that to percentages. So if there are 150 0's and 50 1's for freshman, how do I change it do 75% and 25%?

Super User

## Re: How to make grouped histogams

Calcite | Level 5

## Re: How to make grouped histogams

type wont show up as option though

Super User

## Re: How to make grouped histogams

@toesockshoe wrote:

type wont show up as option though

I don't know what that means.

Calcite | Level 5

## Re: How to make grouped histogams

Here is the error I get

ERROR 22-322: Syntax error, expecting one of the following: ;, ALPHA, ATTRID, BARWIDTH, BASELINE, BASELINEATTRS, CATEGORYORDER,

CLUSTERWIDTH, COLORMODEL, COLORRESPONSE, COLORSTAT, DATALABEL, DATALABELATTRS, DATALABELFITPOLICY, DATALABELPOS,

DATASKIN, DISCRETEOFFSET, FILL, FILLATTRS, FILLTYPE, FREQ, GROUP, GROUPDISPLAY, GROUPORDER, LEGENDLABEL, LIMITATTRS,

LIMITS, LIMITSTAT, MISSING, NAME, NOFILL, NOOUTLINE, NOSTATLABEL, NOZEROBARS, NUMSTD, OUTLINE, OUTLINEATTRS, RATTRID,

RESPONSE, SEGLABEL, SEGLABELATTRS, SEGLABELFITPOLICY, SEGLABELFORMAT, SPLITCHAR, SPLITCHARNODROP, STAT, STATLABEL,

TIP, TIPFORMAT, TIPLABEL, TRANSPARENCY, URL, WEIGHT, X2AXIS, Y2AXIS.

ERROR 76-322: Syntax error, statement will be ignored.

Super User

## Re: How to make grouped histogams

Full code and log please. My suggestion was to change the TYPE= from the original code posted by Rob Allison.

@toesockshoe wrote:

Here is the error I get

ERROR 22-322: Syntax error, expecting one of the following: ;, ALPHA, ATTRID, BARWIDTH, BASELINE, BASELINEATTRS, CATEGORYORDER,

CLUSTERWIDTH, COLORMODEL, COLORRESPONSE, COLORSTAT, DATALABEL, DATALABELATTRS, DATALABELFITPOLICY, DATALABELPOS,

DATASKIN, DISCRETEOFFSET, FILL, FILLATTRS, FILLTYPE, FREQ, GROUP, GROUPDISPLAY, GROUPORDER, LEGENDLABEL, LIMITATTRS,

LIMITS, LIMITSTAT, MISSING, NAME, NOFILL, NOOUTLINE, NOSTATLABEL, NOZEROBARS, NUMSTD, OUTLINE, OUTLINEATTRS, RATTRID,

RESPONSE, SEGLABEL, SEGLABELATTRS, SEGLABELFITPOLICY, SEGLABELFORMAT, SPLITCHAR, SPLITCHARNODROP, STAT, STATLABEL,

TIP, TIPFORMAT, TIPLABEL, TRANSPARENCY, URL, WEIGHT, X2AXIS, Y2AXIS.

ERROR 76-322: Syntax error, statement will be ignored.

Calcite | Level 5

## Re: How to make grouped histogams

here is my code:

proc sgplot data=collegesurvey;
vbar gradelevel /group=planstogotocollege groupdisplay=cluster;
run;

uhh I don't think I can give you the full log... there are some variable names that are proprietary... the example I gave above is a madeup example.

Super User

## Re: How to make grouped histogams

The example provided was GCHART and the solution suggested aligned with GCHART. You are using SGPLOT instead, and the option within SGPLOT is STAT=PERCENT.

http://documentation.sas.com/?docsetId=grstatproc&docsetTarget=n0p7vdd69sgf3wn1479qxqxuryrt.htm&docs...

@toesockshoe wrote:

here is my code:

proc sgplot data=collegesurvey;
vbar gradelevel /group=planstogotocollege groupdisplay=cluster;
run;

uhh I don't think I can give you the full log... there are some variable names that are proprietary... the example I gave above is a madeup example.

Calcite | Level 5

## Re: How to make grouped histogams

ok thanks... ill keep that in mind for future posts!

Super User

## Re: How to make grouped histogams

There are multiple ways, see the posts below for example code with the graphs so you can see what matches up to what you're attempting.

https://blogs.sas.com/content/iml/2016/03/09/comparative-panel-overlay-histograms-sas.html

https://blogs.sas.com/content/graphicallyspeaking/2017/04/30/getting-started-with-sgplot-histograms/

@toesockshoe wrote:

I'm new to SAS and have been struggling with learning it. Hopefully someone can help me with a task.

I'm trying to make a grouped histogram. So I have a dataset with the following information:

idNumber: id number of the student

gradeLevel: grade in high school... takes values of freshman, sophomore, junior, and senior.

plansToGoToCollege: binary indicator indicating whether the student plans to go to college (1 if yes,0 if no).

dataset is called collegeSurvey

I'm trying to make a histogram plotting the 0's and 1's of the plansToGoToCollege variable segmented by gradeLevel. Something that looks kinda like the picture below (which is just a random picture I found from google).

So I'd have 4 different x values (fresh, sophomore, junior, and senior)... each x value has 2 bars (0 and 1), and the y axis would measure the frequency of the occurrence.

gradeLevel is of type character

plansToGoToCollege is numeric

Meteorite | Level 14

## Re: How to make grouped histogams

Here's one way to do it, using a grouped bar chart, created with Proc Gchart:

Here, I'm just creating some "random data" similar to the data you describe, since I don't have your real data:

data collegeSurvey (keep = idNumber gradelevel plansToGoToCollege);
do loop = 1 to 1000;
idNumber=round(ranuni(123)*10000);
length grade_level \$10;
gradelevel_num=round(ranuni(123)*4);
if gradelevel_num=1 then gradelevel='freshman';
if gradelevel_num=2 then gradelevel='sophmore';
if gradelevel_num=3 then gradelevel='junior';
if gradelevel_num=4 then gradelevel='senior';
if ranuni(123)>.7 then plansToGoToCollege=1;
else plansToGoToCollege=0;
output;
end;
run;

And this is how I generate the plot:

pattern1 v=s c=pink;
pattern2 v=s c=lime;

axis1 label=none order=('freshman' 'sophmore' 'junior' 'senior') offset=(2,2);
axis2 label=('Count') major=none minor=none style=0;
axis3 label=none value=none;

legend1 shape=bar(.15in,.15in);

proc gchart data=collegeSurvey;
vbar plansToGoToCollege / discrete type=freq
group=gradelevel
gaxis=axis1 raxis=axis2 maxis=axis3 noframe
autoref clipref cref=graydd legend=legend1
subgroup=plansToGoToCollege /* controls color */
coutline=gray77
space=0;

run;

Discussion stats
• 12 replies
• 1910 views
• 4 likes
• 4 in conversation