%unistats with "by" variables

(Author: Roland Rashleigh-Berry                                                                Date: 27 Sep 2011)

Introduction

You can use "by" variables with the %unistats macro. Just define them to the byvars= parameter. But if you want your treatment arm population to be that for each "by" value then you have to do some extra work. How to do this will be explained in this document. You can use the same technique with the %npcttab macro so this description of how to handle "by" variables will not be repeated for that macro. The explanation given in this document applies to both macros.

This document is an extension to the previous document detailing how to use the %unistats macro and will assume you have test datasets available.

Using "by" variables

When you are using "by" variables and you want your column totals to be that for the value of the "by" variable and not the total population for the treatment arms, then a separate report must be produced for each "by" value. This is because a format is used to display the (N=xxx) totals and formats can not be dynamic. But these separate reports will be contained in a single output report so this will not cause any inconvenience.

You have to be aware of the logic. Once you have that clear in your mind then it is easy. Here is the logic for not using "by" variables.
 
Call %titles

Select account data on population (dataset referred to as "acct" later)

Call %popfmt

Merge reporting data with acct dataset and keep the data that matches with the acct data (outut dataset referred to as "repdata" later)

Call %openrep

Call %unistats to produce the report from repdata

Call %closerep

Here is the logic for using "by" variables. Note the use of the %nobs and %getvalue macro.
 
If inside a macro, declare a local macro variable "byval" (for example)

Call %titles

Select account data on population (dataset referred to as "acct" later)

Merge reporting data with acct dataset and keep the data that matches with the acct data (output dataset referred to as "repdata" later)

Create a "by" value dataset from the acct dataset with just the unique "by" values in (dataset referred to as "bydset" later and "by" variable referred to as "byval" for this example)

Call %openrep

%do i=1 %to %nobs(bydset);
  %let byval=%getvalue(bydset,byval,&i);

  data acct2;
    set acct(where=(byval=&byval));
  run;

  Run %popfmt on the acct2 dataset

  Call %unistats to produce the report from repdata subsetting on &byval

%end;   *% end of do loop -;

Call %closerep

An example

The example that follows carries on from the example shown in the document on %unistats for demonstration 7. The code is shown first followed by the report produced. The macro %titles, %openrep and %closerep will be commented out in the code. The "by" line is left as a dashed line for clarity.
 
/* Demonstrate %unistats: program 7 amended for a "by" variable */ 
options noovp nodate nonumber center 
missing=" " formchar='|_---|+|---+=|-/\<>*'; 

options sasautos=("C:\spectre\macros" SASAUTOS); 

options ls=78 ps=63; 

%*titles;  %*- macro commented out -;

title1; 

proc format; 
  value racecd 
  1="CAUCASIAN" 
  2="BLACK" 
  3="ASIAN" 
  4="HISPANIC" 
  5="OTHER" 
  ; 
  value sexcd 
  1="MALE" 
  2="FEMALE" 
  ; 
  value trtnarr 
  1="Ambident@(1g/day)" 
  2="Betamax@(500mg/day)" 
  3="No@treatment" 
  ; 
run; 
 

proc sort data=sasuser.demog(where=(fascd=1)) 
           out=demog(drop=fascd); 
  by patno invid; 
run; 

data demog; 
  set demog; 
  format trtcd trtnarr.; 
  label sexcd="Gender" 
        racecd="Race" 
        age="Age (yrs)" 
        weight="Weight (kg)" 
        height="Height (cm)" 
        ; 
run; 

proc sort nodupkey data=demog(keep=invid) out=bydset;
  by invid;
run;

%*openrep;   %*- macro commented out -;

%macro rep;
  %local i invid;
  %do i=1 %to %nobs(bydset);
    %let invid=%getvalue(bydset,invid,&i);

    %popfmt(demog(where=(invid=&invid)),trtcd,uniqueid=patno invid) 

    %unistats(dsin=demog(where=(invid=&invid)),total=yes,byvars=invid,
    trtlabel="Number of Patients (%) /" "Descriptive Statistics" " ", 
    lowcasevarlist=sexcd racecd, 
    descstats=N Min Mean Max STD., 
    minfmt=3.,maxfmt=3., 
    indent=3,trtspace=6, 
    varlist=sexcd racecd age weight/m height/m, 
    allcatvars=racecd)

  %end;
