SAS编程实践---宏AESOCPT:按系统术语和首选术语计算受试者发生不良反应(AE)例数和例次

写在前面。

新手小白,疏漏在所难免,如果您看完有所指正,那我会很感谢您。如果您看完有所收获,那是我的荣幸。

SAS编程实践---宏:按系统术语和首选术语分层次计算受试者发生不良反应(AE)例数和例次

其实是对上述文章的完善,上面文章没有最后的代码汇总也没解释参数意义

有读者要应用的话需要一块一块复制代码,很费劲,还可能复制漏了,实在是不方便。

因此本文主要内容是上篇文章的示例数据生成代码宏代码汇总版本


示例数据生成代码

proc delete data = work._ALL_;
run;

%let seed1 = 666666;

data adae;
    do ii = 1 to 3;
        armn = ii;
        arm = cats("第",put(ii, best.) ,"组");

        do jj = 1 to 100;
            usubjid =  cats( "X",put(ii, best.) ,"-", put(jj, z3.));
            soc = cats(  "SOC",put(ranbin( &seed1., 5, 0.2) + 1, best.) );
            pt = cats(  "PT", compress(soc, , "kd")  , put(ranbin( &seed1., 10, 0.1) + 1, best.) );
            AESEVN =  ranbin( &seed1., 4, 0.5) + 1;
            AESEV = cats(AESEVN, "级");
            output;
        end;
    end;
run;

data adsl;
    do ii = 1 to 3;
        armn = ii;
        arm = cats("第",put(ii, best.) ,"组");

        do jj = 1 to 100;
            usubjid =  cats( "X",put(ii, best.) ,"-", put(jj, z3.));
            output;
        end;
    end;
run;

宏代码汇总版本

%macro AESOCPT(
            libin= , 
            dtin =   ,
            adsl =   , 
            usubjid =   ,
            l1var = ,
            l2var = ,
            grpvarn = , 
            rowsumyn = , 
            colsumyn = ,
            libout = ,
            dtout = );
