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 'coeffsa'.;
*                                                                                ;
      proc logistic descending data = temp noprint covout outest = coeffsa ;
           model y = x ;
      run ;

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

      data coeffs ;
        retain xcoeff ;
        set coeffsa ;
        if _type_ eq 'PARMS' then xcoeff = x ;
        if _type_ eq 'COV' and _name_ eq 'x' then do ;
           xstderr = sqrt(x) ;
           tstat = xcoeff / xstderr ;
           df = &m - 2 ;
           pvalue = 1 - probt(abs(tstat), df) ;
           output ;
        end ;

      run ;

      data &coeffdataset ;
          set &coeffdataset coeffs ;
      run ;

  %end ;

  options linesize = 160 ;

  proc print data = coeffsa ;
  title 'Printout of the dataset coeffsa :' ;
  run ;

  proc sort data = &coeffdataset ; by pvalue ;
  run ;

  proc print data = &coeffdataset ;
  title 'Printout of the file of the summary stats for each simulation, ordered by p-value' ;
  run ;

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

%mend ;

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

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


%logistic(1, 1, 100, 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 ;

                                                               Printout of the dataset coeffsa :                            18:39 Monday, September 19, 2011   1

                                    Obs    _LINK_    _TYPE_     _STATUS_      _NAME_       Intercept        x       _LNLIKE_

                                     1     LOGIT     PARMS     0 Converged    y              0.73691     1.10637    -51.9369
                                     2     LOGIT     COV       0 Converged    Intercept      0.23147    -0.37824    -51.9369
                                     3     LOGIT     COV       0 Converged    x             -0.37824     0.83033    -51.9369






                                       Printout of the file of the summary stats for each simulation, ordered by p-value    18:39 Monday, September 19, 2011   2

            Obs     xcoeff     _LINK_    _TYPE_     _STATUS_      _NAME_    Intercept       x       _LNLIKE_    xstderr      tstat     df     pvalue

              1      .                                                         .          .            .         .           .          .     .     
              2     2.74396    LOGIT      COV      0 Converged      x        -0.33540    0.86042    -47.6829    0.92759     2.95817    98    0.00194
              3     2.74053    LOGIT      COV      0 Converged      x        -0.40303    1.02295    -41.4438    1.01141     2.70962    98    0.00398
              4     2.65420    LOGIT      COV      0 Converged      x        -0.45481    1.06114    -46.3651    1.03012     2.57660    98    0.00573
              5     2.06078    LOGIT      COV      0 Converged      x        -0.30209    0.74737    -52.0172    0.86451     2.38376    98    0.00953
              6     1.88159    LOGIT      COV      0 Converged      x        -0.25292    0.67448    -49.8179    0.82126     2.29108    98    0.01205
              7     3.23593    LOGIT      COV      0 Converged      x        -0.60573    2.09117    -33.5802    1.44609     2.23771    98    0.01375
              8     2.18704    LOGIT      COV      0 Converged      x        -0.37295    1.01524    -42.9328    1.00759     2.17057    98    0.01619
              9     2.12394    LOGIT      COV      0 Converged      x        -0.39581    1.06705    -43.1927    1.03298     2.05613    98    0.02121
             10     2.15892    LOGIT      COV      0 Converged      x        -0.40932    1.12630    -43.2865    1.06127     2.03427    98    0.02231
             11     1.93204    LOGIT      COV      0 Converged      x        -0.38496    0.91241    -44.9454    0.95520     2.02265    98    0.02292
             12     2.16036    LOGIT      COV      0 Converged      x        -0.44643    1.21180    -34.5670    1.10082     1.96250    98    0.02627
             13     1.78117    LOGIT      COV      0 Converged      x        -0.35305    0.84672    -50.6925    0.92018     1.93568    98    0.02789
             14     1.68810    LOGIT      COV      0 Converged      x        -0.35444    0.76988    -50.7405    0.87743     1.92391    98    0.02863
             15     1.81308    LOGIT      COV      0 Converged      x        -0.38015    0.90076    -42.0510    0.94908     1.91035    98    0.02951
             16     1.80044    LOGIT      COV      0 Converged      x        -0.34804    0.89991    -43.6063    0.94864     1.89792    98    0.03033
             17     1.65506    LOGIT      COV      0 Converged      x        -0.30607    0.79180    -52.0783    0.88983     1.85996    98    0.03294
             18     1.91431    LOGIT      COV      0 Converged      x        -0.39965    1.07796    -42.1196    1.03825     1.84379    98    0.03412
             19     1.73971    LOGIT      COV      0 Converged      x        -0.37991    0.91642    -42.1844    0.95730     1.81730    98    0.03611
             20     1.52836    LOGIT      COV      0 Converged      x        -0.33920    0.76684    -49.8244    0.87570     1.74531    98    0.04203
             21     1.76445    LOGIT      COV      0 Converged      x        -0.41453    1.04223    -43.9734    1.02090     1.72833    98    0.04354
             22     1.54856    LOGIT      COV      0 Converged      x        -0.37326    0.83337    -52.4167    0.91289     1.69633    98    0.04650
             23     1.70827    LOGIT      COV      0 Converged      x        -0.41007    1.03234    -40.7615    1.01604     1.68131    98    0.04794
             24     1.39145    LOGIT      COV      0 Converged      x        -0.31623    0.68494    -51.2288    0.82761     1.68129    98    0.04795
             25     1.71087    LOGIT      COV      0 Converged      x        -0.41245    1.03717    -40.7586    1.01841     1.67994    98    0.04808
             26     1.81084    LOGIT      COV      0 Converged      x        -0.48866    1.17752    -39.0248    1.08513     1.66877    98    0.04918
             27     1.34162    LOGIT      COV      0 Converged      x        -0.30014    0.69680    -54.8776    0.83475     1.60722    98    0.05561
             28     1.41569    LOGIT      COV      0 Converged      x        -0.31252    0.77826    -50.0433    0.88219     1.60475    98    0.05588
             29     1.66658    LOGIT      COV      0 Converged      x        -0.45350    1.16965    -41.0182    1.08150     1.54098    98    0.06327
             30     1.78639    LOGIT      COV      0 Converged      x        -0.54065    1.34884    -37.3926    1.16140     1.53814    98    0.06362
             31     1.51538    LOGIT      COV      0 Converged      x        -0.41584    1.03968    -41.0984    1.01965     1.48618    98    0.07022
             32     1.45308    LOGIT      COV      0 Converged      x        -0.42253    0.99971    -46.0300    0.99985     1.45329    98    0.07467
             33     1.25966    LOGIT      COV      0 Converged      x        -0.33469    0.77877    -46.0863    0.88248     1.42740    98    0.07832
             34     1.27073    LOGIT      COV      0 Converged      x        -0.36524    0.83375    -52.9336    0.91310     1.39167    98    0.08359
             35     1.18751    LOGIT      COV      0 Converged      x        -0.32217    0.75536    -50.4276    0.86911     1.36635    98    0.08748
             36     1.10637    LOGIT      COV      0 Converged      x        -0.37824    0.83033    -51.9369    0.91122     1.21416    98    0.11380
             37     1.21034    LOGIT      COV      0 Converged      x        -0.45570    1.02228    -41.5381    1.01108     1.19708    98    0.11708
             38     1.23930    LOGIT      COV      0 Converged      x        -0.46076    1.12050    -43.2544    1.05854     1.17076    98    0.12227
             39     0.95196    LOGIT      COV      0 Converged      x        -0.29220    0.68483    -55.5543    0.82755     1.15034    98    0.12640
             40     0.87955    LOGIT      COV      0 Converged      x        -0.31344    0.65908    -55.6331    0.81184     1.08341    98    0.14064
             41     1.21056    LOGIT      COV      0 Converged      x        -0.55405    1.28121    -34.0625    1.13191     1.06949    98    0.14374
             42    -0.83752    LOGIT      COV      0 Converged      x        -0.32579    0.61731    -54.5284    0.78569    -1.06596    98    0.14453
             43     0.96180    LOGIT      COV      0 Converged      x        -0.37441    0.82483    -48.0553    0.90820     1.05902    98    0.14610
             44     0.90754    LOGIT      COV      0 Converged      x        -0.36672    0.73572    -50.8278    0.85774     1.05806    98    0.14632
             45     1.24951    LOGIT      COV      0 Converged      x        -0.65115    1.43554    -34.1070    1.19814     1.04287    98    0.14979
             46     0.96252    LOGIT      COV      0 Converged      x        -0.39714    0.88649    -50.8604    0.94154     1.02229    98    0.15458
             47     0.93848    LOGIT      COV      0 Converged      x        -0.35354    0.89816    -45.0821    0.94771     0.99026    98    0.16224
             48     0.75921    LOGIT      COV      0 Converged      x        -0.30339    0.61363    -56.8309    0.78335     0.96919    98    0.16742
             49     0.87229    LOGIT      COV      0 Converged      x        -0.35730    0.81131    -46.6610    0.90073     0.96843    98    0.16761
             50     0.86151    LOGIT      COV      0 Converged      x        -0.40565    0.80427    -48.1588    0.89681     0.96063    98    0.16955
             51     0.84505    LOGIT      COV      0 Converged      x        -0.33576    0.79025    -49.5736    0.88896     0.95061    98    0.17207
             52     0.82301    LOGIT      COV      0 Converged      x        -0.36027    0.76319    -49.5951    0.87361     0.94208    98    0.17423
             53     0.68840    LOGIT      COV      0 Converged      x        -0.26726    0.57735    -56.8906    0.75984     0.90598    98    0.18358
             54    -0.84046    LOGIT      COV      0 Converged      x        -0.50800    0.87284    -45.1764    0.93426    -0.89960    98    0.18527
             55     0.72481    LOGIT      COV      0 Converged      x        -0.32572    0.71860    -55.8632    0.84770     0.85503    98    0.19731
             56     0.76545    LOGIT      COV      0 Converged      x        -0.38827    0.80243    -45.2205    0.89578     0.85450    98    0.19746
                                       Printout of the file of the summary stats for each simulation, ordered by p-value    18:39 Monday, September 19, 2011   3

            Obs     xcoeff     _LINK_    _TYPE_     _STATUS_      _NAME_    Intercept       x       _LNLIKE_    xstderr      tstat     df     pvalue

             57     0.82489    LOGIT      COV      0 Converged      x        -0.45142    0.96067    -43.6076    0.98014     0.84161    98    0.20103
             58     0.75255    LOGIT      COV      0 Converged      x        -0.37972    0.89347    -45.2652    0.94523     0.79615    98    0.21393
             59     0.64240    LOGIT      COV      0 Converged      x        -0.30842    0.66075    -52.3773    0.81286     0.79029    98    0.21563
             60     0.59227    LOGIT      COV      0 Converged      x        -0.28860    0.59696    -55.9383    0.77263     0.76656    98    0.22259
             61     0.65559    LOGIT      COV      0 Converged      x        -0.39474    0.77089    -48.3416    0.87801     0.74669    98    0.22852
             62     0.59926    LOGIT      COV      0 Converged      x        -0.32628    0.69022    -51.1328    0.83079     0.72131    98    0.23622
             63     0.66757    LOGIT      COV      0 Converged      x        -0.41934    0.88645    -43.7137    0.94151     0.70903    98    0.23999
             64     0.60851    LOGIT      COV      0 Converged      x        -0.36047    0.74075    -51.1450    0.86067     0.70702    98    0.24062
             65    -0.63441    LOGIT      COV      0 Converged      x        -0.51437    0.96884    -43.7596    0.98429    -0.64453    98    0.26037
             66     0.55693    LOGIT      COV      0 Converged      x        -0.40025    0.85132    -42.0875    0.92267     0.60360    98    0.27375
             67    -0.49335    LOGIT      COV      0 Converged      x        -0.37918    0.67724    -52.5094    0.82295    -0.59949    98    0.27511
             68    -0.50342    LOGIT      COV      0 Converged      x        -0.36748    0.70899    -49.8604    0.84201    -0.59788    98    0.27565
             69     0.58614    LOGIT      COV      0 Converged      x        -0.45342    0.99542    -36.5177    0.99770     0.58749    98    0.27911
             70     0.58914    LOGIT      COV      0 Converged      x        -0.43881    1.12161    -38.4816    1.05906     0.55629    98    0.28964
             71     0.45730    LOGIT      COV      0 Converged      x        -0.33391    0.73785    -49.8978    0.85898     0.53238    98    0.29784
             72    -0.47174    LOGIT      COV      0 Converged      x        -0.38276    0.79053    -46.9983    0.88912    -0.53057    98    0.29846
             73     0.47325    LOGIT      COV      0 Converged      x        -0.38272    0.83429    -45.4530    0.91340     0.51812    98    0.30277
             74     0.45582    LOGIT      COV      0 Converged      x        -0.41287    0.91084    -40.3812    0.95438     0.47760    98    0.31700
             75     0.40916    LOGIT      COV      0 Converged      x        -0.35650    0.77160    -53.8189    0.87841     0.46579    98    0.32120
             76    -0.40589    LOGIT      COV      0 Converged      x        -0.37989    0.79401    -48.5185    0.89107    -0.45550    98    0.32488
             77     0.33583    LOGIT      COV      0 Converged      x        -0.25733    0.58735    -57.2093    0.76639     0.43820    98    0.33110
             78     0.49794    LOGIT      COV      0 Converged      x        -0.64884    1.34606    -32.4159    1.16020     0.42919    98    0.33437
             79     0.37528    LOGIT      COV      0 Converged      x        -0.38854    0.87698    -48.5417    0.93647     0.40074    98    0.34474
             80     0.34246    LOGIT      COV      0 Converged      x        -0.41414    0.79227    -52.6168    0.89010     0.38474    98    0.35063
             81     0.33504    LOGIT      COV      0 Converged      x        -0.35969    0.77017    -48.5492    0.87759     0.38178    98    0.35173
             82     0.32126    LOGIT      COV      0 Converged      x        -0.32834    0.72505    -52.6193    0.85150     0.37728    98    0.35339
             83     0.36383    LOGIT      COV      0 Converged      x        -0.56205    1.04660    -38.5755    1.02304     0.35563    98    0.36144
             84     0.35376    LOGIT      COV      0 Converged      x        -0.45257    1.05791    -40.4367    1.02855     0.34394    98    0.36581
             85     0.26264    LOGIT      COV      0 Converged      x        -0.44082    0.99370    -42.2360    0.99685     0.26348    98    0.39637
             86     0.22597    LOGIT      COV      0 Converged      x        -0.45868    1.00520    -38.6132    1.00260     0.22538    98    0.41108
             87    -0.18262    LOGIT      COV      0 Converged      x        -0.52074    0.99932    -40.4796    0.99966    -0.18268    98    0.42771
             88     0.19941    LOGIT      COV      0 Converged      x        -0.66884    1.24108    -36.6765    1.11404     0.17900    98    0.42915
             89     0.19234    LOGIT      COV      0 Converged      x        -0.58859    1.24785    -32.4934    1.11707     0.17218    98    0.43183
             90     0.13971    LOGIT      COV      0 Converged      x        -0.40021    0.80495    -51.3836    0.89719     0.15572    98    0.43829
             91     0.13457    LOGIT      COV      0 Converged      x        -0.34973    0.75729    -55.0960    0.87022     0.15464    98    0.43871
             92    -0.12566    LOGIT      COV      0 Converged      x        -0.45993    0.86560    -40.4872    0.93038    -0.13506    98    0.44642
             93     0.11752    LOGIT      COV      0 Converged      x        -0.48449    0.93527    -43.9596    0.96709     0.12152    98    0.45176
             94     0.11068    LOGIT      COV      0 Converged      x        -0.48453    1.00598    -45.5825    1.00299     0.11035    98    0.45618
             95     0.07204    LOGIT      COV      0 Converged      x        -0.47228    0.87691    -47.1364    0.93643     0.07694    98    0.46942
             96    -0.06619    LOGIT      COV      0 Converged      x        -0.43706    0.87714    -45.5861    0.93655    -0.07067    98    0.47190
             97    -0.05339    LOGIT      COV      0 Converged      x        -0.31146    0.66468    -53.9255    0.81528    -0.06548    98    0.47396
             98    -0.02430    LOGIT      COV      0 Converged      x        -0.40910    0.81066    -51.3953    0.90037    -0.02699    98    0.48926
             99    -0.01357    LOGIT      COV      0 Converged      x        -0.37431    0.72192    -47.1392    0.84966    -0.01598    98    0.49364
            100    -0.01207    LOGIT      COV      0 Converged      x        -0.33292    0.70650    -50.0401    0.84054    -0.01436    98    0.49429
            101     0.01016    LOGIT      COV      0 Converged      x        -0.36155    0.78245    -48.6222    0.88456     0.01149    98    0.49543
                                       Printout of the file of the summary stats for each simulation, ordered by p-value    18:39 Monday, September 19, 2011   4

                                                                      The MEANS Procedure

                                                                                                Lower 95%       Upper 95%
       Variable     Label               N            Mean         Std Dev       Std Error     CL for Mean     CL for Mean         Minimum         Maximum
       --------------------------------------------------------------------------------------------------------------------------------------------------
       Intercept    Intercept: y=1    100      -0.3997910       0.0836304       0.0083630      -0.4163851      -0.3831969      -0.6688381      -0.2529241
       x                              100       0.8958078       0.2179952       0.0217995       0.8525529       0.9390628       0.5773528       2.0911728
       --------------------------------------------------------------------------------------------------------------------------------------------------
===============================================================================

~john-c/5421/notes.004.1   Last update: September 19, 2011.