楼主: l1i2n3i4n5g
31805 123

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

41
l1i2n3i4n5g 在职认证  发表于 2018-3-2 11:44:06 |只看作者 |坛友微信交流群
Loess Plot
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Loess 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. /*Loess Plot*/
  19. proc template;
  20. define statgraph Fig_4_5_2;
  21. begingraph;
  22. entrytitle 'Loess Fit Plot Mileage by Horsepower';
  23. layout overlay / yaxisopts=(griddisplay=on)
  24.    xaxisopts=(griddisplay=on);
  25.    modelband 'Loess' / name='band' legendlabel='95% Confidence';
  26.    scatterplot x=horsepower y=mpg_city;
  27.    loessplot x=horsepower y=mpg_city / clm='Loess' name='Loess';
  28.    discretelegend 'band' / location=inside halign=right valign=top;
  29. endlayout;
  30. endgraph;
  31. end;
  32. run;
  33. proc sgrender data=sashelp.cars(where=(type eq 'Sedan'))
  34. template=Fig_4_5_2;
  35. run;

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


使用道具

42
l1i2n3i4n5g 在职认证  发表于 2018-3-2 13:53:13 |只看作者 |坛友微信交流群
Penalized B-Spline Plot
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Penalized B-Spline 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. /*Penalized B-Spline Plot*/
  19. proc template;
  20. define statgraph Fig_4_5_3;
  21. begingraph;
  22. entrytitle 'Spline Fit Plot Mileage by Horsepower';
  23. layout overlay / yaxisopts=(griddisplay=on)
  24.       xaxisopts=(griddisplay=on);
  25.    scatterplot x=horsepower y=mpg_city;
  26.    pbsplineplot x=horsepower y=mpg_city / smooth=1 name='pbs';
  27. endlayout;
  28. endgraph;
  29. end;
  30. run;
  31. proc sgrender data=sashelp.cars(where=(type eq 'Sedan'))
  32. template=Fig_4_5_3;
  33. run;

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


使用道具

43
l1i2n3i4n5g 在职认证  发表于 2018-3-2 13:55:07 |只看作者 |坛友微信交流群
Ellipse Plot
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Ellipse 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. /*Ellipse Plot*/
  19. proc template;
  20. define statgraph Fig_4_5_4;
  21. begingraph;
  22. entrytitle 'Weight by Height';
  23. layout overlay / yaxisopts=(griddisplay=on)
  24.    xaxisopts=(griddisplay=on);
  25.    ellipse x=height y=weight / name='e' type=predicted
  26.       display=(fill outline) legendlabel='Predicted';
  27.    scatterplot x=height y=weight;
  28.    discretelegend 'e' / location=inside halign=right valign=bottom;
  29. endlayout;
  30. endgraph;
  31. end;
  32. run;
  33. proc sgrender data=sashelp.class template=Fig_4_5_4;
  34. run;

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


使用道具

