楼主: l1i2n3i4n5g
31672 123

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

91
l1i2n3i4n5g 在职认证  发表于 2018-3-21 14:54:24 |只看作者 |坛友微信交流群
Draw Text
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Draw Text
  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. ods _all_ close;     /*关闭所有ods destinations,避免不必要的输出,节省计算机资源*/
  12. /*ods listing gpath='C:\Users\Administrator\Desktop\test20180308' image_dpi=300;*/
  13. ods listing gpath='C:\Users\Administrator\Desktop\test20180308';
  14. /*打开ods listing这一destination,指定图片保存路径,★★★★★需提前建立*/
  15. /*ods graphics on / imagename="picture" outputfmt=pdf;*/
  16. /*图片名称picture,输出类型pdf*/
  17. ods graphics off;

  18. /*Draw Text*/
  19. proc template;
  20. define statgraph Fig_7_4_1;
  21. begingraph;
  22. entrytitle "Regression Fit Plot";
  23. layout overlay / xaxisopts=( offsetmin=.1);
  24.    drawtext textattrs=(style=italic size=7pt)
  25.       "Band shows 99% Confidence Limit of Mean" /
  26.       anchor=bottomleft width=15 widthunit=percent/*文本框的宽度*/
  27.       xspace=wallpercent yspace=wallpercent
  28.       x=0 y=10 justify=center ;
  29.    modelband "myclm";
  30.    scatterplot x=height y=weight / primary=true;
  31.    regressionplot x=height y=weight / alpha=.01 clm="myclm" ;
  32.       drawtext textattrs=(color=gray size=40pt weight=bold)
  33.       "PRELIMINARY"/ transparency=.75 rotate=-30 width=200
  34.       widthunit=percent justify=center;
  35. endlayout;
  36. endgraph;
  37. end;
  38. run;
  39. proc sgrender data=sashelp.class template=Fig_7_4_1;
  40. run;

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


使用道具

92
l1i2n3i4n5g 在职认证  发表于 2018-3-21 15:01:21 |只看作者 |坛友微信交流群
Data Set Based Annotation
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Data Set Based Annotation
  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. ods _all_ close;     /*关闭所有ods destinations,避免不必要的输出,节省计算机资源*/
  12. /*ods listing gpath='C:\Users\Administrator\Desktop\test20180308' image_dpi=300;*/
  13. ods listing gpath='C:\Users\Administrator\Desktop\test20180308';
  14. /*打开ods listing这一destination,指定图片保存路径,★★★★★需提前建立*/
  15. /*ods graphics on / imagename="picture" outputfmt=pdf;*/
  16. /*图片名称picture,输出类型pdf*/
  17. ods graphics off;

  18. /*Data Set Based Annotation*/
  19. proc template; /*--SAS 9.4--*/
  20. define statgraph Fig_7_5_1;
  21. begingraph;
  22. entrytitle 'Vehicle Statistics';
  23. layout lattice / columns=2;
  24.    layout overlay;
  25.       barchart x=origin y=mpg_city / stat=mean group=type
  26.          groupdisplay=cluster dataskin=gloss;
  27.    endlayout;
  28.    layout overlay;
  29.       histogram mpg_city;
  30.       densityplot mpg_city;
  31.    endlayout;
  32.    annotate;
  33. endlayout;
  34. endgraph;
  35. end;
  36. run;
  37. data anno;
  38. retain function 'text' x1space 'graphpercent'
  39.    y1space 'graphpercent';
  40. retain textsize 40 textcolor 'black' textweight 'bold';
  41. width=1000; anchor='center'; x1=50; y1=50; rotate=30;
  42. transparency=0.5; label="Confidential"; output;
  43. run;
  44. proc sgrender template=Fig_7_5_1 sganno=anno
  45. data=sashelp.cars(where=(type in ('Sedan' 'Sports' 'SUV')));
  46. run;

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


使用道具

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

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

  17. /*Macro Variables*/
  18. proc template;
  19. define statgraph Fig_8_4_2;
  20. begingraph;
  21. mvar _x _y;
  22. nmvar _degree;
  23. entrytitle _y ' by ' _x;
  24. layout overlay / xaxisopts=(griddisplay=on)
  25.    yaxisopts=(griddisplay=on);
  26.    scatterplot x= _x y=_y;
  27.    regressionplot x= _x y=_y / degree=_degree;
  28. endlayout;
  29. endgraph;
  30. end;
  31. run;
  32. %let _x=Horsepower;
  33. %let _y=Mpg_City;
  34. %let _degree=2;
  35. proc sgrender template=Fig_8_4_2 data=sashelp.cars;
  36. run;

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


