楼主: l1i2n3i4n5g
48673 125

[学习分享] 如何用SAS画统计图,这是我的学习分享!   [推广有奖]

81
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-14 09:57:07
Time Axis Features
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Time Axis Features
  3.             (using template and sgrender procedure)
  4.    Software: SAS 9.4 TS Level 1M2
  5.    Name: justsoso
  6.    Date: 20180305
  7.    From: Getting Started with the Graph Template Language in SAS:
  8.          Examples, Tips, and Techniques for Creating Custom Graphs
  9.    History: 20180305;
  10. %mend;

  11. ods _all_ close;     /*关闭所有ods destinations,避免不必要的输出,节省计算机资源*/
  12. /*ods listing gpath='C:\Users\Administrator\Desktop\test20180305' image_dpi=300;*/
  13. ods listing gpath='C:\Users\Administrator\Desktop\test20180305';
  14. /*打开ods listing这一destination,指定图片保存路径,★★★★★需提前建立*/
  15. /*ods graphics on / imagename="picture" outputfmt=pdf;*/
  16. /*图片名称picture,输出类型pdf*/
  17. ods graphics off;

  18. /*Time Axis Features*/
  19. proc template;
  20. define statgraph Fig_6_2_5;
  21. begingraph;
  22. entrytitle 'Monthly Stock Prices by Company';
  23. layout overlay / xaxisopts=( display=(ticks tickvalues)
  24.       timeopts=(interval=semimonth minorticks=true))
  25.       y2axisopts=(label='Price' griddisplay=on);
  26.    highlowplot x=date high=high low=low / close=close
  27.       yaxis=y2 lineattrs=graphdata2(thickness=3) ;
  28. endlayout;
  29. endgraph;
  30. end;
  31. run;
  32. ods listing style=htmlblue;
  33. proc sgrender template=Fig_6_2_5
  34. data=sashelp.stocks(where=(date > '01jan2004'd));
  35. where stock eq 'IBM';
  36. run;

  37. ods _all_ close;
  38. /*ods html;      最好将ods destination恢复至默认的html*/
复制代码


82
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-14 10:16:24
Discrete Legend
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Discrete Legend
  3.             (using template and sgrender procedure)
  4.    Software: SAS 9.4 TS Level 1M2
  5.    Name: justsoso
  6.    Date: 20180305
  7.    From: Getting Started with the Graph Template Language in SAS:
  8.          Examples, Tips, and Techniques for Creating Custom Graphs
  9.    History: 20180305;
  10. %mend;

  11. ods trace on;
  12. ods output Survivalplot=GTL_GS_SurvivalPlotData;
  13. ods graphics / reset imagename='Survival-LifeTest';
  14. proc lifetest data=sashelp.BMT plots=survival(atrisk=0 to 2500 by 500);
  15.    time T * Status(0);
  16.    strata Group / test=logrank adjust=sidak;
  17.    run;
  18. ods trace off;

  19. proc format;
  20.   value aml
  21.   3 = 'AML-Low'
  22.   2 = 'AML-High'
  23.   1 = 'All';
  24. run;

  25. ods _all_ close;     /*关闭所有ods destinations,避免不必要的输出,节省计算机资源*/
  26. /*ods listing gpath='C:\Users\Administrator\Desktop\test20180305' image_dpi=300;*/
  27. ods listing gpath='C:\Users\Administrator\Desktop\test20180305';
  28. /*打开ods listing这一destination,指定图片保存路径,★★★★★需提前建立*/
  29. /*ods graphics on / imagename="picture" outputfmt=pdf;*/
  30. /*图片名称picture,输出类型pdf*/
  31. ods graphics off;

  32. /*Discrete Legend*/
  33. proc template;
  34. define statgraph Fig_6_3_1;
  35. begingraph;
  36. entrytitle 'Product-Limit Survival Estimates';
  37. legendItem type=marker name='c' / label='Censored'
  38. markerattrs=(symbol=plus);
  39. layout overlay;
  40.    stepplot x=time y=survival / group=stratum
  41.       lineattrs=(pattern=solid) name='s';
  42.    scatterplot x=time y=censored / group=stratum
  43.       markerattrs=(symbol=plus);
  44.    innermargin;/*这个部分还需要进一步学习*/
  45.       blockplot x=tatrisk block=atrisk / class=stratumnum
  46.          display=(values label) valueattrs=(size=8)
  47.          labelattrs=(size=8);
  48.    endinnermargin;
  49.    discretelegend 'c'/ location=inside halign=right valign=top;
  50.    discretelegend 's' / valueattrs=(size=8);
  51. endlayout;
  52. endgraph;
  53. end;
  54. run;
  55. proc sgrender data=GTL_GS_SurvivalPlotData template=Fig_6_3_1;
  56. format stratumnum aml.;
  57. run;

  58. ods _all_ close;
  59. /*ods html;      最好将ods destination恢复至默认的html*/
