HOW TO USE MACROS TO CALL PROCEDURES REPEATEDLY   SPH 7460 notes.004.1

The following is an example of a macro which calls the SAS procedure
PROC LOGISISTIC inside of a 'do loop' and accumulates the coefficient
estimates on a file.


options linesize = 100  MPRINT ;
footnote "~john-c/5421/logisticmacro.sas &sysdate &systime" ;

FILENAME GRAPH 'gsas.grf' ;
LIBNAME  loc '' ;

GOPTIONS
         RESET = GLOBAL
         ROTATE = PORTRAIT
         FTEXT = SWISSB
         DEVICE = PSCOLOR
         GACCESS = SASGASTD
         GSFNAME = GRAPH
         GSFMODE = REPLACE
         GUNIT = PCT BORDER
         CBACK = WHITE
         HTITLE = 2 HTEXT = 1
         HSIZE = 18 cm  VSIZE = 18 cm ;

*===================================================================== ;

*                                                                      ;
*  Example of a macro which calls a SAS procedure repeatedly:          ;
*                                                                      ;
*  The following is a macro which simulates a logistic-model           ;
*  dataset, calls proc logistic to compute coefficients,               ;
*  and accumulates the coefficient estimates on a data file called     ;
*  coeffdataset.  The accumulated dataset of coefficient estimates is  ;
*  printed at the end of the macro.                                    ;
*                                                                      ;

%macro logistic(b0, b1, m, n, coeffdataset) ;

*                                                                ;
* The following data step initializes the dataset 'coeffdataset' ;
*                                                                ;

  data &coeffdataset ;
  run ;

  %do i = 1 %to &m ;

      data temp ;

      %do j = 1 %to &n ;

          y = 0 ;
          x = ranuni(-1) ;
          p = 1 / (1 + exp(-&b0 - &b1*x)) ;
          r = ranuni(-1) ;
          if r < p then y = 1 ;

          output ;

      %end ;

      run ;

*                                                                                ;
*  Call to proc logistic.  Note this is inside a '%do' loop, inside the macro.   ;
*  Note also that the coefficient estimates are output to a file called 'coeffs'.;
*                                                                                ;
      proc logistic descending data = temp noprint outest = coeffs ;
           model y = x ;
      run ;

*                                                                                ;
*  The following datastep accumulates the coefficients from the various calls    ;
*  to proc logistic in one dataset called 'coeffdataset'                         ;
*                                                                                ;

      data &coeffdataset ;
          set &coeffdataset coeffs ;
      run ;

  %end ;

  proc print data = &coeffdataset ;

  proc means data = &coeffdataset  n mean stddev stderr min max ;
       var   intercept x ;

%mend ;

*==================================================================;

*                                                                  ;
* The following statement is the call to the macro.                ;
*                                                                  ;


%logistic(1, 1, 30, 100, coeffdataset) ;

symbol v = "o" w = 3 h = 2 ;

*                                                                   ;
* The following procedure graphs the intercept versus x-coefficient ;
* from the accumulated dataset ...                                  ;
*                                                                   ;

proc gplot data = coeffdataset ;
     plot x * intercept ;
title1 H = 2.5 'Plot of logistic coefficients: coeff of x versus intercept' ;
run ;

===============================================================================

Output from this program:

===============================================================================

                                                             19:37 Wednesday, September 26, 2007   1

       Obs    _LINK_    _TYPE_     _STATUS_      _NAME_    Intercept        x       _LNLIKE_

         1                                                    .           .            .    
         2    LOGIT     PARMS     0 Converged      y         2.38837    -1.48242    -44.4187
         3    LOGIT     PARMS     0 Converged      y         1.06651     0.68409    -49.7327
         4    LOGIT     PARMS     0 Converged      y         0.58941     1.71592    -49.4523
         5    LOGIT     PARMS     0 Converged      y         1.95525    -0.40968    -42.1946
         6    LOGIT     PARMS     0 Converged      y         0.97890     0.89543    -49.5665
         7    LOGIT     PARMS     0 Converged      y         0.85286     1.12910    -50.6428
         8    LOGIT     PARMS     0 Converged      y        -0.58293     3.97262    -43.7327
         9    LOGIT     PARMS     0 Converged      y         1.36174     0.59528    -43.7584
        10    LOGIT     PARMS     0 Converged      y         1.49442    -0.44524    -52.5395
        11    LOGIT     PARMS     0 Converged      y         0.82079     1.98642    -39.8019
        12    LOGIT     PARMS     0 Converged      y         1.13175     0.76769    -46.8025
        13    LOGIT     PARMS     0 Converged      y         0.52462     1.43194    -52.5688
        14    LOGIT     PARMS     0 Converged      y         0.68393     1.44372    -50.2226
        15    LOGIT     PARMS     0 Converged      y         0.98866     0.88555    -49.5350
        16    LOGIT     PARMS     0 Converged      y         1.28576     0.47735    -46.9825
        17    LOGIT     PARMS     0 Converged      y         1.46450     0.74510    -40.2091
        18    LOGIT     PARMS     0 Converged      y         0.25883     2.17649    -48.2783
        19    LOGIT     PARMS     0 Converged      y         0.85059     1.18296    -49.2636
        20    LOGIT     PARMS     0 Converged      y         0.05978     1.56459    -59.3243
        21    LOGIT     PARMS     0 Converged      y         0.81871     0.97036    -50.8355
        22    LOGIT     PARMS     0 Converged      y         0.36517     2.39165    -46.5041
        23    LOGIT     PARMS     0 Converged      y         0.87898     1.53978    -44.1106
        24    LOGIT     PARMS     0 Converged      y         1.13856     0.51716    -49.8569
        25    LOGIT     PARMS     0 Converged      y         1.31074     0.54572    -45.4076
        26    LOGIT     PARMS     0 Converged      y         1.74902     0.14045    -40.4864
        27    LOGIT     PARMS     0 Converged      y         0.81581     1.97882    -40.5770
        28    LOGIT     PARMS     0 Converged      y         1.34954     0.20305    -48.5970
        29    LOGIT     PARMS     0 Converged      y         1.06693     0.81938    -48.2318
        30    LOGIT     PARMS     0 Converged      y         1.90532    -0.91955    -48.1247
        31    LOGIT     PARMS     0 Converged      y         1.14795     1.73598    -35.4278
                                                             19:37 Wednesday, September 26, 2007   2

                                        The MEANS Procedure

   Variable   Label       N          Mean       Std Dev     Std Error       Minimum       Maximum
   ----------------------------------------------------------------------------------------------
   Intercept  Intercept  30     1.0240159     0.5918063     0.1080485    -0.5829293     2.3883741
   x                     30     0.9746579     1.0605258     0.1936246    -1.4824175     3.9726217
   ----------------------------------------------------------------------------------------------
===============================================================================

~john-c/5421/notes.004.1   Last update: September 26, 2007.