楼主: l1i2n3i4n5g
48359 125

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

31
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-1 15:24:55
Vector Plot (using template and sgrender procedure)
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Vector 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_vector,用于后续画图*/
  11. data GTL_GS_vector;
  12. format label 3.0;
  13. do i=1 to 10;
  14.    x=ranuni(2); xo=x*(1+(ranuni(2)-0.5));
  15.    y=ranuni(2); yo=y*(1+(ranuni(2)-0.5));
  16.    if (ranuni(2) < 0.5) then Type='A';
  17.    else Type='B';
  18.    label=(2*x+3*y);
  19.    output;
  20. end;
  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. /*Vector Plot*/
  31. proc template;
  32. define statgraph Fig_4_2_9;
  33. begingraph;
  34. entrytitle 'Random Vectors';
  35. layout overlay;
  36.    vectorplot x=x y=y xorigin=xo yorigin=yo / group=type name='a'
  37.       arrowheadshape=barbed datalabel=label
  38.       lineattrs=(thickness=2);
  39.    discretelegend 'a' / sortorder=ASCENDINGFORMATTED
  40.       title='Type:' location=inside halign=right valign=top;
  41. endlayout;
  42. endgraph;
  43. end;
  44. run;
  45. proc sgrender data=GTL_GS_vector template=Fig_4_2_9;
  46. run;

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


