
LOG for Lecture 10 SAS Session
================================  9/29/08

* WE CONTINUE DISCUSSING FREQUENCY CROSS-TABULATION
 AND STATISTICS FOR TESTING BIVARIATE RELATIONSHIPS ;

  libname mydir "SASdat";
    data polynes;
              set mydir.polynest;
 
  data newpoly (keep = deforstn ElevCat LatCat rainCat age AGEgp);
    set polynes;
    if (deforstn ne .) then deforstn = min(deforstn, 3.5);
    if (age ne .) then  age = (age ge 3);
    LatCat = (LatitS ge 16.3);
    ElevCat = (elev ge 1000);
    if (rain ne .) then RainCat = (rain ge 2000); 
         else rain=. ; 
    AGEgp = int(age+0.5); run;


/* Consider the UNpaired T-Test: it is not really valid here
   because the deforstn data are SO non-normal      */

   options nodate nocenter linesize=65;
proc ttest data=newpoly;
   class AgeGp;
   var deforstn;
     run;
                           Statistics

                             Lower CL          Upper CL  Lower CL
Variable  AGEgp           N      Mean    Mean      Mean   Std Dev

Deforstn                 28    2.5414  2.7857      3.03     0.498
           0
Deforstn                 47    2.9341  3.0851    3.2361    0.4272
           1
Deforstn  Diff (1-2)           -0.566  -0.299    -0.033    0.4819

                         Statistics

                                Upper CL
Variable  AGEgp       Std Dev    Std Dev   Std Err   Minimum

Deforstn               0.6299     0.8574     0.119         2
           0
Deforstn               0.5141     0.6458     0.075         2
           1
Deforstn  Diff (1-2)   0.5598      0.668    0.1336

...

                            T-Tests

Variable   Method          Variances     DF   t Value   Pr > |t|

Deforstn   Pooled          Equal         73     -2.24     0.0281
Deforstn   Satterthwaite   Unequal     48.2     -2.13     0.0385


                    Equality of Variances

Variable    Method      Num DF    Den DF    F Value    Pr > F

Deforstn    Folded F        27        46       1.50    0.2211



/* NOW HERE IS A COMPARISON BASED ON CHI-SQUARES IN FREQUENCY 
   TABLES THAT IS MUCH MORE REASONABLE IN THIS SETTING ;


proc freq data=newpoly;
   tables  Deforstn*AgegP/ nopercent norow nocol chisq;
run;

Table of Deforstn by AGEgp

Deforstn     AGEgp

Frequency|       0|       1|  Total
---------+--------+--------+
       2 |     10 |      7 |     17
---------+--------+--------+
       3 |     10 |     18 |     28
---------+--------+--------+
     3.5 |      8 |     22 |     30
---------+--------+--------+
Total          28       47       75

Frequency Missing = 5

Statistics for Table of Deforstn by AGEgp

Statistic                     DF       Value      Prob
------------------------------------------------------
Chi-Square                     2      4.8461    0.0886
Likelihood Ratio Chi-Square    2      4.7778    0.0917
Mantel-Haenszel Chi-Square     1      4.7609    0.0291


proc freq data=newpoly noprint;
   tables  Deforstn*AgegP/ 
       nopercent norow nocol expected out = tabdat1;
   run;

proc print data=tabdat1; run;

Obs    Deforstn    AGEgp    COUNT    PERCENT

 1         .         0         1       .
 2        2.0        .         3       .
 3        2.0        0        10     13.3333
 4        2.0        1         7      9.3333
 5        3.0        .         1       .
 6        3.0        0        10     13.3333
 7        3.0        1        18     24.0000
 8        3.5        0         8     10.6667
 9        3.5        1        22     29.3333

data tabdat2;
   set tabdat1 (drop=PERCENT);
   if (Deforstn ne . and agegp ne .); 
 proc print; run;

Obs    Deforstn    AGEgp    COUNT

 1        2.0        0        10
 2        2.0        1         7
 3        3.0        0        10
 4        3.0        1        18
 5        3.5        0         8
 6        3.5        1        22


proc freq data=tabdat2;
   tables Deforstn*Agegp /  nopercent norow nocol ;
   weight count; run;
/* gives same contingency table as above !! */

*===============================================
   PROGRAMMING CONSIDERATIONS
 ===============================================;

/* (i) we use grouping variables where possible for matching;
   (ii) to create a matched file, use OUTPUT statements 
         together with naming of multiple files in single 
        DATA statement in data-step.    

In the Example, we retain only a set of 24 paired observations 
   in which the same island name appears both with suffix 
   "Lee"=L and "Windward"=W are retained and compared */

   data polynes2; 
      set polynes;
      namlen=length(islnd);
      namLab=substr(islnd,namlen,1); /* gives the last character*/
      if(namLab in ("L","W")); run;

/* Now we can use the namLab field to create a set of 12 
    matched-pair differences */

   data polynes3 (keep=dif);
      set polynes2 (keep = deforstn namLab);
      if(namLab="L") then dif+deforstn;
      else do; dif+(-1*deforstn); output; end;
             /* 12 observations at this point */
   proc means data=polynes3 t;
     var dif; run;

 proc means data=polynes3 t, prt;
     var dif; run;

Analysis Variable : dif

t Value    Pr > |t|
-------------------
   5.40      0.0002
-------------------
