Hi @Ksharp , How Can I reduce the gap between the two groups? I tried reducing the cluster width, but it did not change anything. Since the bars look too narrow, I wanted to reduce the gap and increase the width. I tried increasing and decreasing the numbers, but it did not work. Is it because of Page width?
I think you owe me two hundred dollars.
And you could use options offsetmin= ,offsetmax=, clusterwidht= to adjust the distance between box.
/* Step 1: Create Dummy Dataset */ data dummy_data; input Subjid $ Age_Group $ Treatment $ AVAL; /* Numeric representation for Treatment */ if Treatment = 'DrugA' then trtn = 1; else if Treatment = 'DrugB' then trtn = 2; else if Treatment = 'Refer' then trtn = 3; /* Numeric representation for Age_Group */ if Age_Group = '5-13' then agegrpn = 1; else if Age_Group = '14-21' then agegrpn = 2; else if Age_Group = '22-35' then agegrpn = 3; datalines; S01 5-13 DrugA 6000 S02 5-13 DrugA 7000 S03 5-13 DrugB 8000 S04 5-13 DrugB 9000 S05 5-13 Refer 500 S06 5-13 Refer 12000 S07 5-13 DrugA 10000 S08 5-13 DrugB 11000 S09 5-13 Refer 400 S10 14-21 DrugA 4000 S11 14-21 DrugA 5000 S12 14-21 DrugB 6000 S13 5-13 DrugB 7000 S14 14-21 Refer 1000 S15 14-21 Refer 300 S16 22-35 DrugA 8000 S17 14-21 DrugB 9000 S18 14-21 Refer 100 S19 22-35 DrugA 200 S20 22-35 DrugA 300 S21 22-35 DrugB 400 S22 22-35 DrugB 500 S23 22-35 Refer 50 S24 14-21 Refer 100 S25 22-35 DrugA 600 S26 22-35 DrugB 700 S27 22-35 Refer 150 S28 5-13 DrugA 8000 ; run; data dummy_data; length treatment _treatment $ 40; set dummy_data; _treatment=treatment; treatment=cats(_treatment,'2019'); output; /* Modify for Year 2010 */ Year = 2010; AVAL = AVAL + 1000; treatment=cats(_treatment,'2020'); output; run; proc sort data=dummy_data; by treatment agegrpn; run; proc means data=dummy_data nway noprint; /* by trtn strat2rn;*/ /* var pchg;*/ by treatment agegrpn; var aval; *output out=statall min= max= median= mean= n= q1 = q3 =/autoname; output out=stat n=n mean=mean median=median; run; data final; merge dummy_data stat(drop=_:); by treatment agegrpn; num =1; if TREATMENT=:'DrugA' then do;mean1=mean;median1=median;end; if TREATMENT=:'DrugB' then do;mean2=mean;median2=median;end; if TREATMENT=:'Refer' then do;mean3=mean;median3=median;end; if year=2010 then call missing(num); run; proc sort data=final;by treatment;run; proc template; define statgraph boxplot; begingraph / datacolors=(lightblue lightblue lightgreen lightgreen lightgrey lightgrey) datacontrastcolors=(lightblue lightblue lightgreen lightgreen lightgrey lightgrey) datasymbols=(trianglefilled trianglefilled squarefilled squarefilled circlefilled circlefilled); /* Define Custom Attributes for Age Groups */ discreteattrmap name="groupline" / ignorecase=true; value 'DrugA' / lineattrs=(color=lightblue pattern=1 thickness=2px) markerattrs=(color=lightblue symbol=trianglefilled size=8pt) textattrs=(color=lightblue); value 'DrugB' / lineattrs=(color=lightgreen pattern=1 thickness=2px) markerattrs=(color=green symbol=squarefilled size=8pt) textattrs=(color=lightgreen); value 'Refer' / lineattrs=(color=grey pattern=1 thickness=2px) markerattrs=(color=grey symbol=circlefilled size=8pt) textattrs=(color=grey); enddiscreteattrmap; discreteattrvar attrvar=trtgrp var=trtn attrmap='groupline'; /* Add Custom Legends */ legenditem type=markerline name='A' / lineattrs=(color=blue pattern=1 thickness=2px) markerattrs=(color=blue size=8pt symbol=trianglefilled) label="DrugA" labelattrs=(size=9pt); legenditem type=markerline name='B' / lineattrs=(color=green pattern=1 thickness=2px) markerattrs=(color=green size=8pt symbol=squarefilled) label="DrugB" labelattrs=(size=9pt); legenditem type=markerline name='R' / lineattrs=(color=grey pattern=1 thickness=2px) markerattrs=(color=grey size=8pt symbol=circlefilled) label="Refer" labelattrs=(size=9pt); layout overlay / xaxisopts=(offsetmin=0.2 offsetmax=0.2 label="Age Groups" labelattrs=(size=12pt weight=bold) tickvalueattrs=(size=12pt weight=bold)) yaxisopts=(offsetmin=0.05 offsetmax=0.05 label="Results" labelfitpolicy=splitalways labelsplitchar='^' labelattrs=(size=11pt weight=bold) tickvalueattrs=(size=12pt weight=bold) linearopts=(tickvaluesequence=(start=-2000 end=15000 increment=1000) viewmin=-2000 viewmax=15000)); /* Reference Line */ referenceline y=0 / lineattrs=(color=grey pattern=3); /* Box Plot */ boxplot x=agegrpn y=AVAL / group=treatment groupdisplay=cluster boxwidth=0.6 clusterwidth=0.9 ; /*scatter of mean*/ scatterplot x=agegrpn y=mean1/group=treatment groupdisplay=cluster clusterwidth=0.9 markerattrs=(symbol=trianglefilled color=blue); scatterplot x=agegrpn y=mean2/group=treatment groupdisplay=cluster clusterwidth=0.9 markerattrs=(symbol=squarefilled color=green); scatterplot x=agegrpn y=mean3/group=treatment groupdisplay=cluster clusterwidth=0.9 markerattrs=(symbol=circlefilled color=grey); /*scatter of median*/ highlowplot x=agegrpn low=median1 high=median1/group=treatment groupdisplay=cluster clusterwidth=0.9 type=bar barwidth=0.6 outlineattrs=(thickness=2px color=blue) ; highlowplot x=agegrpn low=median2 high=median2/group=treatment groupdisplay=cluster clusterwidth=0.9 type=bar barwidth=0.6 outlineattrs=(thickness=2px color=green) ; highlowplot x=agegrpn low=median3 high=median3/group=treatment groupdisplay=cluster clusterwidth=0.9 type=bar barwidth=0.6 outlineattrs=(thickness=2px color=grey) ; /* Legend */ discretelegend 'A' 'B' 'R' / down=1 border=false; /* Axis Table */ innermargin; axistable x=agegrpn value=num / class=trtn classdisplay=cluster clusterwidth=0.9 classorder=ascending valueattrs=(weight=bold size=12) labelattrs=(weight=bold) colorgroup=trtgrp ; endinnermargin; endlayout; endgraph; end; run; proc format; value agegrpn 1="5-13" 2="14-21" 3="22-35" ; value treatment 1="DrugA" 2="DrugB" 3="Refer" ; run; /* Generate the Graph */ options orientation = landscape errors = 2 missing = ' ' nofmterr ls = 175 validvarname = upcase nobyline; ods escapechar = '^'; ods results on; ods listing close; ods rtf file = "C:\temp\test.rtf" ; ods graphics on / imagefmt=png attrpriority=none height=4.8 in width=8.5 in border=off; proc sgrender data=final template=boxplot; format trtn treatment. agegrpn agegrpn.; run; ods rtf close; ods listing;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.