32
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-1 15:37:45
Bar Chart
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Bar Chart (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. /*Bar Chart*/
  19. proc template;
  20. define statgraph Fig_4_3_1;
  21. begingraph;
  22. entrytitle 'Mileage by Origin and Type';
  23. layout overlay / xaxisopts=(display=(ticks tickvalues))
  24.    yaxisopts=(griddisplay=on);
  25.       barchart x=origin y=mpg_city / group=type stat=mean
  26.       groupdisplay=cluster dataskin=gloss name='a';
  27.    discretelegend 'a' / title='Type:';
  28. endlayout;
  29. endgraph;
  30. end;
  31. run;
  32. proc sgrender data=sashelp.cars(where=(type not in ('Hybrid' 'Truck')))
  33. template=Fig_4_3_1;
  34. run;

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


33
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-1 16:03:12
Axis Table
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Axis Table (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. /*Axis Table*/
  19. proc template; /*--SAS 9.4--*/
  20. define statgraph Fig_4_3_2;
  21. begingraph;
  22. entrytitle 'Mileage by Origin and Type';
  23. layout overlay / xaxisopts=(griddisplay=on)
  24.    yaxisopts=(display=(ticks tickvalues)
  25.       discreteopts=(colorbands=even));
  26.    barchart category=type response=mpg_city / orient=horizontal
  27.       groupdisplay=cluster stat=mean dataskin=gloss;
  28.    innermargin / align=right;
  29.    axistable y=type value=mpg_city / class=origin display=(label)
  30.       labelposition=max stat=mean;
  31.    endinnermargin;
  32. endlayout;
  33. endgraph;
  34. end;
  35. run;
  36. proc sgrender data=sashelp.cars template=Fig_4_3_2;
  37. format mpg_city 4.1; run;

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


34
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-1 16:12:31
Line Chart
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Line Chart (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. /*Line Chart*/
  19. proc template; /*--SAS 9.4--*/
  20. define statgraph Fig_4_3_3;
  21. begingraph;
  22. entrytitle 'Mileage by Origin and Type';
  23. layout overlay / xaxisopts=(display=(ticks tickvalues)) yaxisopts=(griddisplay=on);
  24.    linechart category=type response=mpg_city / group=origin   stat=mean
  25.       lineattrs=(thickness=5 pattern=solid) datatransparency=0.4   name='a';
  26.    discretelegend 'a' / title='Origin:';
  27. endlayout;
  28. endgraph;
  29. end;
  30. run;
  31. proc sgrender data=sashelp.cars template=Fig_4_3_3;
  32. run;

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


35
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-2 11:15:25
Pie Chart
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Pie Chart (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_Sedans,用于后续画图*/
  11. proc sort data=sashelp.cars out=CarsCyOrigin;
  12.   by origin;
  13.   run;
  14. data GTL_GS_Sedans;
  15.   retain Sedans 0 Rest 0;
  16.   format Pct percent.;
  17.   keep Origin Type Count Pct;
  18.   set CarsCyOrigin;
  19.   by origin;
  20.   if first.origin then do; Sedans=0; Rest=0; end;
  21.   if type eq 'Sedan' then Sedans+1;
  22.   else  Rest+1;
  23.   if last.origin then do;
  24.    Type='Sedans'; Count=Sedans; Pct=count/(sedans+rest); output;
  25.         Type='Rest';   Count=Rest;   Pct=count/(sedans+rest); output;
  26.   end;
  27. run;

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

  36. /*Pie Chart*/
  37. proc template;
  38. define statgraph Fig_4_3_4;
  39. begingraph;
  40. entrytitle 'Share of Sedans among Cars in USA';
  41. layout region;/*饼图要用layout region!*/
  42.    piechart category=type response=pct / dataskin=sheen
  43.       datalabelcontent=(category response) name='a' start=340;
  44. endlayout;
  45. endgraph;
  46. end;
  47. run;
  48. proc sgrender data=GTL_GS_Sedans(where=(origin='USA')) template=Fig_4_3_4;
  49. run;

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


36
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-2 11:18:22
Histogram
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Histogram (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. /*Histogram*/
  19. proc template;
  20. define statgraph Fig_4_4_1;
  21. begingraph;
  22. entrytitle 'Distribution of Mileage';
  23. layout overlay;
  24.    histogram mpg_city;
  25. endlayout;
  26. endgraph;
  27. end;
  28. run;
  29. proc sgrender data=sashelp.cars(where=(type ne 'Hybrid'))
  30. template=Fig_4_4_1;
  31. run;

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


37
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-2 11:20:15
Density Plots
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Density Plots (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. /*Density Plots*/
  19. proc template;
  20. define statgraph Fig_4_4_2;
  21. begingraph;
  22. entrytitle 'Distribution of Mileage';
  23. layout overlay;
  24.    densityplot mpg_city / name='n' lineattrs=graphfit
  25.       legendlabel='Normal';
  26.    densityplot mpg_city / kernel() name='k' lineattrs=graphfit2
  27.       legendlabel='Kernel';
  28.    discretelegend 'n' 'k' / location=inside across=1
  29.       halign=right valign=top;
  30. endlayout;
  31. endgraph;
  32. end;
  33. run;
  34. proc sgrender data=sashelp.cars(where=(type ne 'Hybrid'))
  35. template=Fig_4_4_2;
  36. run;

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


38
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-2 11:22:10
Box Plot – Horizontal - Discrete
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Box Plot – Horizontal - Discrete (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. /*Box Plot – Horizontal - Discrete*/
  19. proc template;
  20. define statgraph Fig_4_4_3;
  21. begingraph;
  22. entrytitle 'Cholesterol by Death Cause';
  23. layout overlay / yaxisopts=(display=(ticks tickvalues))
  24.       xaxisopts=(griddisplay=on);
  25.    sboxplot y=cholesterol x=deathcause / orient=horizontal;
  26. endlayout;
  27. endgraph;
  28. end;
  29. run;
  30. proc sgrender data=sashelp.heart template=Fig_4_4_3;
  31. run;

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


39
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-2 11:27:43
Box Plot – Vertical – Interval with Groups
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Box Plot – Vertical – Interval with Groups (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_IntervalBoxGroup,用于后续画图*/
  11. data GTL_GS_IntervalBoxGroup;
  12.   format Date date6.;
  13.   drop i;
  14.   do Date='01Jan2009'd, '15Jan2009'd,'15Mar2009'd,
  15.           '01May2009'd, '01Aug2009'd;
  16.     do i=1 to 10;
  17.           Response=rannorm(2); Drug='A'; output;
  18.           Response=ranuni(2);  Drug='A'; output;
  19.           Response=rannorm(2); Drug='B'; output;
  20.           Response=ranuni(2);  Drug='B'; output;
  21.         end;
  22.   end;
  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. /*Box Plot – Vertical – Interval with Groups*/
  33. proc template; /*--SAS 9.4--*/
  34. define statgraph Fig_4_4_4;
  35. begingraph / attrpriority=color;
  36. entrytitle 'Response by Time and Treatment';
  37. layout overlay / yaxisopts=(griddisplay=on)
  38.    xaxisopts=(type=time timeopts=(interval=month)
  39.       display=(ticks tickvalues));
  40.    boxplot x=date y=response / group=drug groupdisplay=cluster
  41.       name='a';
  42.    discretelegend 'a' / title='Treatment:' location=inside
  43.       halign=right valign=bottom;
  44. endlayout;
  45. endgraph;
  46. end;
  47. run;
  48. proc sgrender data=GTL_GS_IntervalBoxGroup template=Fig_4_4_4;
  49. run;

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


40
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-2 11:42:15
Regression Plot
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Regression 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. /*Regression Plot*/
  19. proc template;
  20. define statgraph Fig_4_5_1;
  21. begingraph;
  22. entrytitle 'Fit Plot for Weight by Height';
  23. layout overlay / yaxisopts=(griddisplay=on)
  24.    xaxisopts=(griddisplay=on);
  25.    modelband 'Reg' / name='band' legendlabel='95% Confidence';
  26.    scatterplot x=height y=weight;
  27.    regressionplot x=height y=weight / cli='Reg' name='Reg';
  28.    discretelegend 'band' / location=inside halign=right
  29.       valign=bottom;
  30. endlayout;
  31. endgraph;
  32. end;
  33. run;
  34. proc sgrender data=sashelp.class template=Fig_4_5_1; run;

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


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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-6 06:16