I am trying to use ODS Graphics Designer to create the following graph:
I tried to use the "Block" plot, but it seems to only allow two colors: missing/not missing or multi-colors with no control over color assignment. Is there a way to create such a plot using ODS Graphics? I would prefer using designer since I need to stack a lot of graphs, but it's not a requirement.
Yes, this can be done; but can you tell more about your plot axis? Is it continuous or discrete? Do you want each of your block plots to be labeled?
Data is discrete and stored numerically: 1 (mild), 2 (moderate), 3 (severe), 0 (none), 5 (not collected). I do not need the boxes labelled with the values nor axis labelled as this plot will be under a scatter plot with the same axis.
Normally, I would use PROC SGPLOT to do this; but for your case, I needed the REPEATEDVALUES option on the BLOCK plot, which is not currently surfaced in the SG procedures. To control the value/color association, modify the attrmap data set. To add more BLOCK plots, just add them inside the INNERMARGIN to stack them.
Hope this helps!
Dan
proc template;
define statgraph example;
begingraph;
layout overlay / xaxisopts=(discreteOpts=(tickvaluefitpolicy=SplitRotate));
ScatterPlot X=Name Y=Weight / primary=true;
InnerMargin / align=bottom;
BlockPlot X=Name Block=age / Display=( Fill Outline Label ) NAME="block" repeatedvalues=true;
EndInnerMargin;
DiscreteLegend "block" / Location=Outside;
endlayout;
endgraph;
end;
run;
data attrmap;
retain ID "myid";
length fillcolor $ 6;
input value $ fillcolor $;
cards;
12 blue
13 green
14 yellow
15 orange
16 red
;
run;
proc sgrender data=sashelp.class template=example dattrmap=attrmap;
dattrvar age="myid";
run;
If you are willing to eliminate the gap between each row, this can be done by using a heat map. For example, see the article, "Create a discrete heat map with PROC SGPLOT"
You can use a discrete attribute map to assign colors to values.
data Clinical;
input SiteID @;
do Week = 0 to 13;
input Count @;
output;
end;
/* ID Wk1 Wk2 Wk3 Wk4 ... Wk14*/
datalines;
001 1 0 0 0 0 0 0 3 1 3 3 0 3 0
002 0 0 0 1 1 2 1 2 2 1 1 0 2 2
003 1 . . 1 0 1 0 3 . 1 0 3 2 1
004 1 1 . 1 0 1 2 2 3 2 1 0 . 0
005 1 1 1 . 0 0 0 1 0 1 2 3 3 1
;
/* https://blogs.sas.com/content/iml/2019/07/15/create-discrete-heat-map-sgplot.html */
data DiscreteAttrOrder; /* create discrete attribute map */
length Value $1 FillColor $15;
input Value FillColor;
retain ID 'Malaria' /* name of map */
Show 'AttrMap'; /* always show all groups in legend */
datalines;
. Gray
0 White
1 CXFFFFB2
2 CXFD8D3C
3 CXBD0026
;
title "Heat Map of Malaria Data";
proc sgplot data=Clinical DATTRMAP=DiscreteAttrOrder;
heatmapparm x=Week y=SiteID colorgroup=Count / outline outlineattrs=(color=gray)
ATTRID=Malaria;
discretelegend;
refline (1.5 to 5.5) / axis=Y lineattrs=(color=black thickness=2);
xaxis integer values=(0 to 13) valueshint;
run;
I wrote more details and comments about using formats to bin numeric data, discrete attribute maps, and using heat maps for longitudinal data: https://blogs.sas.com/content/iml/2022/04/20/viz-ordinal-response-longitudinal.html
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.