复制代码


83
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-14 10:21:40
Continuous Legend
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Continuous Legend
  3.             (using template and sgrender procedure)
  4.    Software: SAS 9.4 TS Level 1M2
  5.    Name: justsoso
  6.    Date: 20180305
  7.    From: Getting Started with the Graph Template Language in SAS:
  8.          Examples, Tips, and Techniques for Creating Custom Graphs
  9.    History: 20180305;
  10. %mend;

  11. data GTL_GS_HeatmapParm;
  12.   drop pi val;
  13.   pi=constant('Pi');
  14.   do x=1 to 20;
  15.     do y=1 to 10;
  16.           value=sin(x*pi/10) + cos(y*pi/10);
  17.           val=ranuni(2);
  18.       group=ifc(val < 0.5, 'M', 'F');
  19.           output;
  20.         end;
  21.   end;
  22. run;
  23. /*proc print data=GTL_GS_HeatmapParm; run;*/

  24. ods _all_ close;     /*关闭所有ods destinations,避免不必要的输出,节省计算机资源*/
  25. /*ods listing gpath='C:\Users\Administrator\Desktop\test20180305' image_dpi=300;*/
  26. ods listing gpath='C:\Users\Administrator\Desktop\test20180305';
  27. /*打开ods listing这一destination,指定图片保存路径,★★★★★需提前建立*/
  28. /*ods graphics on / imagename="picture" outputfmt=pdf;*/
  29. /*图片名称picture,输出类型pdf*/
  30. ods graphics off;

  31. /*Continuous Legend*/
  32. proc template;
  33. define statgraph Fig_6_3_2;
  34. begingraph;
  35. entrytitle "Basic Heat Map";
  36. layout overlay / xaxisopts=(display=(ticks tickvalues))
  37.       yaxisopts=(display=(ticks tickvalues));
  38.    heatmapparm x=x y=y colorresponse=value / name='a';
  39.    continuouslegend 'a';
  40. endlayout;
  41. endgraph;
  42. end;
  43. run;
  44. proc sgrender data=GTL_GS_HeatmapParm template=Fig_6_3_2;
  45. run;
  46. ods _all_ close;
  47. /*ods html;      最好将ods destination恢复至默认的html*/
复制代码


84
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-14 10:29:14
Titles and Footnotes
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Titles and Footnotes
  3.             (using template and sgrender procedure)
  4.    Software: SAS 9.4 TS Level 1M2
  5.    Name: justsoso
  6.    Date: 20180305
  7.    From: Getting Started with the Graph Template Language in SAS:
  8.          Examples, Tips, and Techniques for Creating Custom Graphs
  9.    History: 20180305;
  10. %mend;

  11. ods _all_ close;     /*关闭所有ods destinations,避免不必要的输出,节省计算机资源*/
  12. /*ods listing gpath='C:\Users\Administrator\Desktop\test20180305' image_dpi=300;*/
  13. ods listing gpath='C:\Users\Administrator\Desktop\test20180305';
  14. /*打开ods listing这一destination,指定图片保存路径,★★★★★需提前建立*/
  15. /*ods graphics on / imagename="picture" outputfmt=pdf;*/
  16. /*图片名称picture,输出类型pdf*/
  17. ods graphics off;

  18. /*Titles and Footnotes*/
  19. proc template;
  20. define statgraph Fig_6_4_1;
  21. begingraph;
  22. entrytitle halign=left textattrs=(color=blue) 'Item 1:'
  23.    halign=center textattrs=graphtitletext 'Mileage by Type';
  24. entryfootnote halign=left 'Excluding Hybrids';
  25. layout overlay / xaxisopts=(display=(ticks tickvalues))
  26.       yaxisopts=(griddisplay=on);
  27.    barchart x=type y=mpg_city / stat=mean fillattrs=graphdata3
  28.       dataskin=gloss;
  29. endlayout;
  30. endgraph;
  31. end;
  32. run;
  33. proc sgrender template=Fig_6_4_1
  34. data=sashelp.cars(where=(type ne 'Hybrid'));
  35. run;

  36. ods _all_ close;
  37. /*ods html;      最好将ods destination恢复至默认的html*/
复制代码


