楼主: l1i2n3i4n5g
31807 123

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

11
l1i2n3i4n5g 在职认证  发表于 2018-1-29 16:48:27 |只看作者 |坛友微信交流群
Horizontal Bar Chart in a ROWLATTICE Layout
  1. /*建立数据集cancer,用于后续画图*/
  2. data cancer;
  3. input cause : &$20.  /*死亡原因,各种癌症等*/
  4.       cases       /*各种原因的死亡数*/
  5.       sex : &$20.;
  6. cards4;
  7. Lung Cancer  160000  female
  8. Colorectal Cancer  50000  male
  9. Breast Cancer  45000  female
  10. Pancreatic Cancer  40000  male
  11. Prostate Cancer  35000  female
  12. Leukemia  30000  male
  13. Lymphoma  28000  female
  14. Liver Cancer  26000  male
  15. Ovarian Cancer  24000  female
  16. Esophageal Cancer  22000  male
  17. Bladder Cancer  20000  female
  18. Kidney Cancer  18000  male
  19. ;;;;     /*注意“cause : &$20.”“cards4”“;;;;”用法!*/
  20. run;

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

  27. /*Horizontal Bar Chart in a ROWLATTICE Layout*/
  28. proc sgpanel data=cancer;
  29. panelby sex / layout=rowlattice onepanel
  30. novarname uniscale=column;
  31. hbar cause / response=cases stat=sum;
  32. rowaxis discreteorder=data
  33. display=(nolabel);
  34. colaxis display=(nolabel);
  35. run;

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

SGPanel1.png (17.85 KB)

SGPanel1.png

使用道具

12
l1i2n3i4n5g 在职认证  发表于 2018-2-1 15:14:41 |只看作者 |坛友微信交流群
Paneled Graph of Independent Scatter Plots
  1. data heart;
  2. input weight cholesterol systolic diastolic ageatdeath smoking;
  3. cards;
  4. 50 100 120 80 70 1
  5. 60 120 130 70 60 0
  6. 70 140 135 90 55 1
  7. 80 180 125 100 50 1
  8. ;
  9. run;

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

  16. /*Paneled Graph of Independent Scatter Plots*/
  17. proc sgscatter data=heart;
  18. plot (weight cholesterol systolic
  19. diastolic) * (ageatdeath) /
  20. group=smoking columns=2
  21. markerattrs=(size=10 symbol=circle)
  22. legend=(notitle);
  23. run;

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

Scatter Plot Matrix
  1. ods _all_ close;     /*关闭所有ods destinations,避免不必要的输出,节省计算机资源*/
  2. ods listing gpath='C:\Users\Administrator\Desktop\test20180124';
  3. /*打开ods listing这一destination,指定图片保存路径,★★★★★需提前建立*/
  4. /*ods graphics on / imagename="picture" outputfmt=pdf noborder;*/
  5. ods graphics off;
  6. /*图片名称picture,输出类型pdf,图片不需要边框*/

  7. /*Scatter Plot Matrix*/
  8. proc sgscatter data= heart;
  9. matrix weight systolic diastolic cholesterol/
  10. ellipse=(alpha=0.05 type=predicted)
  11. diagonal=(histogram normal);
  12. run;

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



使用道具

13
line_us 发表于 2018-2-1 15:56:01 |只看作者 |坛友微信交流群
支持分享

使用道具

