BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Anv_SAS
Fluorite | Level 6

Hi Team,

 

Can you assist me in adding purple flag/ribbon in below plot.

WhatsApp Image 2025-11-03 at 8.51.29 PM.jpeg

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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;

Ksharp_0-1762501495393.png

 

View solution in original post

8 REPLIES 8
ballardw
Super User

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? 

Rick_SAS
SAS Super FREQ

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

Create a wind chill chart in SAS - The DO Loop

Ksharp
Super User
And what is your desired output format ?
Excel ,PDF or just a Graph ?
Anv_SAS
Fluorite | Level 6

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.

Ksharp
Super User
You just show us there is a purple flag, but didn't tell us how to get it.
Does this flag stand for anything ?
Ksharp
Super User

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;

Ksharp_0-1762501495393.png

 

Anv_SAS
Fluorite | Level 6
Thanks Ksharp, For you quick response. The logic and band statement portrait looks good.

Thanks once again.
How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 804 views
  • 2 likes
  • 4 in conversation