楼主: userzht
10064 12

[问答] 求SAS做图示中森林图的宏程序 [推广有奖]

  • 6关注
  • 3粉丝

博士生

34%

还不是VIP/贵宾

-

威望
0
论坛币
2362 个
通用积分
1.0004
学术水平
3 点
热心指数
3 点
信用等级
3 点
经验
4857 点
帖子
185
精华
0
在线时间
224 小时
注册时间
2009-5-19
最后登录
2021-10-19

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
找了一个宏程序,但做出来的图要素不全,所以想再求一个能做出下面这样图的宏程序。谢谢!

1.png
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:宏程序 森林图 程序 森林

沙发
pingmin555 发表于 2014-10-9 10:19:26 |只看作者 |坛友微信交流群
这图是你画的么?

使用道具

藤椅
userzht 发表于 2014-10-10 16:29:03 |只看作者 |坛友微信交流群
pingmin555 发表于 2014-10-9 10:19
这图是你画的么?
不是呀,截的图

使用道具

板凳
Imasasor 发表于 2014-10-10 20:14:39 |只看作者 |坛友微信交流群
做森林图最好有STATA,操作简单,SAS要用annotation做,程序很复杂,SAS网站上有,你可以查查

使用道具

报纸
userzht 发表于 2014-10-11 10:03:41 |只看作者 |坛友微信交流群
Imasasor 发表于 2014-10-10 20:14
做森林图最好有STATA,操作简单,SAS要用annotation做,程序很复杂,SAS网站上有,你可以查查
好的,谢谢!我也看看stata的,好像需要再下个专门的程序包才可以用,也不知道stata做出来的图能不能给修饰得好看一些。。。先学学sas的好啦

从别的地儿转一个免费学习网址,找到好多有用的,其中有森林图哦:http://saslist.net/archives/124

使用道具

地板
Imasasor 发表于 2014-10-11 10:05:55 |只看作者 |坛友微信交流群
userzht 发表于 2014-10-11 10:03
好的,谢谢!我也看看stata的,好像需要再下个专门的程序包才可以用,也不知道stata做出来的图能不能给修 ...
stata做出的森林图,导出eps格式,该格式为矢量图格式,用adobe illustrator打开,可以随心所欲的随便修饰

使用道具

7
userzht 发表于 2014-10-11 13:46:37 |只看作者 |坛友微信交流群
Imasasor 发表于 2014-10-11 10:05
stata做出的森林图,导出eps格式,该格式为矢量图格式,用adobe illustrator打开,可以随心所欲的随便修饰 ...
高手啊

看来ILLUSTRATOR也要学起了啊

使用道具

8
userzht 发表于 2014-10-13 08:42:56 |只看作者 |坛友微信交流群
Imasasor 发表于 2014-10-11 10:05
stata做出的森林图,导出eps格式,该格式为矢量图格式,用adobe illustrator打开,可以随心所欲的随便修饰 ...
再次求助一下,根据例子修改的annotate过程,可是不能达到预期效果,敢问能否给修改一下啊?图形如下:
forest_plot10.png

诉求如下:
1、age和sex及其各自的两个分层合并到一条bar里,就是横向上用相同的颜色;
2、去掉图左边(也就是subgroup下)的竖线,让分层名和图形混然一体;
3、age和sex行的短竖线去掉;
4、除了图左边的竖线外,上边、下边、最右边的竖线都去掉;
5、最右面三列数字,PCI Group、Group、P Value列的缺失值.都去掉;

有点多,是想更好的解决问题,谢谢!代码装不下了,见下贴吧:

复制代码


使用道具

9
userzht 发表于 2014-10-13 08:44:26 |只看作者 |坛友微信交流群
Imasasor 发表于 2014-10-11 10:05
stata做出的森林图,导出eps格式,该格式为矢量图格式,用adobe illustrator打开,可以随心所欲的随便修饰 ...
代码:[code]%let name=forest_plot;                                                                                                                  
filename odsout '.';                                                                                                                    
                                                                                                                                       
data forest_data;                                                                                                                       
input Subgroup $1-13 No_Pct $14-23 HazardRatio 24-26 LowerCL 28-30 UpperCL 32-34 PCI 37-40 other 42-45 P 49-52;                        
  data_order=_N_;                                                                                                                       
  Q1=HazardRatio-0.01;                                                                                                                  
  Q3=HazardRatio+0.01;                                                                                                                  
