BookmarkSubscribeRSS Feed
DavidsM
Fluorite | Level 6

Dear SAS Community,

 

I would like to plot both an overall and a stratified graph on the same graph procedure. I have been using the following procedures for the overall and stratified graphs separately, but I would like to combine them into one graph showing the percentages for both the overall and stratified graphs. 

 

/*Creating Data with Categorical Variables for Satisfaction and Age Categories*/
Data Customer_response; set Origina_Data;
if Response="Satisfied" then Satisfaction=1;
else if Response "Not Satisfied" then Satisfaction=2;

if Age lt 35 then Age_Category=1
else if Age ge 35 then Age_Category=2;
run;

 

 

Below is that SAS code that works for the overall graph.

 

 

/*Creates an Overall Graph for the Satisfaction Variable showing percentages */
title; /* Removes all titles */
Proc sgplot data=Customer_response;
format Satisfaction.;
styleattrs datacolors=(VIGB VIPK) ;
vbar Satisfaction / group=Satisfaction stat=percent; /* Creates a vertical bar chart showing percentages */
LABEL Satisfaction='Satisfaction Category';
XAXIS LABEL = 'Satisfaction';
XAXIS VALUES =("1" "2");
YAXIS LABEL = 'Proportion of Participants According to Satisfaction';
keylegend / title="Customer Satisfaction";
run;

 

 

Below is the program that seems to produce the desired age stratified graph.


/* Frequency plot of percentages for two variables */
Proc freq data=Customer_response;
tables Age_Category *Satisfaction / plots=FreqPlot(twoway=cluster scale=Percent) out=Freq2Out;
run;


/* Dividing by 100 and apply PERCENTw.d format*/
Data Freq2Out;
set Freq2Out;
Percent = Percent / 100;
format Percent PERCENT5.;
run;

 

 

/*Graph showing Satisfaction Stratified by Age Categories*/
ods listing style=listing;
ods graphics / width=7.8in height=7in;
*title 'Graph showing Satisfaction Stratified by Age Categories';
Proc sgplot data=Freq2Out;
format Satisfaction.;
styleattrs datacolors=(VIGB VIPK) ;
vbar Age_Category / group=Satisfaction groupdisplay=cluster response=Percent;
XAXIS LABEL = 'Age Category';
YAXIS LABEL = 'Proportion of Participants According to Satisfaction Categories'
VALUES =("1" "2") VALUESDISPLAY=("Satisfied" "Not Satisfied");
keylegend / title="Satisfaction";
run;

 

Those two graphs are currently drawn separately, but I want to have both the overall and stratified graphs combined on the same graph in a single procedure, with the overall graph on the left side and the stratified on the right side of the graph creates. I will be glad to receive assistance from the community. If there is any other more efficient procedure, I would like to see a sample code that I can use to produced those graphs.

 

Thank you.

6 REPLIES 6
JeffMeyers
Barite | Level 11
I'm sure there are more elegant solutions, but one that comes to mind for me in these situations is to just duplicate the dataset. Set the dataset into itself twice using the in operators to separate the two versions. Then in one of the versions make the category variable equal to "Overall" for all values. Then it just becomes another "stratification level" to the graph.

E.g.
Data bmt;
Set sashelp.bmt (in=a)
Sashelp.bmt (in=b);
If a Then group="Overall";
Run;

Proc lifetest data=bmt;
Strata group;
Time T*status(0);
Run;

The calculated statistics aren't all valid anymore but the graph part doesn't care. I think you can still use your strata code to make the graph and it will just have another level for counts.
DavidsM
Fluorite | Level 6

Dear JeffMeyers,

 

I do not have variables for time (T) or status because I am not conducting time-to-event analysis. I am just obtaining descriptive visualization of the data for the overall and the stratified. 

 

Any other procedure that can produce these graphs would be fine. I look forward to receiving assistance either through modification of my code or any other approach or procedure. 

 

Thanks

JeffMeyers
Barite | Level 11
Hello. I wasn't telling you that you had to do a KM analysis. I described a method you could use to overlay the plots with each other so you could try the same method with your code. It's not a survival analysis exclusive method.
DavidsM
Fluorite | Level 6

Dear JeffMeyers,

 

Thank you for the reply.

What do the T and Status variables represent in the Proc Lifetest procedure in this case? 

I look forward to receiving some clarification.

Thank you.

DavidsM
Fluorite | Level 6

Dear SAS Community,

 

I would like to plot both an overall and a stratified graph on the same graph procedure. I have been using the following procedures for the overall and stratified graphs separately, but I would like to combine them into one graph showing the percentages for both the overall and stratified graphs. 

 

/*Creating Data with Categorical Variables for Satisfaction and Age Categories*/
Data Customer_response; set Origina_Data;
if Response="Satisfied" then Satisfaction=1;
else if Response "Not Satisfied" then Satisfaction=2;

if Age lt 35 then Age_Category=1
else if Age ge 35 then Age_Category=2;
run;

 

 

Below is that SAS code that works for the overall graph.

 

 

/*Creates an Overall Graph for the Satisfaction Variable showing percentages */
title; /* Removes all titles */
Proc sgplot data=Customer_response;
format Satisfaction.;
styleattrs datacolors=(VIGB VIPK) ;
vbar Satisfaction / group=Satisfaction stat=percent; /* Creates a vertical bar chart showing percentages */
LABEL Satisfaction='Satisfaction Category';
XAXIS LABEL = 'Satisfaction';
XAXIS VALUES =("1" "2");
YAXIS LABEL = 'Proportion of Participants According to Satisfaction';
keylegend / title="Customer Satisfaction";
run;

 

 

Below is the program that seems to produce the desired age stratified graph.


/* Frequency plot of percentages for two variables */
Proc freq data=Customer_response;
tables Age_Category *Satisfaction / plots=FreqPlot(twoway=cluster scale=Percent) out=Freq2Out;
run;


/* Dividing by 100 and apply PERCENTw.d format*/
Data Freq2Out;
set Freq2Out;
Percent = Percent / 100;
format Percent PERCENT5.;
run;

 

 

/*Graph showing Satisfaction Stratified by Age Categories*/
ods listing style=listing;
ods graphics / width=7.8in height=7in;
*title 'Graph showing Satisfaction Stratified by Age Categories';
Proc sgplot data=Freq2Out;
format Satisfaction.;
styleattrs datacolors=(VIGB VIPK) ;
vbar Age_Category / group=Satisfaction groupdisplay=cluster response=Percent;
XAXIS LABEL = 'Age Category';
YAXIS LABEL = 'Proportion of Participants According to Satisfaction Categories'
VALUES =("1" "2") VALUESDISPLAY=("Satisfied" "Not Satisfied");
keylegend / title="Satisfaction";
run;

 

Those two graphs are currently drawn separately, but I want to have both the overall and stratified graphs combined on the same graph in a single procedure, with the overall graph on the left side and the stratified on the right side of the graph creates. I will be glad to receive assistance from the community. If there is any other more efficient procedure, I would like to see a sample code that I can use to produced those graphs.

 

Thank you.

StatDave
SAS Super FREQ

With some work, there might be a way to simplify this down into a single call to PROC SGPANEL. But since you already have code that creates the two plots you want, you can just put them together in a side by side panel using ODS LAYAOUT GRIDDED and ODS REGION statements as illustrated in this note.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 691 views
  • 3 likes
  • 3 in conversation