BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
pink_poodle
Barite | Level 11

Hi,

Could someone please share SAS code to make a simple forest plot out of this (two studies go on the y axis):

data forest;                                                                                                                            
   input Study $1-16 OddsRatio LowerCL UpperCL;                                                                              
   format oddsratio lowercl uppercl 5.3;                                                                                                                                                                                       
datalines;                                                                                                                              
Modano  (1967)    0.590 0.096 3.634                                                                                              
Borodan (1981)    0.464 0.201 1.074                                                                                                                                                                                     
;   
run; 

Many thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
data forest;                                                                                                                            
   input Study $1-16 OddsRatio LowerCL UpperCL;                                                                              
   format oddsratio lowercl uppercl 5.3;                                                                                                                                                                                       
datalines;                                                                                                                              
Modano  (1967)    0.590 0.096 3.634                                                                                              
Borodan (1981)    0.464 0.201 1.074                                                                                                                                                                                     
;   
run; 


title "Odds Ratios with 95% Wald Confidence Limits";
proc sgplot data=forest noautolegend;
   scatter y=Study x=oddsratio / xerrorlower=LowerCL xerrorupper=UpperCL
           markerattrs=(symbol=diamondfilled);
   refline 1 / axis=x;
  * xaxis grid type=log label="Odds Ratio (log scale)";  /* specify log scale */
  * yaxis grid display=(nolabel) discreteorder=data reverse;
run;

Based on this post, but I commented out the log scale axis.

https://blogs.sas.com/content/iml/2015/07/29/or-plots-log-scale.html

 

Quite a few more complicated examples with data and code on the SAS blogs.

https://blogs.sas.com/content/graphicallyspeaking/2012/02/01/forest-plot-using-sgplot-procedure/

 


@pink_poodle wrote:

Hi,

Could someone please share SAS code to make a simple forest plot out of this (two studies go on the y axis):

data forest;                                                                                                                            
   input Study $1-16 OddsRatio LowerCL UpperCL;                                                                              
   format oddsratio lowercl uppercl 5.3;                                                                                                                                                                                       
datalines;                                                                                                                              
Modano  (1967)    0.590 0.096 3.634                                                                                              
Borodan (1981)    0.464 0.201 1.074                                                                                                                                                                                     
;   
run; 

Many thanks!


 

View solution in original post

13 REPLIES 13
Reeza
Super User
data forest;                                                                                                                            
   input Study $1-16 OddsRatio LowerCL UpperCL;                                                                              
   format oddsratio lowercl uppercl 5.3;                                                                                                                                                                                       
datalines;                                                                                                                              
Modano  (1967)    0.590 0.096 3.634                                                                                              
Borodan (1981)    0.464 0.201 1.074                                                                                                                                                                                     
;   
run; 


title "Odds Ratios with 95% Wald Confidence Limits";
proc sgplot data=forest noautolegend;
   scatter y=Study x=oddsratio / xerrorlower=LowerCL xerrorupper=UpperCL
           markerattrs=(symbol=diamondfilled);
   refline 1 / axis=x;
  * xaxis grid type=log label="Odds Ratio (log scale)";  /* specify log scale */
  * yaxis grid display=(nolabel) discreteorder=data reverse;
run;

Based on this post, but I commented out the log scale axis.

https://blogs.sas.com/content/iml/2015/07/29/or-plots-log-scale.html

 

Quite a few more complicated examples with data and code on the SAS blogs.

https://blogs.sas.com/content/graphicallyspeaking/2012/02/01/forest-plot-using-sgplot-procedure/

 


@pink_poodle wrote:

Hi,

Could someone please share SAS code to make a simple forest plot out of this (two studies go on the y axis):

data forest;                                                                                                                            
   input Study $1-16 OddsRatio LowerCL UpperCL;                                                                              
   format oddsratio lowercl uppercl 5.3;                                                                                                                                                                                       
datalines;                                                                                                                              
Modano  (1967)    0.590 0.096 3.634                                                                                              
Borodan (1981)    0.464 0.201 1.074                                                                                                                                                                                     
;   
run; 

Many thanks!


 

pink_poodle
Barite | Level 11

@Reeza,

Thank you for a simple and elegant solution. I have just one follow-up question, I would like population numbers as a secondary y-axis on a graph like this (please also see image below):

Study                          N     

Modano (1967)          100

Borodan (1981)          200