datalines;                                                                                                                              
Overall      2166(100) 1.3 0.8 1.6  17.2 15.6                                                                                          
Age                                             0.05                                                                                    
    <=65 Yr  1534( 71) 1.5 1.1 1.9  17   13.2                                                                                          
    >65 Yr    632( 29) 0.8 0.5 1.3  17.8 21.3                                                                                          
Sex                                             0.13                                                                                    
    Male     1690( 78) 1.5 1.1 1.9  16.8 13.5                                                                                          
    Female    476( 22) 0.8 0.5 1.3  18.3 22.9                                                                                          
;                                                                                                                                       
run;                                                                                                                                    
                                                                                                                                       
/*                                                                                                                                      
User-defined format so that aeseq number prints as aedecod text.                                                                        
(this way, the axis will be in the numeric order, but will show the text values)                                                        
*/                                                                                                                                      
proc sql;                                                                                                                              
create table foo as select unique data_order, subgroup from forest_data;                                                               
quit; run;                                                                                                                              
data control; set foo (rename=(data_order=start subgroup=label));                                                                       
fmtname = 'forfmt';                                                                                                                     
type = 'N';                                                                                                                             
end = START;                                                                                                                           
run;                                                                                                                                    
proc format lib=work cntlin=control;                                                                                                   
run;                                                                                                                                    
                                                                                                                                       
/* Annotate the box & whiskers */                                                                                                      
data anno_box; set forest_data;                                                                                                         
length function color $8 style $20 text $30;                                                                                            
when='a'; hsys='3';                                                                                                                     
                                                                                                                                       
xsys='2'; ysys='2'; color=''; size=.; line=.;                                                                                          
/* draw whiskers */                                                                                                                    
function='move'; x=LowerCL; y=data_order; output;                                                                                      
function='draw'; size=.01; x=UpperCL; output;                                                                                          
/* draw solid box */                                                                                                                  
function='move'; x=q1; y=data_order; output;                                                                                          
function='move'; y=y-.03; output;                                                                                                      
function='bar'; style='solid'; color='gray77'; line=0;                                                                                 
y=y+2*.03; x=q3; output;                                                                                                               
/* draw border around box */                                                                                                           
function='move'; x=q1; y=data_order; output;                                                                                          
function='move'; y=y-.03; output;                                                                                                      
function='bar'; style='empty'; color='black'; line=0;                                                                                 
y=y+2*.03; x=q3; output;                                                                                                               
                                                                                                                                       
if subgroup='Overall' then do;                                                                                                         
color='cx6495ed';                                                                                                                     
/* dashed line */                                                                                                                     
xsys='2'; ysys='1';                                                                                                                    
x=hazardratio;                                                                                                                        
y=0; function='move'; output;                                                                                                         
y=100; function='draw'; size=.01; line=3; output;                                                                                      
end;                                                                                                                                   
                                                                                                                                       
run;                                                                                                                                    
                                                                                                                                       
/* annotate the little lines to denote treatment & placebo */                                                                           
data anno_lines;                                                                                                                        
length function color $8 style $20;                                                                                                     
xsys='2'; ysys='1'; when='a'; color='black';                                                                                            
x=1.0;                                                                                                                                 
y=0; function='move'; output;                                                                                                           
y=104; function='draw'; output;                                                                                                         
x=100;                                                                                                                                 
y=0; function='move'; output;                                                                                                           
y=104; function='draw'; output;                                                                                                         
run;                                                                                                                                    
                                                                                                                                       
data anno_headers;                                                                                                                     
length function color $8 style $20 text $30;                                                                                            
xsys='1'; ysys='1'; when='a'; size=1.5;                                                                                                
function='label'; style='albany amt';                                                                                                   
y=115;                                                                                                                                 
x=0; text='Subgroup'; output;                                                                                                           
x=20; text='No. of Patients (%)'; output;                                                                                               
x=50; text='Hazard Ratio'; output;                                                                                                      
x=87; text='4-Yr Cumulative Event Rate'; output;                                                                                       
size=1;                                                                                                                                 
y=110; text='Medical Therapy'; output;                                                                                                  
size=1.2;                                                                                                                              
y=105;                                                                                                                                 
x=95; text='P value'; output;                                                                                                           
x=x-8; text='Group'; output;                                                                                                            
x=x-8; text='PCI Group'; output;                                                                                                        
xsys='2'; size=1;                                                                                                                       
x=1.5; text='---Medical Therapy Better--->'; output;                                                                                    
x=.5; text='<---PCI Better---'; output;                                                                                                
run;                                                                                                                                    
                                                                                                                                       

使用道具

