Macro %baseline

1. Description

The macro BASELINE will intake SAS dataset and outputs SAS dataset with baseline flag.

2. Dependencies

2.1 Global Macro Variables - N/A

2.2 Global Macros - N/A

3. Macro Parameters

Macro parameter Description Example
INDAT (R) Name of Input dataset. INDAT=EG , Default value: None
OUTDAT (R) Name of Output dataset. OUTDAT=EG1 , Default value: None
DATVAR (R) Name of the date variable in the input dataset. DATVAR=EGDTC ,Default value: None
BYVARS (R) By variables in the input dataset. BYVARS=USUBJID EGTESTCD ,Default value: None
LASTBY (R) The variable that is used for taking the ‘last.’ as the flag LASTBY=EGTESTCD , Default value: None
RESULT (R) Result variable in findings domain. RESULT=EGORRES , Default value: None
DNAM (R) Name of the domain. DNAM=EG , Default value: None
LIB (O) Library name which contains DM dataset LIB=DSDTM , Default value: SDTM
SPDTFL (O) Sponsor defined macro variable which contains the value 0 and 1 only.SPDTFL=1 means that the –DTC variables compared with date and time.SPDTFL=0 mean that the –DTC variables compared with dates only. SPDTFL=1 , Default value: 0

(R): Required (O): Optional

4. Assumptions

  1. The Demographics (DM) dataset must contain RFXSTDTC variable.

5. Sample Call

  %BASELINE(INDAT=VS,
                OUTDAT=VS4, 
            DATVAR=VSDTC, 
            BYVARS=USUBJID VSTESTCD,
            LASTBY=VSTESTCD, 
            RESULT=VSORRES, 
            DNAM=VS);

6. Macro Code

%macro baseline(indat=,outdat=,datvar=,byvars=,lastby=,result=,dnam=,lib=sdtm,spdt     fl=0);
       *Custom  message when date variable is missing in macro call;
       %if &datvar eq  %then %do;
         %put ERROR: Date variable is missing in macro call;
        %return;
       %end;
       %else %do;
         *Custom  message when Input dataset is missing in macro call;
         %if &indat eq %then %do;
           %put ERROR: Input dataset is missing in macro call;
          %return;
         %end;
         %else %do;
          *Custom  message when Input dataset is invalid;
           %if %sysfunc(exist(&indat)) ne 1 %then %do;
             %put ERROR: Invalid dataset;
            %return;
           %end;
           %else %do;
             *Custom  message when Output dataset is missing in macro call;
             %if &outdat eq %then %do;
               %put ERROR: Output dataset is missing in macro call;
              %return;
             %end;
             %else %do;
              *Custom  message when by variable is missing in macro call;
               %if &byvars eq %then %do;
                 %put ERROR: By variable is missing in macro call;
                %return;
               %end;
               %else %do;
                *Custom  message when last. by variable is missing in macro call;
                 %if &lastby eq %then %do;
                   %put ERROR: Last by variable is missing in macro call;
                  %return;
                 %end;
                 %else %do;
                  *Custom  message when result variable is missing in macro call;
                   %if &result eq %then %do;
                     %put ERROR: Result variable is missing in macro call;
                    %return;
                   %end;
                   %else %do;
                    *Custom  message when Domain name is missing in macro call;
                     %if &dnam eq %then %do;
                       %put ERROR: Domain name is missing in macro call;
                      %return;
                     %end;
                     %else %do;
                       *RFXSTDTC from DM domain;
                       proc sort data=&lib..dm out=dm1(keep=usubjid rfxstdtc);
                         by usubjid;
                       run;
                       
                       proc sort data=&indat;
                         by usubjid;
                       run;
       
                       *Handling the situation of date only;
                       %if &spdtfl=0 %then %do;
                         data rfxs;
                           merge &indat(in=a) dm1;
                           by usubjid;
                           if a;                 
                          if ~cmiss(&datvar) and length(&datvar)>10 then      dat=substr(&datvar,1,10);
                            else dat=&datvar;
                          if ~cmiss(rfxstdtc) and length(rfxstdtc)>10 then      rfx=substr(rfxstdtc,1,10);
                             else rfx=rfxstdtc; 
                           if ~cmiss(&result) and (dat le rfx);
                         run;
                       %end;
       
                       *Includes date and time whether if sponsor specified;
                       %if &spdtfl=1 %then %do; 
                          data rfxs;
                            merge &indat(in=a) dm1;
                            by usubjid;
                            if a;
                           if ~cmiss(&datvar) and length(&datvar)=10 then      &datvar=&datvar||"T:00:00";
                            if ~cmiss(&datvar) then datmf="Y"; 
                           if ~cmiss(&result) and datmf="Y" and (&datvar le rfxstdtc);
                         run;
                       %end;
       
                       *sorted with by variables;
                       proc sort data=rfxs;
                         by &byvars;
                       run;
                       
                       data out(keep=&byvars &datvar &dnam.blfl) ;
                         set rfxs;
                         by &byvars;
                         if last.&lastby then &dnam.blfl="Y";
                       run;
                        
                       *Merge with original dataset;
                       proc sort data=out out=out1;
                         by &byvars &datvar;
                       run;
       
                       proc sort data=&indat out=input;
                         by &byvars &datvar;
                       run;
       
                       data &outdat;
                         merge input(in=a) out1;
                         by &byvars &datvar ;
                         if a;
                       run; 
                      *To delete the internal datasets used for creating macro;
                      proc delete data=dm1 input out out1 rfxs;
                      run;
                    %end;
                  %end;
                %end;
              %end;
            %end;
           %end;  
         %end;
       %end;
     %mend baseline;

Back