How would you modify the code to accommodate the N variable?:

data forest;                                                                                                                            
   input Study $1-16 N OddsRatio LowerCL UpperCL;                                                                              
   format oddsratio lowercl uppercl 5.3;                                                                                                                                                                                       
datalines;                                                                                                                              
Modano  (1967)    100 0.590 0.096 3.634                                                                                              
Borodan (1981)    200 0.464 0.201 1.074                                                                                                                                                                                     
;   
run; 

title "Odds Ratios with 95% Wald Confidence Limits";
proc sgplot data=forest noautolegend;
   scatter y=Study x=oddsratio / xerrorlower=LowerCL xerrorupper=UpperCL
           markerattrs=(symbol=diamondfilled);
   refline 1 / axis=x;
  * xaxis grid type=log label="Odds Ratio (log scale)";  /* specify log scale */
  * yaxis grid display=(nolabel) discreteorder=data reverse;
run;

Many thanks!

forest plot.png

 

Reeza
Super User
Several of the examples in the blog posts have this feature, have you tried using any of the code examples from there?

pink_poodle
Barite | Level 11

I can put population numbers in columns to the right, but am not sure which part of code is responsible for those columns

(https://blogs.sas.com/content/graphicallyspeaking/files/2012/02/Full-SAS-93-Code.txt)

pink_poodle_0-1631307218979.png

/*--Set up columns to create the stat tables--*/
  OR='OR'; LCL='LCL'; UCL='UCL'; WT='Weight';

  datalines;
Modano  (1967)    1  0.590 0.096 3.634  1
Borodan (1981)    1  0.464 0.201 1.074  3.5
Leighton (1972)   1  0.394 0.076 2.055  2
Novak   (1992)    1  0.490 0.088 2.737  2
Stawer  (1998)    1  1.250 0.479 3.261  3
Truark   (2002)   1  0.129 0.027 0.605  2.5
Fayney   (2005)   1  0.313 0.054 1.805  2
Modano  (1969)    1  0.429 0.070 2.620  2
Soloway (2000)    1  0.718 0.237 2.179  3
Adams   (1999)    1  0.143 0.082 0.250  4
Truark2  (2002)   1  0.129 0.027 0.605  2.5
Fayney2  (2005)   1  0.313 0.054 1.805  2
Modano2 (1969)    1  0.429 0.070 2.620  2
Soloway2(2000)    1  0.718 0.237 2.179  3
Adams2   (1999)   1  0.143 0.082 0.250  4
Overall           2  0.328 0.233 0.462  .
;
run;
Reeza
Super User
Then you try them one by one and see which does what.
pink_poodle
Barite | Level 11
Sequential inactivation, yes :). I decided why not stretch a table next to a graph instead. It will look just as good.
Reeza
Super User
The code comments are pretty clear to me to be honest.
pink_poodle
Barite | Level 11
@Reeza,
I have a quick follow-up question:
Is there a way to make xerrorlower and xerrorupper (i.e., odds ratio) values to appear above the tick marks?
Many thanks!
pink_poodle
Barite | Level 11
*Here is one way of doing it;
title "Odds Ratios with 95% Wald Confidence Limits"; proc sgplot data=forest noautolegend; scatter y=Study x=oddsratio / xerrorlower=LowerCL xerrorupper=UpperCL markerattrs=(symbol=diamondfilled) datalabel=oddsratio datalabelattrs=(weight=bold); scatter y=study x=lowercl / datalabel = lowercl markerattrs=(size=0 color=crimson); scatter y=study x=uppercl / datalabel = uppercl markerattrs=(size=0 color=crimson); refline 1 / axis=x; * xaxis grid type=log label="Odds Ratio (log scale)"; /* specify log scale */ * yaxis grid display=(nolabel) discreteorder=data reverse; run;
Reeza
Super User
Change XERRORLOWER to YERRORLOWER?

I think that doesn't make sense though as scale is along the x axis so how would the Y axis also be the same scale?
pink_poodle
Barite | Level 11

The output looks right:

 
pink_poodle_0-1643049359038.png

 

Reeza
Super User
That shows error bars along the X axis only, no Y axis error bars.
Reeza
Super User
Update: Are you asking for datalabels for the error bars? If so, looks like you've got that solved.
If there isn't an option within Scatter you could use TEXT statements to plot those.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 13 replies
  • 4409 views
  • 5 likes
  • 2 in conversation