%mend rep;

%rep;

%*closerep;   %*- macro commented out -;

And here is the output shown for the first two invid's only.
 
---------------------------- INVESTIGATOR ID=1000 ----------------------------

______________________________________________________________________________

                                           Number of Patients (%) /
                                            Descriptive Statistics

                                  Ambident          Betamax
                                  (1g/day)        (500mg/day)         Total
                                    (N=2)            (N=1)            (N=3)
______________________________________________________________________________

Gender
   Male                            1 ( 50.0)        1 (100.0)        2 ( 66.7)
   Female                          1 ( 50.0)        0 (  0.0)        1 ( 33.3)

Race
   Caucasian                       2 (100.0)        0 (  0.0)        2 ( 66.7)
   Black                           0 (  0.0)        1 (100.0)        1 ( 33.3)
   Asian                           0 (  0.0)        0 (  0.0)        0 (  0.0)
   Hispanic                        0 (  0.0)        0 (  0.0)        0 (  0.0)
   Other                           0 (  0.0)        0 (  0.0)        0 (  0.0)

Age (yrs)
   N                               2                1                3        
   Min                            20               16               16        
   Mean                           25.0             16.0             22.0      
   Max                            30               16               30        
   STD.                            7.07                              7.21     

Weight (kg)
   N                               2                1                3        
   Min                            78.1             65.5             65.5      
   Mean                           78.9             65.5             74.4      
   Max                            79.6             65.5             79.6      
   STD.                            1.06                              7.74     

Height (cm)
   N                               2                1                3        
   Min                           200.0            175.0            175.0      
   Mean                          206.0            175.0            195.7      
   Max                           212.0            175.0            212.0      
   STD.                            8.49                             18.88     


---------------------------- INVESTIGATOR ID=2000 ----------------------------

______________________________________________________________________________

                                           Number of Patients (%) /
                                            Descriptive Statistics

                                  Ambident          Betamax
                                  (1g/day)        (500mg/day)         Total
                                    (N=2)            (N=2)            (N=4)
______________________________________________________________________________

Gender
   Male                            1 ( 50.0)        1 ( 50.0)        2 ( 50.0)
   Female                          1 ( 50.0)        1 ( 50.0)        2 ( 50.0)

Race
   Caucasian                       2 (100.0)        0 (  0.0)        2 ( 50.0)
   Black                           0 (  0.0)        0 (  0.0)        0 (  0.0)
   Asian                           0 (  0.0)        1 ( 50.0)        1 ( 25.0)
   Hispanic                        0 (  0.0)        0 (  0.0)        0 (  0.0)
   Other                           0 (  0.0)        1 ( 50.0)        1 ( 25.0)

Age (yrs)
   N                               2                2                4        
   Min                            20               16               16        
   Mean                           30.0             26.0             28.0      
   Max                            40               36               40        
   STD.                           14.14            14.14            11.78     

Weight (kg)
   N                               2                2                4        
   Min                            78.1             65.5             65.5      
   Mean                           78.9             70.4             74.6      
   Max                            79.6             75.3             79.6      
   STD.                            1.06             6.93             6.34     

Height (cm)
   N                               2                2                4        
   Min                           200.0            175.0            175.0      
   Mean                          206.0            180.5            193.3      
   Max                           212.0            186.0            212.0      
   STD.                            8.49             7.78            16.15     
 

Conclusion

You have seen how to call %unistats with a "by" variable such that the treatment arm population is only that for the corresponding "by" value. The same technique can be used for the %npcttab macro. You need to know this method so that you can cope with this and other similar situations.
 
 

Use the "Back" button of your browser to return to the previous page

contact the author