Hi all,
I'd like to specfiy different colors for a barchart based on nsubjects(no.of subjects) and grades at different timepoints . For example, pain with grade1 at day 1 pm should be blue color and pain with grade2 at day 1pm should be green color. Here is the code and data I've used so to produce a chart but the colors are not producing in the output :
data x; input rowlbl$ timepoint$ grade$ nsubjects$; datalines; pain day 1 30 min mild 4
pain day 1 Pm mild
pain day 1 pm moderate 4
pain day 2 mild 4
pain day 2 moderate 4
pain day 3 mild 4
pain day 4 moderate 4 ; run;
Here is the code Iam using:
*For consistent colors of vbars;
proc sort data=final out=map nodupkey;
by rowlbl grade;
run;
data attrmap;
set map(keep=rowlbl grade);
length fillcolor $8. ID $8. value $20. linecolor $10;
if upcase(rowlbl)='REDNESS' then fillcolor ='#cf46b7' ;
else if upcase(rowlbl)='SWELLING' then fillcolor='#DF7F70';
else if upcase(rowlbl)='PAIN' and upcase(grade)='MILD' then fillcolor='#a82a09';
else if upcase(rowlbl)='PAIN' and upcase(grade)='MODERATE' then fillcolor='#46e75e';
id ='rowlbl';
value = rowlbl;
linecolor='black';
keep id value fillcolor linecolor;
run;
proc sort;
by id value;
run;
proc sgplot data=final dattrmap=attrmap;
by rowlbl;
vbar tptn / response=nsubjs group=rowlbl name="Param" barwidth=0.5 attrid=rowlbl;
xaxis label='Timepoints' valueattrs=(size=8) values=(0 to 8
valuesdisplay=('Day 1 30 Min' 'Day 1 PM' 'Day 2' 'Day 3' 'Day 4'
'Day 5' 'Day 6' 'Day 7' 'Day 7+'); *valuesformat=atpt.;* fitpolicy=rotatethin;
yaxis values=(0 to 14 by 2) label='Number of Subjects' ;
keylegend "Param"/ noborder title="" location=outside position=bottom across=9
sortorder=ascending;
run;
Final Ouput i need to get as below:
Please Provide your inputs whether if i am missing something . TIA.
If you expect a worked example please provide the actual data to plot or modify your example data set to match the plot code. Your SGPLOT references a data set not provided, uses variables not in the X data set : TPTN and NSUBJS .
Your Sgplot code also has a missing ) for the VALUES option in the XAXIS statement.
Hi @mounikag,
I'm not sure if the attributes in the DATTRMAP= dataset can depend on BY values. I've modified your code (using sample code from a "Graphically Speaking" blog post) so that only "Pain" data are used (no BY-group processing).
data have;
input rowlbl $ timepoint &$12. grade $ nsubjects;
datalines;
Pain Day 1 30 min mild 3
Pain Day 1 30 min moderate 1
Pain Day 1 PM mild 5
Pain Day 1 PM moderate 1
Pain Day 2 mild 8
Pain Day 2 moderate 2
Pain Day 3 mild 9
Pain Day 3 moderate 2
Pain Day 4 mild 4
Pain Day 5 mild 1
;
data pain_attrmap;
length ID $8 value $20 fillcolor $8 linecolor $10;
ID='Pain'; linecolor='black';
input value fillcolor;
cards;
mild #017DBA
moderate #97C93D
;
ods graphics / height=400px width=800px;
title 'Pain';
proc sgplot data=have dattrmap=pain_attrmap noborder;
vbarparm category=timepoint response=nsubjects / group=grade barwidth=0.5 attrid=Pain;
xaxis label='Timepoints' valueattrs=(size=8)
values=('Day 1 30 min' 'Day 1 PM' 'Day 2' 'Day 3' 'Day 4' 'Day 5' 'Day 6' 'Day 7' 'Day 7+')
display=(noline noticks);
yaxis values=(0 to 12) label='Number of Subjects' display=(noline noticks) grid gridattrs=(color='#D2E2F4');
keylegend / noborder title="" location=outside position=bottom across=9 sortorder=ascending;
run;
title;
Result:
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.
Find more tutorials on the SAS Users YouTube channel.