44
l1i2n3i4n5g 在职认证  发表于 2018-3-2 13:57:40 |只看作者 |坛友微信交流群
Reference Line and Drop Line
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Reference Line and Drop Line (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. /*Reference Line and Drop Line*/
  19. proc template;
  20. define statgraph Fig_4_6_1;
  21. begingraph;
  22. entrytitle 'Mileage by Horsepower for Sedans';
  23. layout overlay;
  24.    scatterplot x=horsepower y=mpg_city;
  25.    referenceline y=22 / curvelabel='Mpg=23'
  26.       curvelabellocation=inside;
  27.    dropline x=200 y=30/label='(200,30)' dropto=x
  28.       lineattrs=(pattern=dash);
  29.    dropline x=200 y=30/label='(200,30)' dropto=y
  30.       lineattrs=(pattern=dash);
  31. endlayout;
  32. endgraph;
  33. end;
  34. run;
  35. proc sgrender data=sashelp.cars(where=(type eq 'Sedan'))
  36. template=Fig_4_6_1;
  37. run;

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


使用道具

45
l1i2n3i4n5g 在职认证  发表于 2018-3-2 14:19:31 |只看作者 |坛友微信交流群
Heat Map Parametric Plot
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Reference Line and Drop Line (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. data GTL_GS_HeatmapParm;
  11.   drop pi val;
  12.   pi=constant('Pi');
  13.   do x=1 to 20;
  14.     do y=1 to 10;
  15.           value=sin(x*pi/10) + cos(y*pi/10);
  16.           val=ranuni(2);
  17.       group=ifc(val < 0.5, 'M', 'F');
  18.           output;
  19.         end;
  20.   end;
  21. run;
  22. /*proc print data=GTL_GS_HeatmapParm; run;*/

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

  31. /*Reference Line and Drop Line*/
  32. proc template;
  33. define statgraph Fig_4_6_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_4_6_2;
  45. run;

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


使用道具

46
l1i2n3i4n5g 在职认证  发表于 2018-3-2 14:31:43 |只看作者 |坛友微信交流群
Line Parametric and Ellipse Parametric Plots
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Reference Line and Drop Line (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. data GTL_EllipseParm;
  11.   input x y semimajor semiminor slope group $;
  12.   datalines;
  13. 60  60  90 60  0 M
  14. 50  40  70 40  1 F
  15. ;
  16. run;
  17. data GTL_EllipseParm_Class;
  18.   set sashelp.class GTL_EllipseParm;
  19.   if semimajor=. then semimajor=1;
  20.   if semiminor=. then semiminor=1;
  21.   if x=. then x=-50;
  22.   if y=. then y=-50;
  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. /*Reference Line and Drop Line*/
  33. proc template;
  34. define statgraph Fig_4_6_3;
  35. begingraph;
  36. entrytitle 'Overlay Ellipses with Line';
  37. layout overlayequated / /*x、y轴固定比例*/
  38. xaxisopts=(griddisplay=on display=(ticks tickvalues))
  39. yaxisopts=(griddisplay=on display=(ticks tickvalues));
  40.    scatterplot x=height y=weight / markerattrs=(size=0);
  41.    ellipseparm xorigin=40 yorigin=100 semimajor=60 semiminor=30
  42.       slope=1 / display=(fill outline) fillattrs=graphdata1
  43.       datatransparency=0.5;
  44.    ellipseparm xorigin=100 yorigin=100 semimajor=60 semiminor=30
  45.       slope=-1/ display=(fill outline) fillattrs=graphdata2
  46.       datatransparency=0.5;
  47.    lineparm x=60 y=100 slope=1 / lineattrs=(pattern=dash
  48.       thickness=2);
  49. endlayout;
  50. endgraph;
  51. end;
  52. run;
  53. proc sgrender data=GTL_EllipseParm_Class template=Fig_4_6_3;
  54. run;

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


使用道具

47
l1i2n3i4n5g 在职认证  发表于 2018-3-2 14:42:24 |只看作者 |坛友微信交流群
Contour Plot
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Contour 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. /*Contour Plot*/
  19. proc template;
  20. define statgraph Fig_4_6_4;
  21. begingraph;
  22. entrytitle 'Lake Depth Contours';
  23. layout overlayequated / equatetype=equate
  24.       xaxisopts=(offsetmin=0 offsetmax=0)
  25.       yaxisopts=(offsetmin=0 offsetmax=0);
  26.    contourplotparm x=width y=length z=depth /
  27.    colormodel=twocolorramp contourtype=labeledlinegradient
  28.    linelabelattrs=(size=5) reversecolormodel=true;
  29. endlayout;
  30. endgraph;
  31. end;
  32. run;
  33. proc sgrender data=sashelp.lake template=Fig_4_6_4;
  34. run;

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


使用道具

48
l1i2n3i4n5g 在职认证  发表于 2018-3-2 15:11:14 |只看作者 |坛友微信交流群
3-D Surface Plot
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: 3-D Surface 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. /*GTL_GS_Gridded*/
  11. data nums;
  12.   do i=1 to 30;
  13.      X=10*ranuni(33)-5;
  14.      Y=10*ranuni(35)-5;
  15.      Z=sin(sqrt(x*x+y*y));
  16.      output;
  17.   end;
  18. run;
  19. proc g3grid data=nums out=GTL_GS_Gridded;
  20.   grid y*x=z / spline
  21.                axis1=-5 to 5 by .1
  22.                axis2=-5 to 5 by .1;
  23. run;
  24. proc sort data=GTL_GS_Gridded;
  25. by y x;
  26. run;

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

  35. /*3-D Surface Plot*/
  36. proc template;
  37. define statgraph Fig_4_6_5;
  38. begingraph;
  39. entrytitle 'Surface Plot';
  40. layout overlay3d / cube=false;
  41.    surfaceplotparm x=x y=y z=z / surfacetype=fillgrid;
  42. endlayout;
  43. endgraph;
  44. end;
  45. run;
  46. proc sgrender data=GTL_GS_Gridded template=Fig_4_6_5;
  47. run;

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


使用道具

49
l1i2n3i4n5g 在职认证  发表于 2018-3-2 15:18:31 |只看作者 |坛友微信交流群
3-D Bivariate Histogram
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: 3-D Surface 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. /*GTL_GS_Gridded*/
  11. data heart;
  12.   set sashelp.heart(keep=height weight);
  13.     if height ne . and weight ne .;
  14.   height=round(height,5);
  15.   weight=round(weight,25);
  16. run;
  17. proc summary data=heart nway completetypes;
  18.   class height weight;
  19.   var height;
  20.   output out=GTL_GS_HeartStats(keep=height weight count) N=Count;
  21. run;

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

  30. /*3-D Surface Plot*/
  31. proc template;
  32. define statgraph Fig_4_6_6;
  33. begingraph;
  34. entrytitle "Bivariate Histogram";
  35. layout overlay3d / cube=false;
  36.    bihistogram3dparm x=height y=weight z=count / display=all ;
  37. endlayout;
  38. endgraph;
  39. end;
  40. run;
  41. proc sgrender data=GTL_GS_HeartStats template=Fig_4_6_6;
  42. run;

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


使用道具

50
l1i2n3i4n5g 在职认证  发表于 2018-3-2 15:27:25 |只看作者 |坛友微信交流群
Model Fit Plot
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: 3-D Surface 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. /*3-D Surface Plot*/
  19. proc template;
  20. define statgraph Fig_4_7_1;
  21. begingraph;
  22. entrytitle 'Model Fit Plot';
  23. layout overlay / xaxisopts=(griddisplay=on)
  24.       yaxisopts=(griddisplay=on);
  25.    modelband 'band' / display=(fill) name='b'
  26.       legendlabel='95% Confidence';
  27.    scatterplot x=height y=weight / name='a';
  28.    regressionplot x=height y=weight / cli='band';
  29.    discretelegend 'b' / location=inside halign=right valign=bottom;
  30. endlayout;
  31. endgraph;
  32. end;
  33. run;
  34. proc sgrender data=sashelp.class template=Fig_4_7_1;
  35. run;

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


使用道具

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

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

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

GMT+8, 2024-4-26 15:05