10
userzht 发表于 2014-10-13 08:45:54 |只看作者 |坛友微信交流群
Imasasor 发表于 2014-10-11 10:05
stata做出的森林图,导出eps格式,该格式为矢量图格式,用adobe illustrator打开,可以随心所欲的随便修饰 ...
  1.                                                                                                       
  2. data anno_table; set forest_data;                                                                                                      
  3. length function color $8 style $20 text $30;                                                                                            
  4. xsys='1'; ysys='2'; when='a';                                                                                                           
  5. function='label'; style='albany amt';                                                                                                   
  6. y=data_order;                                                                                                                           
  7. x=95; text=trim(left(put(p,comma8.2))); output;                                                                                         
  8. x=x-8; text=trim(left(put(other,comma8.1))); output;                                                                                    
  9. x=x-8; text=trim(left(put(pci,comma8.1))); output;                                                                                      
  10. x=20; text=trim(left(no_pct)); output;                                                                                                  
  11. run;                                                                                                                                    
  12.                                                                                                                                        
  13. proc sql;                                                                                                                              
  14. select count(*) into :bar_cnt from forest_data;                                                                                         
  15. quit; run;                                                                                                                              
  16. data anno_alternating_bars;                                                                                                            
  17. length function color $8 style $20 text $30;                                                                                            
  18. xsys='1'; ysys='1'; when='b';                                                                                                           
  19. do loop = 1 to &bar_cnt;                                                                                                               
  20. function='move'; x=0; y=(loop-1)*(100/&bar_cnt); color='';                                                                             
  21. output;                                                                                                                                
  22. function='bar'; style='solid'; x=100; y=(loop)*(100/&bar_cnt);                                                                        
  23. if mod (loop,3) = 0 then color="cxf0f1f1";  /* gray */                                                                                 
  24. else color="white";                                                                                                                    
  25. output;                                                                                                                                
  26. end;                                                                                                                                   
  27. run;                                                                                                                                    
  28.                                                                                                                                        
  29. data anno_all; set anno_box anno_headers anno_table anno_alternating_bars anno_lines;                                                   
  30. run;                                                                                                                                    
  31.                                                                                                                                        
  32.                                                                                                                                        
  33. goptions device=png ypixels=600 xpixels=900;                                                                                            
  34. goptions gunit=pct htitle=17pt htext=9pt;                                                                                               
  35.                                                                                                                                        
  36. ODS LISTING CLOSE;                                                                                                                     
  37. ODS HTML path=odsout body="&name..htm"                                                                                                  
  38. (title="SAS/Graph custom Gplot Forest plot") style=sasweb;                                                                             
  39.                                                                                                                                        
  40. axis1 label=none major=none minor=none offset=(2.5,2.5);                                                                                
  41.                                                                                                                                        
  42. axis2 label=none order=(0 .5 1.0 1.5 2.0) offset=(30,30)                                                                                
  43. /* hard-coding axis values is a little dangerous - if you change the                                                                  
  44.     data, then be sure to change (or delete) these hard-coded values.                                                                  
  45.     (if I didn't use them, then each axis value would show 2 decimal                                                                    
  46.     decimal places) */                                                                                                                  
  47. value=(t=1 '0' t=2 '0.5' t=3 '1' t=4 '1.5' t=5 '2.0')                                                                                 
  48. ;                                                                                                                                      
  49.                                                                                                                                        
  50. symbol1 value=point h=3 interpol=none c=black;                                                                                          
  51.                                                                                                                                        
  52. title1 ls=1.5 "Impact of Treatment on Mortality by Study";                                                                              
  53. title2 ls=.6 h=13pt "Odds Ratio and 95% CL";                                                                                            
  54. title3 h=13pct " "; /* blank space above the graph, for the annotated column labels */                                                  
  55.                                                                                                                                        
  56. proc gplot data=forest_data anno=anno_all;                                                                                             
  57. format data_order forfmt.;                                                                                                              
  58. plot                                                                                                                                    
  59. data_order*LowerCL=1                                                                                                                  
  60. data_order*UpperCL=1                                                                                                                  
  61. / overlay                                                                                                                              
  62. vreverse                                                                                                                              
  63. vaxis=axis1                                                                                                                           
  64. haxis=axis2                                                                                                                           
  65. autohref chref=graydd                                                                                                                  
  66. des='' name="&name";                                                                                                                  
  67. run;                                                                                                                                    
  68.                                                                                                                                        
  69. quit;                                                                                                                                   
  70. ODS HTML CLOSE;                                                                                                                        
  71. ODS LISTING;
复制代码

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-20 02:38