楼主: l1i2n3i4n5g
48675 125

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

61
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-13 10:50:47
Two Class Data Panel
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Two Class Data Panel (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. ods _all_ close;     /*关闭所有ods destinations,避免不必要的输出,节省计算机资源*/
  11. /*ods listing gpath='C:\Users\Administrator\Desktop\test20180305' image_dpi=300;*/
  12. ods listing gpath='C:\Users\Administrator\Desktop\test20180305';
  13. /*打开ods listing这一destination,指定图片保存路径,★★★★★需提前建立*/
  14. /*ods graphics on / imagename="picture" outputfmt=pdf;*/
  15. /*图片名称picture,输出类型pdf*/
  16. ods graphics off;

  17. /*Two Class Data Panel*/
  18. proc template;
  19. define statgraph Fig_5_3_1;
  20. begingraph;
  21. entrytitle 'Mileage by Horsepower by Type and Origin';
  22. layout datapanel classvars=(origin type) / columns=3
  23.       rowaxisopts=(griddisplay=on)
  24.       columnaxisopts=(griddisplay=on);
  25.    layout prototype ;
  26.       scatterplot x=horsepower y=mpg_city / datatransparency=0.5
  27.          markerattrs=(symbol=circlefilled);
  28.    endlayout;
  29. endlayout;
  30. endgraph;
  31. end;
  32. run;
  33. proc sgrender template=Fig_5_3_1
  34. data=sashelp.cars(where=(type in ('Sedan', 'Sports')));
  35. run;

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


62
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-13 11:01:51
Data Panel with One Class Variable
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Data Panel with One Class Variable (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. ods _all_ close;     /*关闭所有ods destinations,避免不必要的输出,节省计算机资源*/
  11. /*ods listing gpath='C:\Users\Administrator\Desktop\test20180305' image_dpi=300;*/
  12. ods listing gpath='C:\Users\Administrator\Desktop\test20180305';
  13. /*打开ods listing这一destination,指定图片保存路径,★★★★★需提前建立*/
  14. /*ods graphics on / imagename="picture" outputfmt=pdf;*/
  15. /*图片名称picture,输出类型pdf*/
  16. ods graphics off;

  17. /*Data Panel with One Class Variable*/
  18. proc template;
  19. define statgraph Fig_5_3_2;
  20. begingraph;
  21. entrytitle 'Mileage by Type and Origin';
  22. layout datapanel classvars=(origin) / headerlabeldisplay=value
  23.       rowaxisopts=(griddisplay=on offsetmin=0) /*条形图从x轴开始*/
  24.       columnaxisopts=(display=(ticks tickvalues)
  25.       tickvalueattrs=(size=7));
  26.    layout prototype;
  27.       barchart x=type y=mpg_city / stat=mean dataskin=gloss
  28.          datatransparency=0.3 barlabel=true;
  29.    endlayout;
  30. endlayout;
  31. endgraph;
  32. end;
  33. run;
  34. proc sgrender template=Fig_5_3_2
  35. data=sashelp.cars(where=(type not in ('Hybrid', 'Wagon')));
  36. format mpg_city 4.1;
  37. run;

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


63
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-13 11:08:01
Data Panel with One Class Variable and Number of Columns
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Data Panel with One Class Variable and Number of Columns
  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 Panel with One Class Variable and Number of Columns*/
  19. proc template;
  20. define statgraph Fig_5_3_3;
  21. begingraph;
  22. entrytitle 'Mileage by Type and Origin';
  23. layout datapanel classvars=(origin) / headerlabeldisplay=value
  24.       rowaxisopts=(griddisplay=on offsetmin=0) columns=3
  25.       columnaxisopts=(display=(ticks tickvalues)
  26.       tickvalueattrs=(size=6));
  27.    layout prototype;
  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_3_3
  36. data=sashelp.cars(where=(type not in ('Hybrid', 'Wagon')));
  37. format mpg_city 4.1;
  38. run;

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


64
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-13 11:18:02
Data Panel with One Class Variable, Columns, and Union
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Data Panel with One Class Variable, Columns, and Union
  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 Panel with One Class Variable, Columns, and Union*/
  19. proc template;
  20. define statgraph Fig_5_3_4;
  21. begingraph;
  22. entrytitle 'Mileage by Type and Origin';
  23. layout datapanel classvars=(origin) / headerlabeldisplay=value
  24.       rowaxisopts=(griddisplay=on offsetmin=0) columns=3
  25.       columndatarange=union columnaxisopts=(display=(ticks/*columndatarange=*/
  26.       tickvalues) tickvalueattrs=(size=6));
  27.    layout prototype;
  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_3_4
  36. data=sashelp.cars(where=(type not in ('Hybrid', 'Wagon')));
  37. format mpg_city 4.1;
  38. run;

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


65
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-13 11:19:28
Data Panel with One Class Variable and Proportional Columns
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Data Panel with One Class Variable and Proportional Columns
  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 Panel with One Class Variable and Proportional Columns*/
  19. proc template; /*--SAS 9.4--*/
  20. define statgraph Fig_5_3_5;
  21. begingraph;
  22. entrytitle 'Mileage by Type and Origin';
  23. layout datapanel classvars=(origin) / headerlabeldisplay=value
  24.       rowaxisopts=(griddisplay=on offsetmin=0) columns=3
  25.       columnweight=proportional columnaxisopts=/*columnweight=是关键!*/
  26.       (display=(ticks tickvalues) tickvalueattrs=(size=7));
  27.    layout prototype;
  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_3_5
  36. data=sashelp.cars(where=(type not in ('Hybrid', 'Wagon')));
  37. format mpg_city 4.1;
  38. run;

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


66
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-13 11:24:24
Data Panel with Side Bar
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Data Panel 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 Panel with Side Bar*/
  19. proc template;
  20. define statgraph Fig_5_3_6;
  21. begingraph;
  22. entrytitle 'Mileage by Type and Origin';
  23. layout datapanel classvars=(origin) / headerlabeldisplay=value
  24.       rowaxisopts=(label='Mileage' griddisplay=on offsetmin=0)
  25.       columnaxisopts=(display=(ticks tickvalues)) columns=2;
  26.    layout prototype / cycleattrs=true;
  27.       barchart x=type y=mpg_city / stat=mean dataskin=gloss
  28.          datatransparency=0.3 name='c' legendlabel='City';
  29.       barchart x=type y=mpg_highway / stat=mean dataskin=gloss
  30.          barwidth=0.5 datatransparency=0.3 name='h'
  31.          legendlabel='Highway';
  32.    endlayout;
  33.    sidebar / spacefill=false;
  34.       discretelegend 'c' 'h';
  35.    endsidebar;
  36. endlayout;
  37. endgraph;
  38. end;
  39. run;
  40. proc sgrender template=Fig_5_3_6
  41. data=sashelp.cars(where=(type not in ('Hybrid', 'Truck', 'Wagon')));
  42. run;

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


67
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-13 11:29:14
Data Panel with Multi-Page Output1 Data Panel with Multi-Page Output2 Data Panel with Multi-Page Output3
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Data Panel with Multi-Page Output
  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 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 Panel with Multi-Page Output*/
  31. proc template;
  32. define statgraph Fig_5_3_7;
  33. begingraph;
  34. entrytitle 'Lab Value by Day';
  35. layout datapanel classvars=(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=2 columns=1 rowgutter=5;
  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_3_7 data=GTL_GS_Labs2;
  50. run;

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


68
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-13 11:33:09
Row Lattice with Independent Axes
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Row Lattice with Independent Axes
  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 Lattice with Independent Axes*/
  19. proc template;
  20. define statgraph Fig_5_4_1;
  21. begingraph;
  22. entrytitle 'Mileage by Origin and Type';
  23. layout lattice;
  24.    layout overlay / yaxisopts=(griddisplay=on);/*图1*/
  25.       barchart x=origin y=mpg_city / stat=mean dataskin=gloss
  26.       group=drivetrain groupdisplay=cluster;
  27.    endlayout;
  28.    layout overlay / yaxisopts=(griddisplay=on);/*图2*/
  29.       boxplot x=type y=mpg_city / fillattrs=graphdata2;
  30.    endlayout;
  31. endlayout;
  32. endgraph;
  33. end;
  34. run;
  35. proc sgrender template=Fig_5_4_1
  36. data=sashelp.cars(where=(type ne 'Hybrid'));
  37. run;

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


69
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-13 11:38:41
Row Lattice with Common Axis
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Row Lattice with Common Axis
  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 Lattice with Common Axis*/
  19. proc template;
  20. define statgraph Fig_5_4_2;
  21. begingraph;
  22. entrytitle 'Mileage and Horsepower by Type';
  23. layout lattice / columndatarange=union;/*各管各的*/
  24.    columnaxes;/*设置公共的列坐标*/
  25.       columnaxis / display=(ticks tickvalues);
  26.    endcolumnaxes;
  27.    layout overlay / yaxisopts=(griddisplay=on);/*图1*/
  28.       barchart x=type y=mpg_city / stat=mean dataskin=gloss
  29.          group=drivetrain groupdisplay=cluster;
  30.    endlayout;
  31.    layout overlay / yaxisopts=(griddisplay=on);/*图2*/
  32.       boxplot x=type y=horsepower / fillattrs=graphdata2;
  33.    endlayout;
  34. endlayout;
  35. endgraph;
  36. end;
  37. run;
  38. proc sgrender template=Fig_5_4_2
  39. data=sashelp.cars(where=(type ne 'Hybrid'));
  40. run;

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


70
l1i2n3i4n5g(未真实交易用户) 在职认证  发表于 2018-3-13 14:15:33
Row Lattice with Common Axis and Unequal Weights
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Row Lattice with Common Axis and Unequal Weights
  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 Lattice with Common Axis and Unequal Weights*/
  19. proc template;
  20. define statgraph Fig_5_4_3;
  21. begingraph;
  22. entrytitle 'Distribution of Mileage';
  23. layout lattice / columndatarange=union rowweights=(0.8 0.2);/*单元比例*/
  24.    columnaxes;
  25.       columnaxis / display=(ticks tickvalues);
  26.    endcolumnaxes;
  27.    layout overlay / yaxisopts=(griddisplay=on);
  28.       histogram mpg_city / binaxis=false;
  29.       densityplot mpg_city / lineattrs=graphfit name='n'
  30.          legendlabel='Normal';
  31.       densityplot mpg_city / kernel() lineattrs=graphfit2 name='k'
  32.          legendlabel='Kernel';
  33.       discretelegend 'n' 'k' / location=inside across=1
  34.          halign=right valign=top ;
  35.    endlayout;
  36.    layout overlay / yaxisopts=(griddisplay=on);
  37.       boxplot y=mpg_city / orient=horizontal;
  38.    endlayout;
  39. endlayout;
  40. endgraph;
  41. end;
  42. run;
  43. proc sgrender template=Fig_5_4_3
  44. data=sashelp.cars(where=(type ne 'Hybrid'));
  45. run;

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


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

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