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

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1141 views
  • 0 likes
  • 2 in conversation