85
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-14 10:38:01
Entries
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Entries
  3.             (using template and sgrender procedure)
  4.    Software: SAS 9.4 TS Level 1M2
  5.    Name: justsoso
  6.    Date: 20180305
  7.    From: Getting Started with the Graph Template Language in SAS:
  8.          Examples, Tips, and Techniques for Creating Custom Graphs
  9.    History: 20180305;
  10. %mend;

  11. ods _all_ close;     /*关闭所有ods destinations,避免不必要的输出,节省计算机资源*/
  12. /*ods listing gpath='C:\Users\Administrator\Desktop\test20180305' image_dpi=300;*/
  13. ods listing gpath='C:\Users\Administrator\Desktop\test20180305';
  14. /*打开ods listing这一destination,指定图片保存路径,★★★★★需提前建立*/
  15. /*ods graphics on / imagename="picture" outputfmt=pdf;*/
  16. /*图片名称picture,输出类型pdf*/
  17. ods graphics off;

  18. /*Entries*/
  19. proc template; /*--SAS 9.4--*/
  20. define statgraph Fig_6_4_2;
  21. begingraph / subpixel=on;
  22. entrytitle 'Car Statistics';
  23. layout overlay / yaxisopts=(griddisplay=on)
  24.       xaxisopts=(griddisplay=on);
  25.    scatterplot x=horsepower y=mpg_city / filledoutlinedmarkers=true
  26.       dataskin=gloss markerattrs=(size=13 symbol=circlefilled)
  27.       markerfillattrs=graphdata2;
  28.    layout gridded / rows=2 order=columnmajor border=true
  29.          autoalign=(topright);
  30.       entry textattrs=(size=10) halign=right 'N = ';
  31.       entry textattrs=(size=10) halign=right {unicode SIGMA}
  32.          {sub '1'} ' =';
  33.       entry textattrs=(size=10) halign=left
  34.          eval(strip(put(n(mpg_city),4.0)));
  35.       entry textattrs=(size=10) halign=left
  36.          eval(strip(put(stddev(mpg_city),4.1)));
  37.    endlayout;
  38.    regressionplot x=horsepower y=mpg_city / degree=2
  39.       lineattrs=(color=black thickness=4);
  40. endlayout;
  41. endgraph;
  42. end;
  43. run;
  44. proc sgrender template=Fig_6_4_2 data=sashelp.cars; run;

  45. ods _all_ close;
  46. /*ods html;      最好将ods destination恢复至默认的html*/
复制代码


86
天堂的狗坚强(真实交易用户) 发表于 2018-3-19 15:36:32
公卫的道友?学习了,很有用,一直想学sas绘图,回头好好看看分析资料

87
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-20 10:09:26
Discrete Attributes Map – Line Attributes
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Discrete Attributes Map – Line Attributes
  3.             (using template and sgrender procedure)
  4.    Software: SAS 9.4 TS Level 1M2
  5.    Name: justsoso
  6.    Date: 20180305
  7.    From: Getting Started with the Graph Template Language in SAS:
  8.          Examples, Tips, and Techniques for Creating Custom Graphs
  9.    History: 20180305;
  10. %mend;

  11. /*--Fig_7_2_1 Group Series Discrete Attr Map--*/
  12. data GTL_GS_Series_Dis_Attr_Map;
  13. format Date Date9.;
  14. do i=0 to 334 by 30;
  15. date='01jan2009'd+i;
  16. if mod (i, 30) =0 then freq=1; else freq=0;
  17. Drug='Drug A'; Val = 16+ 3*sin(i/90+0.5) + 1*sin(3*i/90+0.7); output;
  18. Drug='Drug C'; Val =  6+ 3*cos(i/90+0.5) + 1*sin(3*i/90+0.7); output;
  19. end;
  20. run;

  21. ods _all_ close;     /*关闭所有ods destinations,避免不必要的输出,节省计算机资源*/
  22. /*ods listing gpath='C:\Users\Administrator\Desktop\test20180308' image_dpi=300;*/
  23. ods listing gpath='C:\Users\Administrator\Desktop\test20180308';
  24. /*打开ods listing这一destination,指定图片保存路径,★★★★★需提前建立*/
  25. /*ods graphics on / imagename="picture" outputfmt=pdf;*/
  26. /*图片名称picture,输出类型pdf*/
  27. ods graphics off;

  28. /*Discrete Attributes Map – Line Attributes*/
  29. proc template; /*--SAS 9.4--*/
  30. define statgraph Fig_7_2_1;
  31. dynamic _title;
  32. begingraph / subpixel=on;/*subpixel=on使连接线更加光滑*/
  33. discreteattrmap name='drug';
  34.    value 'Drug A' / lineattrs=(color=red pattern=solid);
  35.    value 'Drug B' / lineattrs=(color=green pattern=solid);
  36.    value 'Drug C' / lineattrs=(color=blue pattern=solid);
  37. enddiscreteattrmap;
  38. discreteattrvar attrvar=drugname var=drug attrmap='drug';/*attrvar=drugname用于group*/
  39. entrytitle 'Response by Time and Treatment';
  40. layout overlay / yaxisopts=(griddisplay=on label='Response')
  41.       xaxisopts=(griddisplay=on display=(ticks tickvalues));
  42.    seriesplot x=date y=val / group=drugname curvelabel=drug name='a'
  43.       smoothconnect=true lineattrs=(thickness=2);/*smoothconnect=true使连接线更加光滑*/
  44.    discretelegend 'drug' / type=line;
  45. endlayout;
  46. endgraph;
  47. end;
  48. run;

  49. proc sgrender data=GTL_GS_Series_Dis_Attr_Map template=Fig_7_2_1; run;

  50. ods _all_ close;
  51. /*ods html;      最好将ods destination恢复至默认的html*/
