楼主: l1i2n3i4n5g
31785 123

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

51
l1i2n3i4n5g 在职认证  发表于 2018-3-2 15:35:47 |只看作者 |坛友微信交流群
Distribution Plot
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Distribution Plot (using template and sgrender procedure)
  3.    Software: SAS 9.4 TS Level 1M2
  4.    Name: justsoso
  5.    Date: 20180229
  6.    From: Getting Started with the Graph Template Language in SAS:
  7.          Examples, Tips, and Techniques for Creating Custom Graphs
  8.    History: 20180229;
  9. %mend;

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

  18. /*Distribution Plot*/
  19. proc template;
  20. define statgraph Fig_4_7_2;
  21. begingraph;
  22. entrytitle 'Distribution of Mileage';
  23. layout overlay;
  24.    histogram mpg_city / binaxis=false;
  25.    densityplot mpg_city / name='n' lineattrs=graphfit
  26.       legendlabel='Normal';
  27.    densityplot mpg_city / kernel() name='k' lineattrs=graphfit2
  28.       legendlabel='Kernel';
  29.    discretelegend 'n' 'k' / location=inside across=1
  30.       halign=right valign=top;
  31. endlayout;
  32. endgraph;
  33. end;
  34. run;
  35. proc sgrender data=sashelp.cars(where=(type ne 'Hybrid'))
  36. template=Fig_4_7_2;
  37. run;

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


使用道具

52
l1i2n3i4n5g 在职认证  发表于 2018-3-2 15:45:17 |只看作者 |坛友微信交流群
SGRender.png
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Survival Plot (using template and sgrender procedure)
  3.    Software: SAS 9.4 TS Level 1M2
  4.    Name: justsoso
  5.    Date: 20180229
  6.    From: Getting Started with the Graph Template Language in SAS:
  7.          Examples, Tips, and Techniques for Creating Custom Graphs
  8.    History: 20180229;
  9. %mend;

  10. ods trace on;
  11. ods output Survivalplot=GTL_GS_SurvivalPlotData;
  12. ods graphics / reset imagename='Survival-LifeTest';
  13. proc lifetest data=sashelp.BMT plots=survival(atrisk=0 to 2500 by 500);
  14.    time T * Status(0);
  15.    strata Group / test=logrank adjust=sidak;
  16.    run;
  17. ods trace off;
  18. proc format;
  19.   value aml
  20.   3 = 'AML-Low'
  21.   2 = 'AML-High'
  22.   1 = 'All';
  23. run;

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

  32. /*Survival Plot*/
  33. proc template;
  34. define statgraph Fig_4_7_3;
  35. begingraph;
  36. entrytitle 'Product-Limit Survival Estimates';
  37. layout overlay;
  38.    stepplot x=time y=survival / group=stratum
  39.       lineattrs=(pattern=solid) name='s';
  40.    scatterplot x=time y=censored /
  41.       markerattrs=graphdatadefault(symbol=plus) name='c';
  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_4_7_3;
  56. format stratumnum aml.;
  57. run;

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

SGRender.png (17.64 KB)

Survival Plot

Survival Plot

使用道具

53
8112mmw 发表于 2018-3-7 07:46:13 |只看作者 |坛友微信交流群
楼主好厉害,一直在分享自己的知识,谢谢谢谢!

使用道具

54
l1i2n3i4n5g 在职认证  发表于 2018-3-12 15:35:12 |只看作者 |坛友微信交流群
Row and Column Data Lattice
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Row and Column Data Lattice
  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. /*Row and Column Data Lattice*/
  19. proc template;
  20. define statgraph Fig_5_2_1;
  21. begingraph;
  22. entrytitle 'Mileage by Horsepower by Type and Origin';
  23. layout datalattice rowvar=origin columnvar=type /
  24.       headerlabeldisplay=value/*head显示*/
  25.       rowaxisopts=(griddisplay=on)
  26.       columnaxisopts=(griddisplay=on);
  27.    layout prototype;
  28.       scatterplot x=horsepower y=mpg_city / datatransparency=0.7
  29.          markerattrs=(symbol=circlefilled size=11);
  30.    endlayout;
  31. endlayout;
  32. endgraph;
  33. end;
  34. run;
  35. proc sgrender template=Fig_5_2_1
  36. data=sashelp.cars(where=(type not in ('Hybrid', 'Truck')));
  37. run;

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


使用道具

