* Generic session options - page and line size, no page breaks;
OPTIONS nonumber nodate nocenter pagesize=MAX linesize=140 formdlim='-';
* Kill default titles on output;
TITLE;
LIBNAME example "D:\Teaching\2011\REMS Seminar - HLM\Lectures\04_29\SAS Example";
* Importing data into work library;
DATA grade10; SET example.grade10;
studentID = state_id;
schoolID = bldg;
districtID = usd;
boyvsgirl = gender;
IF race=4 THEN nonwhite=0;
ELSE IF race=. THEN nonwhite=.;
ELSE nonwhite=1;
IF sped=0 THEN hassped=0;
ELSE IF sped=. THEN hassped=.;
ELSE hassped=1;
IF iep=0 THEN hasiep=0;
ELSE IF iep=. THEN hasiep=.;
ELSE hasiep=1;
IF esol=0 THEN engsec=0;
ELSE IF esol=. THEN engsec=.;
ELSE engsec=1;
IF lunch=0 THEN frlunch=0;
ELSE IF lunch=. THEN frlunch=.;
ELSE frlunch=1;
math = tscore;
LABEL studentID= "studentID: Student ID number"
schoolID= "schoolID: School ID number"
districtID= "districtID: District ID number"
boyvsgirl= "boyvsgirl: Boy=0, Girl=1"
nonwhite= "nonwhite: White=0, NonWhite=1"
hassped= "hassped: 0=No SPED, 1=Special Ed"
hasiep= "hasiep: 0=No IEP, 1=Ind Ed Plan"
engsec= "engsec: 0=English first, 1=English second"
frlunch= "frlunch: 0=No, 1=Free/Reduced Lunch"
math= "math: Math Test Score";
KEEP studentID--math;
RUN;
* Creating school means for predictors;
PROC SORT DATA=grade10; BY schoolID studentID; RUN;
PROC MEANS NOPRINT N DATA=grade10; BY schoolID;
VAR boyvsgirl--math;
OUTPUT OUT=SchoolMeans
MEAN(boyvsgirl nonwhite hassped hasiep engsec frlunch math)= SMboyvsgirl SMnonwhite SMhassped SMhasiep SMengsec SMfrlunch SMmath;
RUN;
DATA SchoolMeans; SET SchoolMeans;
SchoolN=_FREQ_;
DROP _TYPE_ _FREQ_;
LABEL SMboyvsgirl= "SMboyvsgirl: School Mean Boy=0, Girl=1"
SMnonwhite= "SMnonwhite: School Mean White=0, NonWhite=1"
SMhassped= "SMhassped: School Mean 0=No SPED, 1=Special Ed"
SMhasiep= "SMhasiep: School Mean 0=No IEP, 1=Ind Ed Plan"
SMengsec= "SMengsec: School Mean0=English first, 1=English second"
SMfrlunch= "SMfrlunch: School Mean 0=No, 1=Free/Reduced Lunch"
SchoolN= "SchoolN: # Students Contributing Data"
SMmath = "SMmath: School Mean Math";
RUN;
* Merging school means back with individual data;
DATA grade10; MERGE grade10 SchoolMeans; BY schoolID;
IF SchoolN < 31 THEN DELETE;
IF SchoolID=3 THEN DELETE;
RUN;
DATA grade10; SET grade10;
* Selecting cases that are complete for analysis variables;
WHERE NMISS(studentID, schoolID, boyvsgirl, frlunch, engsec, math)=0;
IF SchoolN < 31 THEN DELETE;
IF SchoolID=3 THEN DELETE;
RUN;
* Creating district means for predictors;
PROC SORT DATA=grade10; BY districtID schoolID studentID; RUN;
PROC MEANS NOPRINT N DATA=grade10; BY districtID;
VAR boyvsgirl--frlunch;
OUTPUT OUT=DistrictMeans
MEAN(boyvsgirl nonwhite hassped hasiep engsec frlunch)= DMboyvsgirl DMnonwhite DMhassped DMhasiep DMengsec DMfrlunch;
RUN;
DATA DistrictMeans; SET DistrictMeans;
DistrictN=_FREQ_;
DROP _TYPE_ _FREQ_;
LABEL DMboyvsgirl= "DMboyvsgirl: School Mean Boy=0, Girl=1"
DMnonwhite= "DMnonwhite: School Mean White=0, NonWhite=1"
DMhassped= "DMhassped: School Mean 0=No SPED, 1=Special Ed"
DMhasiep= "DMhasiep: School Mean 0=No IEP, 1=Ind Ed Plan"
DMengsec= "DMengsec: School Mean0=English first, 1=English second"
DMfrlunch= "DMfrlunch: School Mean 0=No, 1=Free/Reduced Lunch"
DistrictN= "DistrictN: # Students Contributing Data";
RUN;
*** Data creation code for example handout;
* Importing data into work library;
%LET example = F:\Example Data\KS Schools;
LIBNAME example "&example.";
DATA grade10; SET grade10;
* Selecting cases that are complete for analysis variables;
WHERE NMISS(studentID, schoolID, boyvsgirl, frlunch, math)=0;
LABEL studentID= "studentID: Student ID number"
schoolID= "schoolID: School ID number"
boyvsgirl= "boyvsgirl: Boy=0, Girl=1"
frlunch= "frlunch: 0=No, 1=Free/Reduced Lunch"
math= "math: Math Test Score Outcome";
KEEP studentID--math;
RUN;
* Getting school means to use as predictors;
PROC SORT DATA=grade10; BY schoolID studentID; RUN;
PROC MEANS NOPRINT N DATA=grade10;
BY schoolID;
VAR boyvsgirl frlunch;
OUTPUT OUT=SchoolMeans
MEAN(boyvsgirl frlunch math)= SMboyvsgirl SMfrlunch SMmath;
RUN;
* Labeling new school mean variables;
DATA SchoolMeans; SET SchoolMeans;
SchoolN = _FREQ_; * Saving N per school;
DROP _TYPE_ _FREQ_; * Dropping unneeded SAS-created variables;
LABEL SMboyvsgirl= "SMboyvsgirl: School Mean Boy=0, Girl=1"
SMfrlunch= "SMfrlunch: School Mean 0=No, 1=Free/Reduced Lunch"
SMmath = "SMmath: School Mean Math"
SchoolN= "SchoolN: # Students Contributing Data";
RUN;
* Merging school means back with individual data;
DATA grade10; MERGE grade10 SchoolMeans; BY schoolID;
* Selecting only schools with data from at least 30 students;
IF SchoolN < 31 THEN DELETE;
RUN;
DATA schoolmeans; SET schoolmeans;
IF SchoolN < 31 THEN DELETE;
IF SchoolID=3 THEN DELETE;
RUN;
* Outputting table of descriptives to rtf document;
ODS RTF FILE="&example.\Descriptive Stats.rtf";
TITLE "Getting means to center predictors with";
ODS RTF CLOSE;
* Centering school mean predictors;
DATA grade10; SET grade10;
SMboyvsgirl50 = SMboyvsgirl - .50; LABEL SMboyvsgirl50= "SMboyvsgirl50: 0=.50";
SMfrlunch30 = SMfrlunch - .30; LABEL SMfrlunch30= "SMfrlunch30: 0=.30)";
SMCmath = math-SMmath; LABEL SMCmath= "SMCmath: School Mean Centered Math)";
SMmath48 = SMmath-48.1185599; LABEL SMmath48= "SMmath48: School Mean Math Grand Mean Centered";
RUN;
PROC MEANS N MEAN STDDEV MIN MAX DATA=grade10;
VAR math SMCmath SMmath boyvsgirl frlunch nonwhite SchoolN;
RUN; TITLE;
*Model 1A:;
PROC GLIMMIX DATA=grade10 METHOD=QUAD;
MODEL frlunch=/SOLUTION DIST=BINOMIAL;
RUN;
*Model 1B:;
PROC GLIMMIX DATA=grade10 METHOD=QUAD;
CLASS schoolID;
MODEL frlunch=/SOLUTION DIST=BINOMIAL;
RANDOM INTERCEPT / SUBJECT=schoolID TYPE=UN G;
COVTEST 'H0: Random Intercept Variance=0' GLM;
RUN;
*Model 2A:;
PROC GLIMMIX DATA=grade10 METHOD=QUAD;
CLASS schoolID;
MODEL frlunch=SMCmath SMmath48/SOLUTION DIST=BINOMIAL;
RANDOM INTERCEPT / SUBJECT=schoolID TYPE=UN G;
COVTEST 'H0: Random Intercept Variance=0' GLM;
RUN;
*Model 2B:;
PROC GLIMMIX DATA=grade10 METHOD=QUAD;
CLASS schoolID;
MODEL frlunch=SMCmath SMmath48/SOLUTION DIST=BINOMIAL;
RANDOM INTERCEPT SMCmath / SUBJECT=schoolID TYPE=UN G GCORR;
COVTEST 'H0: Random Slope Var/Cov = 0' . 0 0;
RUN;
*Model 2C:;
PROC GLIMMIX DATA=grade10 METHOD=QUAD;
CLASS schoolID;
MODEL frlunch=SMCmath SMmath48 SMCmath*SMmath48/SOLUTION DIST=BINOMIAL;
RANDOM INTERCEPT SMCmath / SUBJECT=schoolID TYPE=UN G GCORR;
COVTEST 'H0: Random Slope Var/Cov = 0' . 0 0;
RUN;