/*******************************************************************************************
Purpose:按系统术语、首选术语和严重程度分层次计算受试者发生不良反应(AE)例数和例次
libin:输入数据集所在逻辑库,如work;
dtin:用于分析的输入数据集,可以跟筛选条件,也可以提前处理好,如adlb(where = FASFL = "是");
adsl:adsl数据集,用于确定受试者数量;
usubjid:受试者编号,用于确定受试者数量;
l1var:层级1的变量,如器官系统术语,AEBODSYS;
l2var:层级2的变量,如首选术语,AEDECOD;
grpvarn:受试者实验分组变量,数值型;
rowsumyn:指定是否计算合计列,取值限定为Y或者N;
colsumyn:指定是否计算首行合计,取值限定为Y或者N;
libout:输出数据集所在逻辑库,如work;
dtout:输出数据集名称,如Table1;
**************************************************************************************************/
    *_1. pre-processing;
    *_1.1 macro variables;
    *subjid number;
    proc sql noprint;
        select count(distinct  &grpvarn.) ,  count(distinct  &usubjid.)  into: grpnum, : SUBN999 from  &adsl.;
    quit;

    %put 受试者数量:&SUBN999.   分组数量:&grpnum.;

    %do xx = 1 %to &grpnum.;

        proc sql  noprint;
            select  count(distinct  &usubjid.)  into:SUBN&xx.  from  &adsl. where &grpvarn. = &xx.;
        quit;

        %put &grpvarn. = &xx.组的受试者数量: &&SUBN&xx.;
    %end;

    *_1.2 input datasets processing;
    data stdt0;
        set &libin..&dtin.;
    run;

    proc sort data=stdt0 out=&l1var._ nodupkey;
        by &l1var.;
    run;

    data &l1var.n;
        set &l1var._;
        &l1var.n = _N_;

    proc sort;
        by &l1var.;
    run;

    proc sort data=stdt0;
        by &l1var.;
    run;

    *_1.2.1 for times of case;
    data times1;
        merge stdt0
            &l1var.n;
        by &l1var.;
    run;

    data times2;
        set  times1;
        &l1var. = "合计";
        &l1var.n = 0;
        &l2var. = "合计";
    run;

    *_1.2.2 for number of case;
    data case1;
        merge stdt0
            &l1var.n;
        by &l1var.;
    run;

    proc sort data=case1 out=cs1nodup nodup dupout=cs1dup;
        by  &usubjid. &l1var.n  &l1var.  &l2var.;
    run;

    proc sort data=case1 out=cs2nodup nodup dupout=cs2dup;
        by  &usubjid. &l1var.n  &l1var.;
    run;

    data case2;
        set  cs2nodup;
        &l1var. = "合计";
        &l1var.n = 0;
        &l2var. = "合计";
    run;

    *_2.stat step;
    *_2.1  number of case;
    %do aa = 1 %to &grpnum.;

        proc sql noprint;
            create table ST_&aa. as

            select   &l1var.n, &l1var., "合计" as  &l2var., 
                cats(sum(&grpvarn. = &aa.), "(", put(sum(&grpvarn. = &aa.)/&&SUBN&aa.*100, 8.2), ")") as CASE_&aa.,
                sum(&grpvarn. > 0) + 0.2 as seq1
            from cs1nodup
                group by   &l1var.n, &l1var.

                    union 
                select &l1var.n, &l1var., &l2var., 
                    cats(sum(&grpvarn. = &aa.), "(", put(sum(&grpvarn. = &aa.)/&&SUBN&aa.*100, 8.2), ")") as CASE_&aa.,
                    sum(&grpvarn. > 0) +0.1  as seq1
                from cs1nodup
                    group by  &l1var.n, &l1var., &l2var.

                        union 
                    select   &l1var.n, &l1var., "合计" as &l2var., 
                        cats(sum(&grpvarn. = &aa.), "(", put(sum(&grpvarn. = &aa.)/&&SUBN&aa.*100, 8.2), ")") as CASE_&aa.,
                        sum(&grpvarn. > 0) + 1 as seq1
                    from case2
                        group by    &l1var.n, &l1var.
            ;
        quit;

        proc sort data=  ST_&aa.;
            by &l1var.n &l1var.  &l2var.;
        run;

    %end;

    *_2.2  times of case;
    %do aa = 1 %to &grpnum.;

        proc sql noprint;
            create table ST_&aa._ as

            select   &l1var.n, &l1var., "合计" as  &l2var., 
                cats(sum(&grpvarn. = &aa.)) as CASE_&aa._ ,
                sum(&grpvarn. > 0) + 0.2 as seq2
            from times1
                group by   &l1var.n, &l1var.

                    union 
                select &l1var.n, &l1var., &l2var., 
                    cats(sum(&grpvarn. = &aa.)) as CASE_&aa._ ,
                    sum(&grpvarn. > 0) +0.1  as seq2
                from times1
                    group by  &l1var.n, &l1var., &l2var.

                        union 
                    select   &l1var.n, &l1var., "合计" as &l2var., 
                        cats(sum(&grpvarn. = &aa.)) as CASE_&aa._ ,
                        sum(&grpvarn. > 0) + 1 as seq2
                    from times2
                        group by    &l1var.n, &l1var.
            ;
        quit;

        proc sort data=  ST_&aa._;
            by &l1var.n &l1var.  &l2var.;
        run;

    %end;

    *_2.3  caculation of each row;
    %if %sysfunc(upcase(&rowsumyn.) ) = %str(Y) %then
        %do;
            %put WARNING:      已经计算每行合计;

            *_2.3.1  caculation of each row for number of case;
            proc sql noprint;
                create table ST_99 as

                select   &l1var.n, &l1var., "合计" as  &l2var.,  1  as idid,
                    cats(sum(&grpvarn.  > 0), "(", put(sum(&grpvarn.  > 0)/&SUBN999.*100, 8.2), ")") as CASE_99,
                    sum(&grpvarn. > 0) + 0.2 as seq1
                from cs1nodup
                    group by   &l1var.n, &l1var.

                        union 
                    select   &l1var.n, &l1var.,   &l2var.,  2 as idid,
                        cats(sum(&grpvarn. > 0), "(", put(sum(&grpvarn. > 0)/&SUBN999.*100, 8.2), ")") as CASE_99,
                        sum(&grpvarn. > 0) + 0.1 as seq1
                    from  cs1nodup 
                        group by   &l1var.n, &l1var., &l2var.

                            union 
                        select   &l1var.n, &l1var., "合计" as &l2var.,   3 as idid,
                            cats(sum(&grpvarn. > 0), "(", put(sum(&grpvarn. > 0)/&SUBN999.*100, 8.2), ")") as CASE_99,
                            sum(&grpvarn. > 0) + 1 as seq1
                        from case2
                            group by    &l1var.n, &l1var.
                ;
            run;

            proc sort data=  ST_99;
                by &l1var.n  &l1var.  &l2var.;
            run;

            *_2.3.2  caculation of each row for times of case;
            proc sql noprint;
                create table ST_99_ as

                select   &l1var.n, &l1var., "合计" as  &l2var.,  1  as idid,
                    cats(sum(&grpvarn.  > 0) ) as CASE_99_,
                    sum(&grpvarn. > 0) + 0.2 as seq2
                from times1
                    group by   &l1var.n, &l1var.

                        union 
                    select   &l1var.n, &l1var.,   &l2var.,  2 as idid,
                        cats(sum(&grpvarn.  > 0) ) as CASE_99_,
                        sum(&grpvarn. > 0) + 0.1 as seq2
                    from  times1 
                        group by   &l1var.n, &l1var., &l2var.

                            union 
                        select   &l1var.n, &l1var., "合计" as &l2var.,   3 as idid,
                            cats(sum(&grpvarn.  > 0) ) as CASE_99_,
                            sum(&grpvarn. > 0) + 1 as seq2
                        from times2
                            group by    &l1var.n, &l1var.
                ;
            run;

            proc sort data=  ST_99_;
                by &l1var.n  &l1var.  &l2var.;
            run;

        %end;
    %else
        %do;
            %put WARNING:      不计算每行合计;
        %end;

    *_2.4  caculation of each column;
    data  _0&dtout.;
        merge  ST_:
        ;
        by &l1var.n  &l1var.  &l2var.;

        %if %sysfunc(upcase(&colsumyn.) ) = %str(Y) %then
            %do;
                %put  WARNING:      已经计算每列合计;
            %end;
        %else
            %do;
                %put  WARNING:      不计算每列合计;

                if &l1var.n = 0 then
                    delete;
            %end;

    proc sort;
        by &l1var.n descending seq1 descending seq2;
    run;

    run;

    *_3 processing step of stat;
    data _1&dtout.;
        set  _0&dtout.;
        by &l1var.n descending seq1 descending seq2;

        if first.&l1var.n or first.&l1var. then
            &l1var. = &l1var.;
        else  &l1var. = "    "||&l2var.;
        keep &l1var.  CASE_:;
    run;

    * _4.output steps;
    proc contents data=  _1&dtout.  out= _1outs noprint;

    proc sort;
        by varnum;
    run;

    proc sql noprint;
        select count(distinct NAME) , NAME into:varn,:col1-:col99 from _1outs;
    quit;

    data &libout..&dtout.;
        set  _1&dtout.;

        %do ii = 1 %to &varn.;
            if &&col&ii. = "0(0.00)" then
                &&col&ii. ="0";
            %let jj = %eval(&ii. - 1);
            rename &&col&ii. = C&jj.;
        %end;
    run;

    proc datasets lib=work noprint;
        delete    &l1var._   &l1var.n  times: case: cs: st:  _:;
    run;

%mend;

%AESOCPT(libin=work , 
    dtin = adae  ,
    adsl = adsl  , 
    usubjid = usubjid  ,
    l1var =soc ,
    l2var = pt,
    grpvarn = armn, 
    rowsumyn = Y, 
    colsumyn =Y ,
    libout =work ,
    dtout = table);

以上。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,639评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,277评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,221评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,474评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,570评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,816评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,957评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,718评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,176评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,511评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,646评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,322评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,934评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,755评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,987评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,358评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,514评论 2 348

推荐阅读更多精彩内容