14
l1i2n3i4n5g 在职认证  发表于 2018-2-1 16:25:43 |只看作者 |坛友微信交流群
Laboratory Panel of WBC Results
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: Laboratory Panel of WBC Results (Clinical Trial Reporting)
  3.    Software: SAS 9.4 TS Level 1M2
  4.    Name: justsoso
  5.    Date: 20180131
  6.    From: Clinical Trial Reporting Using SAS/GRAPH® SG Procedures
  7.          http://support.sas.com/resources/papers/proceedings09/174-2009.pdf
  8.    History: 20180131;
  9. %mend;

  10. /*建立数据集labs,用于后续画图*/
  11. data labs;
  12. /*label response = 'Mean Clinical Response'    label会体现在图的x、y轴label
  13.       visit    = 'Visit';*/
  14. input line :$20.    /*临床上关注的某个定量指标,即testname*/
  15.       numlow
  16.       numhi
  17.       result
  18.       visitnum :$20.       /*访视次数,第0天、第1天……*/
  19.       patid
  20.       panel :$20.;
  21. cards;
  22. Neutrophils(%) 40.5 75.0 48.60 PreRx 102 Hematology
  23. Neutrophils(%) 40.5 75.0 48.60 week1 102 Hematology
  24. Neutrophils(%) 40.5 75.0 48.60 week2 102 Hematology
  25. Neutrophils(%) 40.5 75.0 48.60 week3 102 Hematology
  26. Neutrophils(%) 40.5 75.0 48.60 week4 102 Hematology
  27. Neutrophils(%) 40.5 75.0 48.60 week5 102 Hematology
  28. Neutrophils(%) 40.5 75.0 48.60 week6 102 Hematology
  29. Lymphocytes(%) 15.4 48.5 41.60 PreRx 102 Hematology
  30. Basophils(%) 40.5 75.0 48.60 PreRx 102 Hematology
  31. Eosinophils(%) 15.4 48.5 41.60 PreRx 102 Hematology
  32. Monocytes(%) 15.4 48.5 41.60 PreRx 102 Hematology
  33. WBC 15.4 48.5 41.60 PreRx 102 Hematology
  34. ;
  35. run;

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

  41. /*Laboratory Panel of WBC Results*/
  42. ods listing style=statistical;
  43. proc sgpanel data=labs;
  44.    panelby line /
  45.    /*如果要指定顺序,就赋line变量为1、2、3再label*/
  46.    onepanel uniscale=column
  47.    novarname layout=rowlattice;
  48.    refline numlow / label noclip;/*参考线label注释*/
  49.    /*如果参考线在数据范围之外,也要显示*/
  50.    refline numhi / label noclip;
  51.    scatter x=visitnum y=result /
  52.    markerattrs=(symbol=asterisk size=10);
  53.    rowaxis display=(nolabel novalues noticks);
  54.    /*rowaxis不要坐标注释、不要坐标值、不要坐标点*/
  55.    colaxis offsetmax=0.1 display=(nolabel);
  56. run;

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


使用道具

15
l1i2n3i4n5g 在职认证  发表于 2018-2-2 14:29:01 |只看作者 |坛友微信交流群
Panel of LFT Results
  1. /*建立数据集labs,用于后续画图*/
  2. data labs;
  3. /*label response = 'Mean Clinical Response'    label会体现在图的x、y轴label
  4.       visit    = 'Visit';*/
  5. input visitnum     /*访视次数,第0天、第1天……*/
  6.       labtest :$20.     /*临床上关注的某个定量指标*/
  7.       drug :$20.
  8.       pre
  9.       result;
  10. cards;
  11. 1 alat drug_a 0.27216 0.64893
  12. 1 bilirubin_total drug_a 1.28536 0.54600
  13. 1 alk drug_a 0.27216 0.64893
  14. 1 asat drug_a 1.28536 0.54600
  15. 2 alat drug_a 0.27216 0.64893
  16. 2 bilirubin_total drug_a 1.28536 0.54600
  17. 2 alk drug_a 0.27216 0.64893
  18. 2 asat drug_a 1.28536 0.54600
  19. 3 alat drug_a 0.27216 0.64893
  20. 3 bilirubin_total drug_a 1.28536 0.54600
  21. 3 alk drug_a 0.27216 0.64893
  22. 3 asat drug_a 1.28536 0.54600
  23. ;
  24. run;

  25. /*自定义格式,用于刻度值的自定义显示*/
  26. proc format;
  27.    value visitnumfmt
  28.       1="1 week"
  29.       2="3 months"
  30.       3="6 months";
  31. run;

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

  37. /*Panel of LFT Results*/
  38. ods listing style=statistical;
  39. proc sgpanel data=labs;
  40.    format visitnum visitnumfmt.;
  41.    panelby labtest visitnum /
  42.    onepanel novarname layout=lattice;
  43.    scatter x=pre y=result/ group=drug;
  44.    refline 1 1.5 2 / axis=Y
  45.    lineattrs=(pattern=dash);
  46.    refline 1 1.5 2 / axis=X
  47.    lineattrs=(pattern=dash);
  48.    rowaxis integer min=0 max=4
  49.    label='Study (/ULN)';
  50.    colaxis integer min=0 max=4
  51.    label='Baseline (/ULN) *';
  52.    keylegend / title=" " noborder;/*不显示图例的题目,图例无边框*/
  53. run;

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