复制代码


88
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-21 13:12:26
Discrete Attributes Map – Fill Attributes
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Discrete Attributes Map – Fill Attributes
  3.             (using template and sgrender procedure)
  4.    Software: SAS 9.4 TS Level 1M2
  5.    Name: justsoso
  6.    Date: 20180308
  7.    From: Getting Started with the Graph Template Language in SAS:
  8.          Examples, Tips, and Techniques for Creating Custom Graphs
  9.    History: 20180308;
  10. %mend;

  11. /*--7_2_2 HIGHLOW Discrete Attr Map--*/
  12. data GTL_GS_highlow_Dis_Attr_Map;
  13. length cap $ 12;
  14. input drug $ low high cap $;
  15. datalines;
  16. A      10 20 NONE
  17. A      30 60 NONE
  18. B      20 35 NONE
  19. B      50 75 NONE
  20. C      30 90 FILLEDARROW
  21. ;
  22. run;

  23. ods _all_ close;     /*关闭所有ods destinations,避免不必要的输出,节省计算机资源*/
  24. /*ods listing gpath='C:\Users\Administrator\Desktop\test20180308' image_dpi=300;*/
  25. ods listing gpath='C:\Users\Administrator\Desktop\test20180308';
  26. /*打开ods listing这一destination,指定图片保存路径,★★★★★需提前建立*/
  27. /*ods graphics on / imagename="picture" outputfmt=pdf;*/
  28. /*图片名称picture,输出类型pdf*/
  29. ods graphics off;

  30. /*Discrete Attributes Map – Fill Attributes*/
  31. proc template;
  32. define statgraph Fig_7_2_2;
  33. dynamic _title;
  34. begingraph;
  35.    discreteattrmap name='drug';
  36.       value 'A' / fillattrs=(color=lilac);
  37.       value 'B' / fillattrs=(color=powderblue);
  38.       value 'C' / fillattrs=(color=gold);
  39.    enddiscreteattrmap;
  40.    discreteattrvar attrvar=drugname var=drug attrmap='drug';
  41.    entrytitle 'Response by Time and Treatment';
  42.    layout overlay / yaxisopts=(griddisplay=on label='Response')
  43.          xaxisopts=(griddisplay=on display=(ticks tickvalues));
  44.       highlowplot y=drug high=high low=low / type=bar highcap=cap
  45.          group=drugname outlineattrs=(color=black) name='a';
  46.       discretelegend 'a';
  47.    endlayout;
  48. endgraph;
  49. end;
  50. run;
  51. proc sgrender data=GTL_GS_highlow_Dis_Attr_Map template=Fig_7_2_2;
  52. run;

  53. ods _all_ close;
  54. /*ods html;      最好将ods destination恢复至默认的html*/
复制代码


