学习sas(如何生成三线表)

利用proc report生成表格

data class;
    set sashelp.class;
    length agegroup $200.;
    if age<=12 then agegroup = "≤12 岁";
    else if age>12 then agegroup=">12 岁";
    bmi=(weight*0.4536)/(height/100)**2;
    if sex="男" then sexn=1;
    else sexn=2;
    keep sex: agegroup bmi;
run;

data class1;
    set class;
    output;
    sexn=3;
/*    group=1;*/
    output;
run;

proc sort data = class1;
   by sexn;
run;

ods select none; 
ods trace on;
ods output OneWayFreqs=freq;
proc freq data=class1;
by sexn; /*不是交叉*/
table agegroup;
run;
ods output close;
ods trace off;
ods select all;

proc sort data=freq;
    by agegroup sexn;
run;

data freq11;
    set freq;
    var = cats(frequency,'(',put(percent,10.1),'%',')');  /*用put函数将数值型转化为字符型*/
    run;

proc transpose data = freq11 out = freq1 prefix = sex_;
      by agegroup;
      id sexn;
      var var;
run;


data part1;
    set freq1;
    if agegroup = "≤12 岁" then ord=1;
    else ord=2;
    group=1;
    output;
    if ord=1 then do;
    ord=0;
    agegroup="年龄";
    sex_1="";
    sex_2="";
    sex_3="";
    output;
    end;
run;

proc sort data=part1;
by group ord;
run;

ods select none; 
ods trace on;
proc means data=class1;
by sexn;
var bmi;
output out=means n=n mean=mean std=std median=median min=min max=max;
run;
ods trace off;
ods select all;

data means1;
    set means;
      length nc mean_stdc medianc  min_maxc $200;
        *此处仅根据目前数据简单处理;
        if nmiss(n)=1 then nc="0";
        else nc=cats(n);
        if nmiss(mean,std) ne 2 then mean_stdc=catx(" (",put(mean,10.2),strip(put(std,10.3))||")");
        medianc=strip(put(median,10.2));
        if nmiss(min,max) ne 2 then min_maxc=catx(" - ",put(min,10.1),put(max,10.1));
run;

proc transpose data=means1 out=means2 prefix=sex_;
    id sexn;
    var nc mean_stdc medianc min_maxc;
run;

proc format;
    value sta
    1="    例数"
    2="    均值(标准差)"
    3="    中位数"
    4="    最小值 - 最大值"
;
run;

data means3;
    set means2;
    if _NAME_="nc " then ord=1;
    else if _NAME_="mean_stdc" then ord=2;
    else if _NAME_="medianc" then ord=3;
    else if _NAME_="min_maxc" then ord=4;
    length col1-col4 $200.;
    agegroup=put(ord,sta.);
/*    col2=_1;*/
/*    col3=_2;*/
/*    col4=_3;*/
    group=2;
    keep group ord agegroup sex_:;
run;

data part2;
    set means3;
    output;
    if ord=1 then do;
    ord=0;
    agegroup="BMI (kg/m@{super 2})";
    sex_1="";
    sex_2="";
    sex_3="";
    output;
    end;
run;

proc sort data=part2;
    by group ord;
run;

data all;
    set part:;
    by group ord;
run;

*注意需要在report前引用或运行template程序;
%include "template.sas";
options papersize=letter ps=max ls=max nofmterr nonumber nodate formchar='|_---|+|---+=|-/\<>*' missing=' ';
ods escapechar="@";
ods rtf file="test-表格 14.1.3.1.1 人口统计学.rtf" style=rtftable; *bodytitle;
title j=c "表格 14.1.3.1.1 人口统计学";
footnote j=l "百分比的计算基于每组的人数";
footnote2 j=l "sashelp.class";
proc report data=all nowd missing split="^";
    column group ord agegroup ("性别@R/RTF'\fs0\brdrb\brdrs\brdrw20" sex_1 sex_2) sex_3;
    define group/order order=internal noprint;
    define ord/order order=internal noprint;
    define agegroup/display '' order=internal style={asis=on width=24% just=l};
    define sex_1/display '男性^ (N = 10)' style={asis=on width=25% just=c};
    define sex_2/display '女性^ (N = 9)' style={asis=on width=25% just=c};
    define sex_3/display '总计^ (N = 19)' style={asis=on width=25% just=c};
    compute after group;
        line '';
    endcomp;
run;
ods rtf close;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值