使用道具

16
l1i2n3i4n5g 在职认证  发表于 2018-2-2 15:19:58 |只看作者 |坛友微信交流群
Panel of LFT Results for At-Risk Patients
  1. /*建立数据集labs,用于后续画图*/
  2. data labs;
  3. /*label response = 'Mean Clinical Response'    label会体现在图的x、y轴label
  4.       visit    = 'Visit';*/
  5. input Patient
  6.       alat
  7.       biltot
  8.       alkph
  9.       asat
  10.       relday  /*days*/
  11.       miny    /*dval*/
  12.       sday;   /*sdays*/
  13. cards;
  14. 1 0.50000 0.40000 0.40000 0.50000 -25 . .
  15. 1 1.36533 0.64286 0.68972 1.08498 0 -0.5 0
  16. 1 2.18967 0.87118 0.97332 1.64750 25 -0.5 25
  17. 2 0.50000 0.40000 0.40000 0.50000 -25 . .
  18. 2 1.36533 0.64286 0.68972 1.08498 0 -0.5 0
  19. 2 2.18967 0.87118 0.97332 1.64750 25 -0.5 25
  20. 3 0.50000 0.40000 0.40000 0.50000 -25 . .
  21. 3 1.36533 0.64286 0.68972 1.08498 0 -0.5 0
  22. 3 2.18967 0.87118 0.97332 1.64750 25 -0.5 25
  23. 4 0.50000 0.40000 0.40000 0.50000 -25 . .
  24. 4 1.36533 0.64286 0.68972 1.08498 0 -0.5 0
  25. 4 2.18967 0.87118 0.97332 1.64750 25 -0.5 25
  26. ;
  27. run;

  28. /*自定义格式,用于刻度值的自定义显示*/
  29. proc format;
  30.    value Patientfmt
  31.       1="Patient 5152: White Male Age 48; Drug: A"
  32.       2="Patient 6416: White Male Age 64; Drug: A"
  33.       3="Patient 6950: White Male Age 51; Drug: A"
  34.       4="Patient 6969: White Male Age 48; Drug: B";
  35. run;

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

  41. /*Panel of LFT Results for At-Risk Patients*/
  42. ods listing style=statistical;
  43. proc sgpanel data=labs cycleattrs;
  44.    format Patient Patientfmt.;
  45.    panelby patient / novarname;
  46.    series x=relday y=alat / markers
  47.    lineattrs=(thickness=2px
  48.    pattern=solid);
  49.    series x=relday y=asat / markers
  50.    lineattrs=(thickness=2px
  51.    pattern=solid);
  52.    series x=relday y=alkph / markers
  53.    lineattrs=(thickness=2px pattern=solid);
  54.    series x=relday y=biltot / markers
  55.    lineattrs=(thickness=2px pattern=solid);
  56.    band x=sday lower=miny upper=4.5 /
  57.    transparency=0.8
  58.    legendlabel='Trial Duration';
  59.    refline 1 1.5 2 / axis=Y
  60.    lineattrs=(pattern=dash);
  61.    colaxis min=-50 max= 200;
  62.    rowaxis label="Upper Limit Normal";
  63. run;

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


使用道具

17
l1i2n3i4n5g 在职认证  发表于 2018-2-2 16:18:34 |只看作者 |坛友微信交流群
Two Aggregating Formats Used in a LATTICE Layout
  1. proc format;
  2. value smokfmt
  3. 1-999 = "Smoker"
  4. other = "Non-Smoker";
  5. value diasfmt
  6. 0-90 = "Diastolic < 90 mmHg"
  7. 91-95 = "Diastolic 90 - 95 mmHg"
  8. 96-999 = "Diastolic > 95 mmHg";
  9. run;

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

  15. /*Two Aggregating Formats Used in a LATTICE Layout*/
  16. ods listing style=statistical;
  17. proc sgpanel data=sashelp.heart;
  18.    format smoking smokfmt.
  19.    diastolic diasfmt.;
  20.    panelby smoking diastolic /
  21.    novarname missing onepanel
  22.    layout=lattice;
  23.    histogram weight;
  24.    density weight;
  25.    rowaxis integer grid;
  26.    colaxis grid label='Weight (in pounds)';
  27. run;

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