55
l1i2n3i4n5g 在职认证  发表于 2018-3-12 15:56:32 |只看作者 |坛友微信交流群
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Column Data Lattice with Union-All Data Range
  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. /*Column Data Lattice with Union-All Data Range*/
  19. proc template;
  20. define statgraph Fig_5_2_2;
  21. begingraph;
  22. entrytitle 'Mileage by Type and Origin';
  23. layout datalattice columnvar=origin / headerlabeldisplay=value
  24.       rowaxisopts=(griddisplay=on offsetmin=0)
  25.       columnaxisopts=(display=(ticks tickvalues)
  26.       tickvalueattrs=(size=7));
  27.    layout prototype / cycleattrs=true;
  28.       barchart x=type y=mpg_city / stat=mean dataskin=gloss
  29.          datatransparency=0.3 barlabel=true;
  30.    endlayout;
  31. endlayout;
  32. endgraph;
  33. end;
  34. run;
  35. proc sgrender template=Fig_5_2_2
  36. data=sashelp.cars(where=(type not in ('Hybrid', 'Wagon')));
  37. format mpg_city 4.1; run;

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

SGRender34.png (15.07 KB)

Column Data Lattice with Union-All Data Range

Column Data Lattice with Union-All Data Range

使用道具

56
l1i2n3i4n5g 在职认证  发表于 2018-3-12 16:09:11 |只看作者 |坛友微信交流群
 Column Data Lattice with Union Data Range
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Column Data Lattice with Union Data Range
  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. /*Column Data Lattice with Union Data Range*/
  19. proc template;
  20. define statgraph Fig_5_2_3;
  21. begingraph;
  22. entrytitle 'Mileage by Type and Origin';
  23. layout datalattice columnvar=origin / headerlabeldisplay=value
  24.       columndatarange=union/*关键!*/
  25.       rowaxisopts=(griddisplay=on offsetmin=0)
  26.       columnaxisopts=(display=(ticks tickvalues)
  27.       tickvalueattrs=(size=6));
  28.    layout prototype;
  29.       barchart x=type y=mpg_city / stat=mean dataskin=gloss
  30.          datatransparency=0.3 barlabel=true;
  31.    endlayout;
  32. endlayout;
  33. endgraph;
  34. end;
  35. run;
  36. proc sgrender template=Fig_5_2_3
  37. data=sashelp.cars(where=(type not in ('Hybrid', 'Wagon')));
  38. format mpg_city 4.1;
  39. run;

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


使用道具

57
l1i2n3i4n5g 在职认证  发表于 2018-3-12 16:11:46 |只看作者 |坛友微信交流群
Proportional Data Lattice
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Proportional Data Lattice
  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. /*Proportional Data Lattice*/
  19. proc template; /*--SAS 9.4--*/
  20. define statgraph Fig_5_2_4;
  21. begingraph;
  22. entrytitle 'Mileage by Type and Origin';
  23. layout datalattice columnvar=origin / headerlabeldisplay=value
  24.       columnweight=proportional/*关键!*/
  25.       rowaxisopts=(griddisplay=on offsetmin=0)
  26.       columnaxisopts=(display=(ticks tickvalues)
  27.       tickvalueattrs=(size=7));
  28.    layout prototype;
  29.       barchart x=type y=mpg_city / stat=mean dataskin=gloss
  30.          barlabel=true;
  31.    endlayout;
  32. endlayout;
  33. endgraph;
  34. end;
  35. run;
  36. proc sgrender template=Fig_5_2_4
  37. data=sashelp.cars(where=(type not in ('Hybrid', 'Wagon')));
  38. format mpg_city 4.1; run;

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


使用道具

58
l1i2n3i4n5g 在职认证  发表于 2018-3-12 16:38:48 |只看作者 |坛友微信交流群
Data Lattice with Side Bar
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Data Lattice with Side Bar
  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. /*Data Lattice with Side Bar*/
  19. proc template;
  20. define statgraph Fig_5_2_5;
  21. begingraph;
  22. entrytitle 'Mileage by Type and Origin';
  23. layout datalattice columnvar=origin / headerlabeldisplay=value
  24.       rowaxisopts=(label='Mileage' griddisplay=on offsetmin=0)
  25.       columnaxisopts=(display=(ticks tickvalues)
  26.       tickvalueattrs=(size=7));
  27.    layout prototype / cycleattrs=true;
  28.       barchart x=type y=mpg_city / stat=mean dataskin=gloss
  29.          datatransparency=0.3 name='c' legendlabel='City';
  30.       barchart x=type y=mpg_highway / stat=mean dataskin=gloss
  31.          barwidth=0.5 datatransparency=0.3 name='h'
  32.          legendlabel='Highway';
  33.    endlayout;
  34.    sidebar / spacefill=false;/* if true,框框会变得很长!*/
  35.       discretelegend 'c' 'h';
  36.    endsidebar;
  37. endlayout;
  38. endgraph;
  39. end;
  40. run;
  41. proc sgrender template=Fig_5_2_5
  42. data=sashelp.cars(where=(type not in ('Hybrid', 'Truck', 'Wagon')));
  43. run;

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


