How do i display colors as below ( ihave attached my data and sample code where I am plotting median and p25 p75 stats values in this plot :
for res variable R-R - show green color solid until Ebase visit and green color dash line from Ebase to Ewk38
R-NR - show green color solid until Ebase visit and red color dash line from Ebase to Ewk38
NR-R - show red color solid until Ebase visit and green color dash line from Ebase to Ewk38
NR-NR - show redcolor solid until Ebase visit and redcolor dash line from Ebase to Ewk38
Hi @dsam
In order to change the color of a line within the line, you will need to create separate line segments for each section of the line that will be drawn with the same color.
A new data set variable would be created to identify each line segment and use this new variable as the GROUP= variable on the SERIES statement in PROC SGPLOT.
To associate the group values with the line colors and line patterns, you would define an attribute map.
To see a descriptive value in the legend, you would use PROC FORMAT to associate the new group variable with the text you want to see.
Below is a sample that demonstrates this approach.
Regards,
Marcia
proc format;
value avis
0= 'HBase'
1= 'HWk20'
2= 'HWk38'
3= 'EBase'
4= 'EWk20'
5= 'EWk38'
6= 'Early Termination';
value grpfmt
1="R-R to EBase" 2="R-R Ebase to EWK38"
3="R-NR to EBase" 4="R-NR Ebase to EWK38"
5="NR-R to EBase" 6="NR-R Ebase to EWK38"
7="NR-NR to EBase" 8="NR-NR Ebase to EWK38";
run;
data sampledata;
input aan hmedian res : $7. hp75 hp25;
datalines;
0 0.300 NR-NR 0.390 0.300
1 0.950 NR-NR 2.005 0.470
2 0.965 NR-NR 1.385 0.545
3 1.015 NR-NR 1.400 0.535
4 0.650 NR-NR 0.940 0.360
5 0.360 NR-NR 0.360 0.360
0 0.300 NR-R 0.300 0.300
1 3.100 NR-R 6.920 0.625
2 1.630 NR-R 4.670 0.650
3 2.430 NR-R 7.210 0.820
4 3.885 NR-R 10.865 1.145
5 5.230 NR-R 9.980 0.480
0 0.300 R-NR 0.300 0.300
1 2.040 R-NR 2.040 2.040
2 1.650 R-NR 1.650 1.650
3 1.830 R-NR 1.830 1.830
4 3.260 R-NR 3.260 3.260
5 3.070 R-NR 3.070 3.070
0 0.500 R-R 1.030 0.300
1 2.020 R-R 4.290 0.630
2 3.400 R-R 9.830 0.610
3 3.330 R-R 22.200 1.950
4 7.410 R-R 29.300 1.510
5 7.445 R-R 20.900 0.780
;
run;
proc sort data=sampledata;
by res ;
run;
data newsample;
set sampledata;
by res ;
retain grpstart;
drop grpstart;
if first.res then do;
select;
when (res='R-R') grpstart=1;
when (res='R-NR') grpstart=3;
when (res='NR-R') grpstart=5;
when (res='NR-NR') grpstart=7;
otherwise;
end;
end;
colorgrp=grpstart;
if aan=3 then output;
if aan >=3 and aan <=5 then colorgrp=grpstart+1;
output;
run;
data attrmap;
id='colors';
show='attrmap';
length linecolor linepattern $5;
input v linecolor $ linepattern $;
value=put(v,grpfmt.);
datalines;
1 green solid
2 green dash
3 green solid
4 red dash
5 red solid
6 green dash
7 red solid
8 red dash
;
run;
ods graphics / attrpriority=none;
proc sgplot data=newsample dattrmap=attrmap;
format aan avis. colorgrp grpfmt.;
title "Median and IQR Biomarker Plot for Qualifying subjects with var1 and var2";
series x=aan y=hmedian/ group=colorgrp attrid=colors name='lines';
Yaxis label="var2";
xaxis label="Analysis Visit";
keylegend 'lines' / title='';
run;
Many users here don't want to download Excel files because of virus potential, others have such things blocked by security software. Also if you give us Excel we have to create a SAS data set and due to the non-existent constraints on Excel data cells the result we end up with may not have variables of the same type (numeric or character) and even values.
Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the </> icon or attached as text to show exactly what you have and that we can test code against.
Also, paste code text into text box opened on the forum with the </> icon. Attachments require downloading and have the same concerns as spreadsheets.
proc format;
value avis
0= 'HBase'
1= 'HWk20'
2= 'HWk38'
3= 'EBase'
4= 'EWk20'
5= 'EWk38'
6= 'Early Termination';
value stu
1= 'Harmony'
2='Encore';
run;
proc sgplot data= dum2&var3&var4 ;
format aan avis. sno stu.;
title "Median and IQR Biomarker Plot for Qualifying subjects with &var1 and &var2";
series x=aan y=hmedian//*markers markerattrs= (symbol=circlefilled size=9)*/ group=res /*curvelabel*/;
Yaxis label="&var2";
xaxis label="Analysis Visit";
highlow x=aan high =hp75 low=hp25//*lineattrs=(thickness=2)*/ group=res;
run;
Hi @dsam
In order to change the color of a line within the line, you will need to create separate line segments for each section of the line that will be drawn with the same color.
A new data set variable would be created to identify each line segment and use this new variable as the GROUP= variable on the SERIES statement in PROC SGPLOT.
To associate the group values with the line colors and line patterns, you would define an attribute map.
To see a descriptive value in the legend, you would use PROC FORMAT to associate the new group variable with the text you want to see.
Below is a sample that demonstrates this approach.
Regards,
Marcia
proc format;
value avis
0= 'HBase'
1= 'HWk20'
2= 'HWk38'
3= 'EBase'
4= 'EWk20'
5= 'EWk38'
6= 'Early Termination';
value grpfmt
1="R-R to EBase" 2="R-R Ebase to EWK38"
3="R-NR to EBase" 4="R-NR Ebase to EWK38"
5="NR-R to EBase" 6="NR-R Ebase to EWK38"
7="NR-NR to EBase" 8="NR-NR Ebase to EWK38";
run;
data sampledata;
input aan hmedian res : $7. hp75 hp25;
datalines;
0 0.300 NR-NR 0.390 0.300
1 0.950 NR-NR 2.005 0.470
2 0.965 NR-NR 1.385 0.545
3 1.015 NR-NR 1.400 0.535
4 0.650 NR-NR 0.940 0.360
5 0.360 NR-NR 0.360 0.360
0 0.300 NR-R 0.300 0.300
1 3.100 NR-R 6.920 0.625
2 1.630 NR-R 4.670 0.650
3 2.430 NR-R 7.210 0.820
4 3.885 NR-R 10.865 1.145
5 5.230 NR-R 9.980 0.480
0 0.300 R-NR 0.300 0.300
1 2.040 R-NR 2.040 2.040
2 1.650 R-NR 1.650 1.650
3 1.830 R-NR 1.830 1.830
4 3.260 R-NR 3.260 3.260
5 3.070 R-NR 3.070 3.070
0 0.500 R-R 1.030 0.300
1 2.020 R-R 4.290 0.630
2 3.400 R-R 9.830 0.610
3 3.330 R-R 22.200 1.950
4 7.410 R-R 29.300 1.510
5 7.445 R-R 20.900 0.780
;
run;
proc sort data=sampledata;
by res ;
run;
data newsample;
set sampledata;
by res ;
retain grpstart;
drop grpstart;
if first.res then do;
select;
when (res='R-R') grpstart=1;
when (res='R-NR') grpstart=3;
when (res='NR-R') grpstart=5;
when (res='NR-NR') grpstart=7;
otherwise;
end;
end;
colorgrp=grpstart;
if aan=3 then output;
if aan >=3 and aan <=5 then colorgrp=grpstart+1;
output;
run;
data attrmap;
id='colors';
show='attrmap';
length linecolor linepattern $5;
input v linecolor $ linepattern $;
value=put(v,grpfmt.);
datalines;
1 green solid
2 green dash
3 green solid
4 red dash
5 red solid
6 green dash
7 red solid
8 red dash
;
run;
ods graphics / attrpriority=none;
proc sgplot data=newsample dattrmap=attrmap;
format aan avis. colorgrp grpfmt.;
title "Median and IQR Biomarker Plot for Qualifying subjects with var1 and var2";
series x=aan y=hmedian/ group=colorgrp attrid=colors name='lines';
Yaxis label="var2";
xaxis label="Analysis Visit";
keylegend 'lines' / title='';
run;
Thank you so much! This makes sense and thank you for the code. I appreciate your help so much.
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.