使用道具

18
l1i2n3i4n5g 在职认证  发表于 2018-2-2 17:06:30 |只看作者 |坛友微信交流群
Stacked Group Bar Chart
  1. %macro comment();    /*这是一种比较特殊的注释方式*/
  2.    Picture: GROUPED BAR CHARTS (Clinical Trial Reporting)
  3.    Software: SAS 9.4 TS Level 1M2
  4.    Name: justsoso
  5.    Date: 20180201
  6.    From: Clinical Trial Reporting Using SAS/GRAPH? SG Procedures
  7.          http://support.sas.com/resources/papers/proceedings09/174-2009.pdf
  8.    History: 20180201;
  9. %mend;

  10. /*建立数据集freq,用于后续画图*/
  11. data freq;
  12. /*label response = 'Mean Clinical Response'    label会体现在图的x、y轴label
  13.       visit    = 'Visit';*/
  14. input trt
  15.       pain :$10.
  16.       percent percent.;
  17. cards;
  18. 1 0 12%
  19. 1 1-2 14%
  20. 1 3-4 16%
  21. 2 0 12%
  22. 2 1-2 14%
  23. 2 3-4 16%
  24. ;
  25. run;

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

  31. /*Stacked Group Bar Chart*/
  32. ods listing style=analysis;
  33. proc sgplot data=freq;
  34.    vbar trt / response=percent group=pain;
  35.    xaxis display=(nolabel);
  36.    yaxis
  37.    label='Cumulative Percent of
  38.    Patients';
  39.    keylegend /position=bottom
  40.    title="Pain Score";
  41. run;

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


使用道具

19
l1i2n3i4n5g 在职认证  发表于 2018-2-5 15:07:18 |只看作者 |坛友微信交流群
Adjacent Group Bar Chart
  1. ods _all_ close;     /*关闭所有ods destinations,避免不必要的输出,节省计算机资源*/
  2. ods listing gpath='C:\Users\Administrator\Desktop\test20180201';
  3. /*打开ods listing这一destination,指定图片保存路径,★★★★★需提前建立*/
  4. /*ods graphics on / imagename="picture" outputfmt=pdf;     图片名称picture,输出类型pdf*/
  5. ods graphics off;

  6. /*Adjacent Group Bar Chart*/
  7. ods listing style=analysis;
  8. footnote bold 'Pain Score';
  9. proc sgpanel data=freq;
  10.    panelby pain / layout=columnlattice /*layout根据不同的列值*/
  11.    onepanel noborder novarname  /*cell不加边框*/
  12.    colheaderpos=bottom; /*列头位置在最下面*/
  13.    vbar trt / response=percent group=trt; /*画垂直条图*/
  14.    rowaxis grid offsetmax=0.05;
  15.    colaxis display=none;
  16.    keylegend /position=right across=1;
  17. run;

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


使用道具

20
l1i2n3i4n5g 在职认证  发表于 2018-2-5 15:16:30 |只看作者 |坛友微信交流群
LATTICED GROUP BAR CHART
  1. ods _all_ close;     /*关闭所有ods destinations,避免不必要的输出,节省计算机资源*/
  2. ods listing gpath='C:\Users\Administrator\Desktop\test20180201';
  3. /*打开ods listing这一destination,指定图片保存路径,★★★★★需提前建立*/
  4. /*ods graphics on / imagename="picture" outputfmt=pdf;     图片名称picture,输出类型pdf*/
  5. ods graphics off;

  6. /*LATTICED GROUP BAR CHART*/
  7. ods listing style=analysis;
  8. proc sgpanel data=freq noautolegend;
  9. panelby trt / layout=columnlattice
  10. novarname;
  11. vbar pain / response=percent group=trt;
  12. rowaxis grid;
  13. run;

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


使用道具

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

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

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

GMT+8, 2024-4-26 17:00