使用道具

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

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

  17. /*Dynamics*/
  18. proc template;
  19. define statgraph Fig_8_4;
  20. begingraph;
  21. dynamic _var;
  22. entrytitle 'Distribution of ' _var;
  23. layout overlay / xaxisopts=(display=(ticks tickvalues))
  24.    yaxisopts=(griddisplay=on);
  25.    histogram _var / binaxis=false;
  26.    densityplot _var / kernel();
  27. endlayout;
  28. endgraph;
  29. end;
  30. run;
  31. proc sgrender template=Fig_8_4 data=sashelp.cars;
  32. dynamic _var='Horsepower';
  33. run;

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


使用道具

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

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

  17. /*Conditionals*/
  18. proc template;
  19. define statgraph Fig_8_4_3;
  20. begingraph;
  21. dynamic _var _type;
  22. entrytitle 'Distribution of ' _var;
  23. layout overlay / xaxisopts=(display=(ticks tickvalues))
  24.       yaxisopts=(griddisplay=on);
  25.    histogram _var / binaxis=false;
  26.    if (_type = 'Kernel') densityplot _var / kernel();
  27.    else densityplot _var;
  28.    endif;
  29. endlayout;
  30. endgraph;
  31. end;
  32. run;
  33. proc sgrender template=Fig_8_4_3 data=sashelp.cars;
  34. dynamic _var='Horsepower' _type='Kernels';
  35. run;

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


使用道具

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

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

  17. /*Function Evaluation*/
  18. proc template; /*--SAS 9.4--*/
  19. define statgraph Fig_8_4_4;
  20. begingraph / datasymbols=(circlefilled trianglefilled squarefilled);
  21. entrytitle 'Mileage by Horsepower';
  22. entryfootnote halign=left 'Excluding Hybrids';
  23. layout overlay;
  24.    scatterplot x=horsepower y=mpg_city / group=origin
  25.       filledoutlinedmarkers=true;
  26.    regressionplot x=horsepower y=mpg_city / degree=2 name='a'
  27.       group=origin lineattrs=(thickness=3);
  28.    layout gridded / rows=2 order=columnmajor border=true
  29.       autoalign=(right);
  30.       entry halign=right 'N = ';
  31.       entry halign=right {unicode SIGMA} ' =' ;
  32.       entry halign=left eval(strip(put(n(mpg_city),4.0)));
  33.       entry halign=left eval(strip(put(stddev(mpg_city),4.1)));
  34.    endlayout;
  35. discretelegend 'a'/location=inside halign=right valign=top
  36. across=1;
  37. endlayout;
  38. endgraph;
  39. end; run;
  40. proc sort data=sashelp.cars(where=(type ne 'Hybrid')) out=cars;
  41. by origin; run;
  42. proc sgrender template=Fig_8_4_4 data=cars; run;

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


使用道具

97
ransuoqu 发表于 2018-8-6 13:53:59 |只看作者 |坛友微信交流群
楼主,可以设置图片分辨率吗,因为输出来的图片分辨率必须达到300dpi以上才行

使用道具

匿名网友
98
匿名网友  发表于 2018-8-6 21:57:12 |坛友微信交流群
ransuoqu 发表于 2018-8-6 13:53
楼主,可以设置图片分辨率吗,因为输出来的图片分辨率必须达到300dpi以上才行
改写原文中的这一句:
ods graphics on / imagename="picture" outputfmt=pdf;

ods graphics on / imagename="picture" outputfmt=pdf image_dpi=300;
即可

使用道具

匿名网友
99
匿名网友  发表于 2018-8-6 21:58:52 |坛友微信交流群
李宁老兄真够厉害的,花了真么多图。

总结的文字为也让我看看啊。

使用道具

100
njtyc 发表于 2019-3-15 03:48:37 |只看作者 |坛友微信交流群
谢谢提供好的学习内容。

使用道具

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

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

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

GMT+8, 2024-4-20 06:35