BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Dear all,

I am using PROC SGRENDER in order to create a barchart of two groups at eight different time points. At the top of each bar I want to show the confidence intervals.
My dataset contains 16 records with the value of each bar ("a_rate") for each of the two groups ("group_id") at each time point ("period") already calculated. It also contains the values for the error bars ( "ll_ci", "ul_ci" ) as well as a variable "g_order" with values from 1 to 16 according to the data ordering (=_n_).

I am using the following code:

proc template;
define statgraph gns_bar;
begingraph;
entrytitle halign=left "Barchart";
layout overlay / walldisplay=(fill)
xaxisopts=(linearopts=(TICKVALUEFITPOLICY=STAGGER) LABELATTRS=())
yaxisopts =(linearopts=(tickvaluesequence=(start=0 end=0.5 increment=0.02)));
barchartparm x=g_order y=a_rate / group=group_id
errorlower=ll_ci errorupper=ul_ci;
endlayout;
endgraph;
end;
run;

proc sgrender data=a_bar template=gns_bar;
run;

The problem is that in the options of the barchartparm statement, it seems that the options "group=…" and "errorlower=… errorupper=…" cannot work simultaneously.
If I use only the "group=group_id" option then the bars are displayed in two separate colors according to their grouping ("group_id" variable).
If I use only the "errorlower=… errorupper=…" options, I get the error bars displayed for each bar.
But if I use both options then only the "group=…" option seems to work since the error bars are not displayed at all.

Also, is there a way of defining the space between the bars of the same group as well as the space between bars of different time points? (what I want to do is to have at each time point the 2 bars together – no space – but a small gap between the bars of different time points).

I hope it makes sense.

Thank you,
George
2 REPLIES 2
DanH_sas
SAS Super FREQ
Note that I do not have your data to visually verify the results, but the tips below should help.

To solve your first problem, you can overlay a zero-sized scatterplot with errorbars on the barchartparm to give you both grouped bars and errorbars:

proc template;
define statgraph gns_bar;
begingraph;
entrytitle halign=left "Barchart";
layout overlay / walldisplay=(fill)
xaxisopts=(linearopts=(TICKVALUEFITPOLICY=STAGGER) LABELATTRS=())
yaxisopts =(linearopts=(tickvaluesequence=(start=0 end=0.5 increment=0.02)));
barchartparm x=g_order y=a_rate / group=group_id;
scatterplot x=g_order y=a_rate / group==group_id yerrorlower=ll_ci yerrorupper=ul_ci markerattrs=(size=0);
endlayout;
endgraph;
end;
run;

To handle your second requirement, I recommend a little different structure for your graph. Try using a LAYOUT DATALATTICE. You can then set the BARWIDTH=1 to create no space between your bars and use the COLUMNGUTTER option to control the space between periods. There are other options to move the column headers if you want. Hope this helps!

-- Dan

proc template;
define statgraph gns_bar;
begingraph;
entrytitle halign=left "Barchart";
layout datalattice columnvar=period / columngutter=10 columnaxisopts=(linearopts=(TICKVALUEFITPOLICY=STAGGER) LABELATTRS=())
rowaxisopts =(linearopts=(tickvaluesequence=(start=0 end=0.5 increment=0.02)));
layout prototype / walldisplay=(fill);
barchartparm x=g_order y=a_rate / group=group_id barwidth=1;
scatterplot x=g_order y=a_rate / group=group_id yerrorlower=ll_ci yerrorupper=ul_ci markerattrs=(size=0);
endlayout;
endlayout;
endgraph;
end;
run;
deleted_user
Not applicable
Dear Dan,

Problem 1 solved! The code you suggested works fine! The error bars display correctly together with different color for each bar/group.

As far as the solution suggested for problem 2, I am working on it. The problem is that this way I get a grid of 8 barcharts (as many as the periods) one next to the other, where in each barchart only the bars for the specific period are displayed but together with the whole plotted area (i.e. all 8 periods in each x-axis ).

Thank you very much!

sas-innovate-2024.png

 

Time is running out to save with the early bird rate. Register by Friday, March 1 for just $695 - $100 off the standard rate.

 

Check out the agenda and get ready for a jam-packed event featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events. 

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1101 views
  • 0 likes
  • 2 in conversation