使用道具

59
l1i2n3i4n5g 在职认证  发表于 2018-3-13 09:04:24 |只看作者 |坛友微信交流群
Row Data Lattice
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Row Data Lattice (using template and sgrender procedure)
  3.    Software: SAS 9.4 TS Level 1M2
  4.    Name: justsoso
  5.    Date: 20180305
  6.    From: Getting Started with the Graph Template Language in SAS:
  7.          Examples, Tips, and Techniques for Creating Custom Graphs
  8.    History: 20180305;
  9. %mend;

  10. /*建立数据集GTL_GS_Labs,用于后续画图*/
  11. /*--Fig 5.2.6 Labs--*/
  12. data GTL_GS_Labs;
  13.   pi=constant('PI');
  14.   do Lab='Lab-1', 'Lab-2', 'Lab-3';
  15.     amp=100*ranuni(3);
  16.         phase=pi*ranuni(2);
  17.     do day=1 to 300 by 5;
  18.           Value=amp*(1+0.1*(sin(day*pi/180+phase)+0.2*ranuni(2)));
  19.           output;
  20.         end;
  21.   end;
  22. run;
  23. /*proc print;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. /*Row Data Lattice*/
  32. proc template;
  33. define statgraph Fig_5_2_6;
  34. begingraph;
  35. entrytitle 'Lab Value by Day';
  36. layout datalattice rowvar=lab / headerlabeldisplay=value/*默认为lab=...*/
  37.       rowaxisopts=(label='Value' griddisplay=on
  38.       tickvalueattrs=(size=7))
  39.       columnaxisopts=(display=(ticks tickvalues)
  40.       tickvalueattrs=(size=7))
  41.       rowdatarange=union rowgutter=5;/*根据单个cell,rowgutter单元间隔,5应该是px*/
  42.    layout prototype / cycleattrs=true;/*cycleattrs指定精美样式*/
  43.       seriesplot x=day y=value / lineattrs=(thickness=2);
  44.    endlayout;
  45. endlayout;
  46. endgraph;
  47. end;
  48. run;
  49. proc sgrender template=Fig_5_2_6 data=GTL_GS_Labs;
  50. run;

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


使用道具

60
l1i2n3i4n5g 在职认证  发表于 2018-3-13 09:26:40 |只看作者 |坛友微信交流群
Data Lattice with Multi-Page Output1 Data Lattice with Multi-Page Output2
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Data Lattice with Multi-Page Output (using template and sgrender procedure)
  3.    Software: SAS 9.4 TS Level 1M2
  4.    Name: justsoso
  5.    Date: 20180305
  6.    From: Getting Started with the Graph Template Language in SAS:
  7.          Examples, Tips, and Techniques for Creating Custom Graphs
  8.    History: 20180305;
  9. %mend;

  10. /*建立数据集GTL_GS_Labs,用于后续画图*/
  11. /*--Fig 5.2.7 Labs2--*/
  12. data GTL_GS_Labs2;
  13.   pi=constant('PI');
  14.   do Lab='Lab-1', 'Lab-2', 'Lab-3', 'Lab-4', 'Lab-5', 'Lab-6';
  15.     amp=100*ranuni(4);
  16.         phase=2*pi*ranuni(3);
  17.     do day=1 to 300 by 5;
  18.           Value=amp*(1+0.1*(sin(day*pi/180+phase)+0.2*ranuni(5)));
  19.           output;
  20.         end;
  21.   end;
  22. run;

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

  30. /*Data Lattice with Multi-Page Output*/
  31. proc template;
  32. define statgraph Fig_5_2_7;
  33. begingraph;
  34. entrytitle 'Lab Value by Day';
  35. layout datalattice rowvar=lab / headerlabeldisplay=value
  36.       rowaxisopts=(label='Value' griddisplay=on
  37.       tickvalueattrs=(size=7))
  38.       columnaxisopts=(display=(ticks tickvalues)
  39.       tickvalueattrs=(size=7))
  40.       rowdatarange=union rows=3 rowgutter=5;/*rows=3限制单元高度*/
  41.    layout prototype / cycleattrs=true;
  42.       seriesplot x=day y=value /
  43.       lineattrs=graphdata2(thickness=2 pattern=solid);
  44.    endlayout;
  45. endlayout;
  46. endgraph;
  47. end;
  48. run;
  49. proc sgrender template=Fig_5_2_7 data=GTL_GS_Labs2;
  50. run;

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


使用道具

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

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

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

GMT+8, 2024-4-25 09:55