修改SAS表计数

时间:2019-07-02 16:31:09

标签: arrays loops sas

我正在尝试将Jack Shostak的书《制药行业的SAS编程》中的以下代码用于SAS中的药物表:

PROC SQL NOPRINT;
    SELECT COUNT(DISTINCT USUBJID) FORMAT = 3.
    INTO :n1
    FROM ADSL
    WHERE TRTPN = 1;
    SELECT COUNT(DISTINCT USUBJID) FORMAT = 3.
    INTO :n2
    FROM ADSL
    WHERE TRTPN = 0;
    SELECT COUNT(DISTINCT USUBJID) FORMAT = 3.
    INTO :n3
    FROM ADSL
    WHERE TRTPN NE .;
QUIT;


PROC SQL NOPRINT;
   CREATE TABLE CMTOSUM AS
   SELECT UNIQUE(C.CMDECOD) AS CMDECOD, C.USUBJID, T.TRTPN
   FROM CM AS C, ADSL AS T
   WHERE C.USUBJID = T.USUBJID
   ORDER BY USUBJID, CMDECOD;
QUIT;

ODS LISTING CLOSE;

ODS OUTPUT CROSSTABFREQS = COUNTS;
PROC FREQ DATA = CMTOSUM;
    TABLES TRTPN * CMDECOD;
RUN;

ODS OUTPUT CLOSE;
ODS LISTING;

PROC SORT DATA = COUNTS;
   BY CMDECOD;
RUN;
DATA CM;
   MERGE COUNTS(WHERE = (TRTPN = 1) RENAME = (FREQUENCY = COUNT1))
         COUNTS(WHERE = (TRTPN = 0) RENAME = (FREQUENCY = COUNT2))
         COUNTS(WHERE = (TRTPN = .) RENAME = (FREQUENCY = COUNT3))
         END = EOF;
         BY CMDECOD;

         KEEP CMDECOD ROWLABEL COL1-COL3 SECTION;
         LENGTH ROWLABEL $25 COL1-COL3 $10;

         IF CMDECOD = '' THEN
            DO;
               ROWLABEL = 'ANY MEDICATION';
                  SECTION = 1;
            END;
            ELSE
            DO;
               ROWLABEL = CMDECOD;
                   SECTION = 2;
            END;

            PCT1 = (COUNT1/ &n1) *100;
            PCT2 = (COUNT2/ &n2) *100;
            PCT3 = (COUNT3/ &n3) *100;

            COL1 = PUT(COUNT1, 3.) || " (" || PUT(PCT1, 3.) || "%)";
            COL2 = PUT(COUNT2, 3.) || " (" || PUT(PCT2, 3.) || "%)";
            COL3 = PUT(COUNT3, 3.) || " (" || PUT(PCT3, 3.) || "%)";

RUN;

此代码正确列出了特定药物在每个治疗组内的受试者人数。但是,当我运行此代码时,它会基于“ ANY MEDICATION”行中的药物数量而不是受试者总数来生成计数。目前,百分比超过100;我想修改计数,以使其一旦击中每个治疗组的受试者总数就停止计数。任何见识将不胜感激。

1 个答案:

答案 0 :(得分:0)

我能够通过添加以下代码行来解决此问题:

 IF COUNT1 GE &N1 THEN COUNT1 = &n1;
 IF COUNT2 GE &N2 THEN COUNT2 = &n2;
 IF COUNT3 GE &N3 THEN COUNT3 = &n3;

这将计数限制为每个组中的对象总数。 以下是更新的代码,以供参考。

PROC SQL NOPRINT;
    SELECT COUNT(DISTINCT USUBJID) FORMAT = 3.
    INTO :n1
    FROM ADSL
    WHERE TRTPN = 1;
    SELECT COUNT(DISTINCT USUBJID) FORMAT = 3.
    INTO :n2
    FROM ADSL
    WHERE TRTPN = 0;
    SELECT COUNT(DISTINCT USUBJID) FORMAT = 3.
    INTO :n3
    FROM ADSL
    WHERE TRTPN NE .;
QUIT;


PROC SQL NOPRINT;
   CREATE TABLE CMTOSUM AS
   SELECT UNIQUE(C.CMDECOD) AS CMDECOD, C.USUBJID, T.TRTPN
   FROM CM AS C, ADSL AS T
   WHERE C.USUBJID = T.USUBJID
   ORDER BY USUBJID, CMDECOD;
QUIT;

ODS LISTING CLOSE;

ODS OUTPUT CROSSTABFREQS = COUNTS;
PROC FREQ DATA = CMTOSUM;
    TABLES TRTPN * CMDECOD;
RUN;

ODS OUTPUT CLOSE;
ODS LISTING;

PROC SORT DATA = COUNTS;
   BY CMDECOD;
RUN;
DATA CM;
   MERGE COUNTS(WHERE = (TRTPN = 1) RENAME = (FREQUENCY = COUNT1))
         COUNTS(WHERE = (TRTPN = 0) RENAME = (FREQUENCY = COUNT2))
         COUNTS(WHERE = (TRTPN = .) RENAME = (FREQUENCY = COUNT3))
         END = EOF;
         BY CMDECOD;

         KEEP CMDECOD ROWLABEL COL1-COL3 SECTION;
         LENGTH ROWLABEL $25 COL1-COL3 $10;



         IF COUNT1 GE &N1 THEN COUNT1 = &n1;
         IF COUNT2 GE &N2 THEN COUNT2 = &n2;
         IF COUNT3 GE &N3 THEN COUNT3 = &n3;

         IF CMDECOD = '' THEN
            DO;
               ROWLABEL = 'ANY MEDICATION';
                  SECTION = 1;
            END;
            ELSE
            DO;
               ROWLABEL = CMDECOD;
                   SECTION = 2;
            END;

            PCT1 = (COUNT1/ &n1) *100;
            PCT2 = (COUNT2/ &n2) *100;
            PCT3 = (COUNT3/ &n3) *100;

            COL1 = PUT(COUNT1, 3.) || " (" || PUT(PCT1, 3.) || "%)";
            COL2 = PUT(COUNT2, 3.) || " (" || PUT(PCT2, 3.) || "%)";
            COL3 = PUT(COUNT3, 3.) || " (" || PUT(PCT3, 3.) || "%)";

RUN;