SAS编程-Table:Shift表的处理-数据库论坛-技术分享-千百度社区

SAS编程-Table:Shift表的处理

SAS编程-Table:Shift表的处理

有读者咨询Shift表的处理,这篇文章就分享下Shift表SAS编程处理。

文章所附代码是完整的处理代码,除了没有RTF输出。读者可以自行复制到SAS中进行调试、运行和理解。若有疑问或建议,欢迎评论区指出。

后续另写文章,介绍RTF的输出。

1. Shift Table的本质

关于Shit Table,它本质上就是一个频数统计表,所以可以直接按正常的频数统计表去处理

以下面2张Table为例,左侧为基线后TBS分类的Shift表,右侧为各类AE的频数统计表。左侧与右侧相比,直观上是多了2个“试验分组”并且,Shift表的分组变量为基线分类变量,普通频数统计表为试验分组变量

频数统计表

因此,我们把普通的频数统计表的试验分组变量,换成基线分类变量,是不是就完成了Shift的表的编程?

下面是具体的演示代码:

2. 建立演示数据集

我以SASHelp.Class数据集为基础,做一个演示数据集,便于实现“Shift”。数据集包含变量TRT01AN , ANRIND, BNRIND, AVSITN:

**Create a demo dataset;
data class;
  set sashelp.class( in = a )
    sashelp.class( in = b)
    sashelp.class( in = c)
    sashelp.class( in = d);

    *avisitn;
    if a then avisitn = 1;
    else if b then avisitn = 2;
    else if c then avisitn = 3;
    else if d then avisitn = 4;

    *trt01an;
    if sex = "M" then trt01an = 1;
    else if sex = "F" then trt01an = 2;

    *anrind;
    length anrind $10;
    if height < 57 then anrind = "Low";
    else if height >= 57 and height <= 66 then anrind = "Normal";
    else anrind = "High";

    *bnrind;
    length bnrind $10;
    if weight < 84 then bnrind = "Low";
    else if weight >= 84 and weight <= 112 then bnrind = "Normal";
    else bnrind = "High";

run;

Class

3. Shift Table的具体编程

Shift Table的示例代码参考:

SAS编程:通过Picture语句设置变量Format
SAS编程:频数汇总时,如何使分组类别按固定顺序展示?
SAS编程:生成Table时,汇总组(Total)组如何处理?

大家可以直接复制以下代码到SAS中运行,代码里有详细的注释。对于不理解的代码或选项,可以参考上面3篇示例文章。

若还有疑问,欢迎评论区留言。

***1.Craete Formats for preloadfmt options and Totle group;
proc format;
  *Format for FREQ;
   picture freq (round default=8 )
      0 <-<99.5 = "009.9)" (prefix="( " )
      0, 99.5-100 = "(100)  " (noedit) 
    ;

  value $anrind (notsorted multilabel)
    "Low" = "Low"
    "Normal" = "Normal"
    "High" = "High"
    "Low", "Normal", "High" = "Total"
  ;

  *Informat for anrind order;
  invalue anrindn
    "Low" = 1
    "Normal" = 2
    "High" = 3
    "Total" = 4
    ;
  

  *number for transpose ID var;
  value $bnrind (notsorted multilabel)
    "Low" = "1"
    "Normal" = "2"
    "High" = "3"
    "Low", "Normal", "High" = "4"
  ;

  value trt01an
    1 = 1
    2 = 2
  ;

  value trt01a
    1 = "Placebo"
    2 = "Treatment"
  ;

  value avisitn
    1 = 1
    2 = 2
    3 = 3
    4 = 4
  ;

  value avisit
    1 = "Screening"
    2 = "Month 1"
    3 = "Month 2"
    4 = "Month 3"
  ;
run;


***2. Get data for analysis;

**2.1 Get data for BigN;
data class_bign;
  set class;
  
  if avisitn = 1;

  *Flag for cont;
  flag = 1;

  proc sort nodupkey;
    by trt01an  name;
run;

**2.2 Get data for small n;
data class_n;
  set class;
  
  if avisitn > 1;

  *Flag for cont;
  flag = 1;

  proc sort;
    by trt01an avisitn;
run;


***3. Calculate statistics;

**3.1 Derive BigN and save them to macro vars;
proc means data = class_bign nway completetypes ;
        format trt01an trt01an.;
    class trt01an / preloadfmt order = data;
    var flag;

    output n = bign nmiss = nmiss out = BigN;
run;

data _null_;
    set BigN;
    call symput("N_"||strip(put(trt01an, best.)), strip(put(bign, best.)));
run;

*Check BigN;
proc sql noprint;
    create table BigNcheck as
        select *
        from dictionary.macros
        where name like "N_%";
quit;

**3.2 Derive statistic vars;
proc means data = class_n noprint nway completetypes;
    by trt01an avisitn;

    format anrind $anrind.;
    class anrind / preloadfmt mlf order = data;

    format bnrind $bnrind.;
    class bnrind / preloadfmt mlf order = data;

    var flag; 

    output n=count nmiss=nmiss out=count1 ;
run;

*Get Bign and compute freq;
data count;
    merge count1(in = a) bign;
    by trt01an;

    if a;

    length freq $200;
    if count = 0 then freq =strip(put(count, best.));
    else freq =strip(put(count, best.))||put(count/bign*100, freq.);

    *order var for anrind;
    catn = input(anrind, anrindn.);

    length trt01a avisit $20;
    trt01a = put(trt01an, trt01a.);
    avisit = put(avisitn, avisit.);

    proc sort;
        by  trt01an  avisitn catn anrind; 
run;

proc transpose data = count out = final(drop = _name_) prefix = trt_;
    by  trt01an trt01a avisitn avisit catn anrind;

    id bnrind;
    var freq;
run;

程序运行的结果如下:

Shift Table

总结

这篇文章介绍了,Shift表的完整处理过程,Shift表本质是一个频数统计表,只是分组变量不是常规的试验分组,而是基线分类变量

文章也展示我日常TFL编程的“4段论”:(RTF输出后续介绍)

***1. Craete Formats for output;
***2. Get data for analysis;
***3. Calculate statistics;
***4. Create RTF output;

Shift表的SAS编程,涉及到了分类变量汇总时固定位置的展示,利用multilabel选项生成汇总组,以及通过Picture语句设置变量的Format。当然,用其他方式实现对应功能也是可以的。

关于Shift表的BigN,我取的是每个试验组的总人数,正常的ADaM数据集在这一步是需要去重处理的。当然,具体的BigN还需要查看对应TFL Shell的要求。

Proc means中,分析的分组变量可以放到Class语句中,也可以放到By语句中。如果需要对分析的分组变量提前设置格式和排列顺序,就需要放在Class语句中。Completetypes选项会输出Class变量分类的所有排列组合,如果因此产生多余的分类,需将对应变量放到By语句中

在最后的输出数据集中,我将频数的列名前缀设置为trt_,是方便批量引用处理,trt_:。如果设置为trt, 批量引用trt:就会涉及到trt相关变量,造成处理错误。当然,其他唯一前缀也可以同样的功能。

感谢阅读, 欢迎关注:SAS茶谈!
若有疑问,欢迎评论交流!

最后编辑于 : 2022.10.08 11:03:15 © 著作权归作者所有,转载或内容合作请联系作者

请登录后发表评论

    没有回复内容