/* $Id: simple1.sas,v 1.3 2009/06/23 19:04:52 kkleinma Exp $ */ /* These examples use the HELP data sets, available from http://www.math.smith.edu/sasr/datasets.php. The code provided in these code files reads the data sets in the various formats provided there. For smooth running, we suggest downloading all of the files available. For Windows based systems, the files should be stored in c:\data. The code in the book was run on a windows system in this configuration. For other operating systems, you will need to modify the code below to reflect different directory structures and references. */ *** Code chunk number 1 ***; proc import datafile='c:/book/help.csv' out=dsprelim dbms=dlm; delimiter=','; getnames=yes; run; data ds; set dsprelim; keep id cesd f1a -- f1t i1 i2 female treat; run; *** Code chunk number 2 ***; proc contents data=ds; run; *** Code chunk number 3 ***; proc contents data=ds short; run; *** Code chunk number 4 ***; proc print data=ds (obs=5) width=minimum; run; *** Code chunk number 5 ***; libname book 'c:/temp'; data book.ds (label = "HELP baseline dataset"); set ds; run; *** Code chunk number 6 ***; proc export data=ds replace outfile="c:/temp/ds.xls" dbms=excel; run; *** Code chunk number 7 ***; proc print data=ds (obs=10); var cesd; run; *** Code chunk number 8 ***; proc print data=ds; where cesd gt 55; var cesd; run; *** Code chunk number 9 ***; proc sort data=ds out=dss1; by cesd; run; proc print data=dss1 (obs=4); var id cesd i1 treat; run; *** Code chunk number 10 ***; data cesd; set ds; /* list of backwards questions */ array backwards [*] f1d f1h f1l f1p; /* for each, subtract the stored value from 3 */ do i = 1 to dim(backwards); backwards[i] = 3 - backwards[i]; end; /* this generates the sum of the non-missing questions */ newcesd = sum(of f1a -- f1t); /* This counts the number of missing values, per person */ nmisscesd = nmiss(of f1a -- f1t); /* this gives the sum, imputing the mean of non-missing */ imputemeancesd = mean(of f1a -- f1t) * 20; run; *** Code chunk number 11 ***; proc print data=cesd (obs=20); where nmisscesd gt 0; var cesd newcesd nmisscesd imputemeancesd; run; *** Code chunk number 12 ***; data ds2; set ds; if i1 eq 0 then drinkstat="abstinent"; if (i1 eq 1 and i2 le 3 and female eq 1) or (((i1 eq 1) or (i1 eq 2)) and i2 le 4 and female eq 0) then drinkstat="moderate"; if (((i1 gt 1) or (i2 gt 3)) and female eq 1) or (((i1 gt 2) or (i2 gt 4)) and female eq 0) then drinkstat="highrisk"; if nmiss(i1,i2,female) ne 0 then drinkstat=""; run; *** Code chunk number 13 ***; proc print data=ds2 (firstobs=361 obs=370); var i1 i2 female drinkstat; run; *** Code chunk number 14 ***; proc print data=ds2; where drinkstat eq "moderate" and female eq 1; var i1 i2 female drinkstat; run; *** Code chunk number 15 ***; proc freq data=ds2; tables drinkstat; run; *** Code chunk number 16 ***; proc freq data=ds2; tables drinkstat*female; run; *** Code chunk number 17 ***; data ds3; set ds; if female eq 1 then gender="Female"; else if female eq 0 then gender="male"; run; proc freq data=ds3; tables female gender; run; *** Code chunk number 18 ***; proc sort data=ds; by cesd i1; run; proc print data=ds (obs=5); var id cesd i1; run; *** Code chunk number 19 ***; data females; set ds; where female eq 1; run; proc means data=females mean maxdec=1; var cesd; run; *** Code chunk number 20 ***; proc sort data=ds; by female; run; proc means data=ds mean maxdec=2; by female; var cesd; run; *** Code chunk number 21 ***; data dists; do x = -4 to 4 by .1; normal_01 = sqrt(2 * constant('PI'))**(-1) * exp(-1 * ((x*x)/2)) ; dfval = 1; t_1df = (gamma((dfval +1)/2) / (sqrt(dfval * constant('PI')) * gamma(dfval/2))) * (1 + (x*x)/dfval)**(-1 * ((dfval + 1)/2)); output; end; run; *** Code chunk number 22 ***; goptions reset=all; legend1 label=none position=(top inside right) frame down=2 value = ("N(0,1)" tick=2 "t with 1 df"); axis1 label=(angle=90 "f(x)") minor=none order=(0 to .4 by .1); axis2 minor=none order=(-4 to 4 by 2); symbol1 i=j v=none l=1 c=black w=5; symbol2 i=j v=none l=21 c=black w=5; proc gplot data= dists; plot (normal_01 t_1df) * x / overlay legend=legend1 vaxis=axis1 haxis=axis2; run; quit;