Hello!
I am attempting to make my first ever graph in SAS. I'd like for my graph to look like this:
The code I have so far is:
proc sgplot data=have;
where deathcategory_4 = "CVD";
title 'Number of Deaths by Category Aged 25 and Older by 10-Year Intervals';
vbar agegroup / group=year groupdisplay=cluster;
xaxis display=(nolabel);
yaxis grid;
run;
and it is producing:
I've been googling, but I'm just not getting it. I'd like to:
1.) Move the <25 group to the far left
2.) Add year intervals underneath each cluster like in the reference photo
3.) Make the bars grey
Any help would be very greatly appreciated!
1.) Move the <25 group to the far left
I have to assume that you created a character variable to hold the group values. Which then sorted from left to right alphabetically. The < character is after all of the numeric values.
One way would be add a Values=( ) with the list of values in the parentheses to the XAXIS statement.
Another is to leave the data as numeric and create a custom format with the display text you want. The underlying numeric values are used for the order. This would require Proc Format to create the format and a Format statement for you age variable. Note that the groups create by formats are honored by almost all procedures and is much more flexible than adding lots of text variables based on single varailbe values.
3.) Make the bars grey
Add the option FILLATTRS=(color=grey) to the VBAR statement. If the grey isn't quite what you want try prefixing with "light" or "dark", or even "verylight" or "verydark".
There are multiple ways to provide colors so delve into that if interested.
2.) Add year intervals underneath each cluster like in the reference photo
Not trivial and not designed for SGPLOT.
https://support.sas.com/kb/39/166.html shows one way to put groups and value on an axis.
https://support.sas.com/kb/39/100.html a different way.
With as many values as you may be using you may want to consider a modified NEEDLE instead of VBAR.
@smg3141 wrote:
Hello!
I am attempting to make my first ever graph in SAS. I'd like for my graph to look like this:
The code I have so far is:
proc sgplot data=have;
where deathcategory_4 = "CVD";
title 'Number of Deaths by Category Aged 25 and Older by 10-Year Intervals';
vbar agegroup / group=year groupdisplay=cluster;
xaxis display=(nolabel);
yaxis grid;
run;
and it is producing:
I've been googling, but I'm just not getting it. I'd like to:
1.) Move the <25 group to the far left
2.) Add year intervals underneath each cluster like in the reference photo
3.) Make the bars grey
Any help would be very greatly appreciated!
@smg3141 wrote:
Thank you! It there a different plot besides sgplot you would recommend for number 3?
I think you mean for number 2.
For anything with large numbers of columns that don't need/want data labels I tend to look at Needle plots of one sort or another as I mentioned. You pre-calculate the Y value. Groupdisplay=cluster and clusterwidth= . I might also suggest SGPANEL. Summarize the data by agegroup and year to get the needle height. Use Panelby AGEGROUP. Needle would use x=year y=summarized value. Noborder option on the Panelby suppresses the border between graphs. Colheaderpos=bottom would place the agegroup value below each graph. Your Xaxis statement would control which/how many of the year values get labels/ticks.
title 'xxxxxxxxxxxx'; footnote 'zzzzzzzzzzzzzzz'; proc sgpanel data=sashelp.stocks noautolegend; panelby stock/onepanel rows=1 layout=COLUMNLATTICE NOVARNAME noborder nowall NOHEADERBORDER COLHEADERPOS=bottom HEADERBACKCOLOR=white ; needle x=date y=close/lineattrs=(color=gray); colaxis label=' '; run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.