89
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-21 13:23:46
Range Attributes Map – Discrete Colors
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Range Attributes Map – Discrete Colors
  3.             (using template and sgrender procedure)
  4.    Software: SAS 9.4 TS Level 1M2
  5.    Name: justsoso
  6.    Date: 20180308
  7.    From: Getting Started with the Graph Template Language in SAS:
  8.          Examples, Tips, and Techniques for Creating Custom Graphs
  9.    History: 20180308;
  10. %mend;

  11. data GTL_GS_HeatmapParm;
  12.   drop pi val;
  13.   pi=constant('Pi');
  14.   do x=1 to 20;
  15.     do y=1 to 10;
  16.           value=sin(x*pi/10) + cos(y*pi/10);
  17.           val=ranuni(2);
  18.       group=ifc(val < 0.5, 'M', 'F');
  19.           output;
  20.         end;
  21.   end;
  22. run;
  23. /*proc print data=GTL_GS_HeatmapParm; run;*/

  24. ods _all_ close;     /*关闭所有ods destinations,避免不必要的输出,节省计算机资源*/
  25. /*ods listing gpath='C:\Users\Administrator\Desktop\test20180308' image_dpi=300;*/
  26. ods listing gpath='C:\Users\Administrator\Desktop\test20180308';
  27. /*打开ods listing这一destination,指定图片保存路径,★★★★★需提前建立*/
  28. /*ods graphics on / imagename="picture" outputfmt=pdf;*/
  29. /*图片名称picture,输出类型pdf*/
  30. ods graphics off;

  31. /*Range Attributes Map – Discrete Colors*/
  32. proc template;
  33. define statgraph Fig_7_3_1;
  34. begingraph;
  35.    rangeattrmap name='Resp';
  36.       range -2 - -1 / rangecolor=darkred;
  37.       range -1 - 0 / rangecolor=red;
  38.       range 0 - 1 / rangecolor=white;
  39.       range 1 - 2 / rangecolor=blue;
  40.    endrangeattrmap;
  41.    rangeattrvar attrvar=AttrValue var=value attrmap='Resp';
  42.    entrytitle "Heat Map with Discrete Range Attribute Map";
  43.    layout overlay / xaxisopts=(display=(ticks tickvalues))
  44.          yaxisopts=(display=(ticks tickvalues));
  45.       heatmapparm x=x y=y colorresponse=AttrValue / name='a';
  46.       continuouslegend 'a';
  47.    endlayout;
  48. endgraph;
  49. end;
  50. run;
  51. proc sgrender data=GTL_GS_HeatmapParm template=Fig_7_3_1;
  52. run;

  53. ods _all_ close;
  54. /*ods html;      最好将ods destination恢复至默认的html*/
复制代码


90
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-21 13:25:16
Range Attributes Map – Gradient Colors
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Range Attributes Map – Gradient Colors
  3.             (using template and sgrender procedure)
  4.    Software: SAS 9.4 TS Level 1M2
  5.    Name: justsoso
  6.    Date: 20180308
  7.    From: Getting Started with the Graph Template Language in SAS:
  8.          Examples, Tips, and Techniques for Creating Custom Graphs
  9.    History: 20180308;
  10. %mend;

  11. data GTL_GS_HeatmapParm;
  12.   drop pi val;
  13.   pi=constant('Pi');
  14.   do x=1 to 20;
  15.     do y=1 to 10;
  16.           value=sin(x*pi/10) + cos(y*pi/10);
  17.           val=ranuni(2);
  18.       group=ifc(val < 0.5, 'M', 'F');
  19.           output;
  20.         end;
  21.   end;
  22. run;
  23. /*proc print data=GTL_GS_HeatmapParm; run;*/

  24. ods _all_ close;     /*关闭所有ods destinations,避免不必要的输出,节省计算机资源*/
  25. /*ods listing gpath='C:\Users\Administrator\Desktop\test20180308' image_dpi=300;*/
  26. ods listing gpath='C:\Users\Administrator\Desktop\test20180308';
  27. /*打开ods listing这一destination,指定图片保存路径,★★★★★需提前建立*/
  28. /*ods graphics on / imagename="picture" outputfmt=pdf;*/
  29. /*图片名称picture,输出类型pdf*/
  30. ods graphics off;

  31. /*Range Attributes Map – Gradient Colors*/
  32. proc template;
  33. define statgraph Fig_7_3_2;
  34. begingraph;
  35.    rangeattrmap name='Resp';
  36.       range -2 - -1 / rangecolormodel=(darkred red);
  37.       range -1 - 0 / rangecolormodel=(red white);
  38.       range 0 - 1 / rangecolormodel=(white blue);
  39.       range 1 - 2 / rangecolormodel=(blue darkblue);
  40.    endrangeattrmap;
  41.    rangeattrvar attrvar=AttrValue var=value attrmap='Resp';
  42.    entrytitle "Basic Heat Map";
  43.    layout overlay / xaxisopts=(display=(ticks tickvalues))
  44.          yaxisopts=(display=(ticks tickvalues));
  45.       heatmapparm x=x y=y colorresponse=AttrValue / name='a';
  46.       continuouslegend 'a';
  47.    endlayout;
  48. endgraph;
  49. end;
  50. run;
  51. proc sgrender data=GTL_GS_HeatmapParm template=Fig_7_3_2;
  52. run;

  53. ods _all_ close;
  54. /*ods html;      最好将ods destination恢复至默认的html*/
复制代码


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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-16 23:22