Hi Team,
Can you assist me in adding purple flag/ribbon in below plot.
Assuming this purple flag does not mean anything. Just for an example:
dm 'log;clear;odsresults;clear;';
proc datasets lib=work kill mt=data nolist nodetails nowarn;
quit;
/**************************************************************
Heatmap plot
**************************************************************/
data sample;
infile cards dlm=",";
input subjid :$10. drops week;
cards;
101,2,1
101,4,2
101,3,3
101,.,4
101,1,5
101,2,6
101,1,7
101,2,8
102,3,1
102,1,2
102,2,3
102,.,4
102,4,5
102,1,6
103,2,1
103,4,2
103,3,3
103,4,4
103,1,5
103,2,6
103,3,7
103,2,8
104,2,1
104,4,2
104,3,3
104,.,4
104,.,5
104,1,6
;
run;
data flag;
zero=0;
do week2=0 to 8.5 by 0.5;
y=(9-week2)/9;output;
end;
run;
data sample;
length colormodel $20.;;
set sample flag;
if drops ne . then dropc = strip(put(drops,??best.));
else dropc="0";
if drops=. then drops=0;
if drops=4 then colormodel="CX004080";
else if drops=3 then colormodel="CX3399CC";
else if drops=2 then colormodel="CX66B2CC";
else if drops=1 then colormodel="CX99CCDD";
else if drops in (.,0) then colormodel="CXFFFFFF";
run;
proc sort data=sample out=attrid nodupkey;
by drops;
where drops ne .;
run;
data attrid;
set attrid;
id="dps";
value=drops;
fillcolor=colormodel;
run;
title1 j=c "Headmap Plot";
title2 j=c "Practice plot2";
title3 " ";
ods graphics on / border=off width=24cm height=11cm;
proc sgplot data=sample noautolegend nowall dattrmap=attrid;
heatmapparm x=week y=subjid
colorgroup=drops/outlineattrs=(color=gray) name="dps" transparency=0.1 x2axis attrid=dps;
text x=week y=subjid text=dropc / x2axis textattrs=(size=8pt weight=bold);
band x=week2 lower=zero upper=y/y2axis fill fillattrs=(color=purple) transparency=0.6;
y2axis display=none offsetmin=0.82 offsetmax=0.02;
yaxis label="Subject" offsetmin=0.3
labelpos=center reverse;
x2axis values=(1,2,3,4,5,6,7,8) label="Week" offsetmin=0.06 offsetmax=0.06;
xaxis display=none ;
keylegend "dps"/noborder title="Number of drops per week" sortorder=descending;
run;
Example data in the form of a working data step and the code used to create the plot you have is a good way to ask a question. Without your code we don't know the options that may be available in the procedure to add stuff.
Where would the purple flag go?
It looks like you have a discrete scale of colors. If so, use the KEYLEGEND statement in PROC SGPLOT. If you have a continuous color ramp, use the GRADLEGEND statement. For an example, see
Hi Team,
Please find enclosed piece of code and output. However, in layout if you observed there is a purple/red flag above the subject/pid. It is the scheme of drug administered (in no. of drops) from week 1 to week 22. I'm unable to add that flag to the plot.
Assuming this purple flag does not mean anything. Just for an example:
dm 'log;clear;odsresults;clear;';
proc datasets lib=work kill mt=data nolist nodetails nowarn;
quit;
/**************************************************************
Heatmap plot
**************************************************************/
data sample;
infile cards dlm=",";
input subjid :$10. drops week;
cards;
101,2,1
101,4,2
101,3,3
101,.,4
101,1,5
101,2,6
101,1,7
101,2,8
102,3,1
102,1,2
102,2,3
102,.,4
102,4,5
102,1,6
103,2,1
103,4,2
103,3,3
103,4,4
103,1,5
103,2,6
103,3,7
103,2,8
104,2,1
104,4,2
104,3,3
104,.,4
104,.,5
104,1,6
;
run;
data flag;
zero=0;
do week2=0 to 8.5 by 0.5;
y=(9-week2)/9;output;
end;
run;
data sample;
length colormodel $20.;;
set sample flag;
if drops ne . then dropc = strip(put(drops,??best.));
else dropc="0";
if drops=. then drops=0;
if drops=4 then colormodel="CX004080";
else if drops=3 then colormodel="CX3399CC";
else if drops=2 then colormodel="CX66B2CC";
else if drops=1 then colormodel="CX99CCDD";
else if drops in (.,0) then colormodel="CXFFFFFF";
run;
proc sort data=sample out=attrid nodupkey;
by drops;
where drops ne .;
run;
data attrid;
set attrid;
id="dps";
value=drops;
fillcolor=colormodel;
run;
title1 j=c "Headmap Plot";
title2 j=c "Practice plot2";
title3 " ";
ods graphics on / border=off width=24cm height=11cm;
proc sgplot data=sample noautolegend nowall dattrmap=attrid;
heatmapparm x=week y=subjid
colorgroup=drops/outlineattrs=(color=gray) name="dps" transparency=0.1 x2axis attrid=dps;
text x=week y=subjid text=dropc / x2axis textattrs=(size=8pt weight=bold);
band x=week2 lower=zero upper=y/y2axis fill fillattrs=(color=purple) transparency=0.6;
y2axis display=none offsetmin=0.82 offsetmax=0.02;
yaxis label="Subject" offsetmin=0.3
labelpos=center reverse;
x2axis values=(1,2,3,4,5,6,7,8) label="Week" offsetmin=0.06 offsetmax=0.06;
xaxis display=none ;
keylegend "dps"/noborder title="Number of drops per week" sortorder=descending;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.