SAS Training Session 3

Embed Size (px)

Citation preview

  • 7/29/2019 SAS Training Session 3

    1/36

    SAS Training Session 3

    Sun Li

    Centre for Academic Computing

    [email protected]

  • 7/29/2019 SAS Training Session 3

    2/36

    Outline Using arrays in SAS

    Recoding variables, computing new variable, collapsing over variables

    Identify patterns across variables using arrays

    Reshaping data format btw long and wide using arrays

    Introduction to SAS Macro language (7 steps to get started)

    Macro variables & functions Defining and calling Macro definition

    Macro programs for iterative processing

    Applied longitudinal studies

    Survival analysis

    Estimating multilevel models using SAS

    Panel data analysis

  • 7/29/2019 SAS Training Session 3

    3/36

    Using arrays in SAS

    Using arrays in SAS

    Recoding variables Computing new variables

    Collapsing over variables

    ent y patterns across var a es us ng arrays Reshaping data format btw long and wide using arrays

    Note: SAS arrays always work with DO-END loop.

    ARRAY array_name(n) variable_list;

    ;

  • 7/29/2019 SAS Training Session 3

    4/36

    Using arrays in SASDATADATADATADATA famincfamincfamincfaminc;;;;

    inputinputinputinput famidfamidfamidfamid faminc1faminc1faminc1faminc1----faminc12 ;faminc12 ;faminc12 ;faminc12 ;datalinesdatalinesdatalinesdatalines;;;;

    1 3281 3413 3114 2500 2700 3500 31141 3281 3413 3114 2500 2700 3500 31141 3281 3413 3114 2500 2700 3500 31141 3281 3413 3114 2500 2700 3500 3114 ----999 3514 1282 2434 2818999 3514 1282 2434 2818999 3514 1282 2434 2818999 3514 1282 2434 2818

    2 4042 3084 3108 31502 4042 3084 3108 31502 4042 3084 3108 31502 4042 3084 3108 3150 ----999 3100 1531 2914 3819 4124 4274 4471999 3100 1531 2914 3819 4124 4274 4471999 3100 1531 2914 3819 4124 4274 4471999 3100 1531 2914 3819 4124 4274 44713 6015 6123 61133 6015 6123 61133 6015 6123 61133 6015 6123 6113 ----999 6100 6200 6186 6132999 6100 6200 6186 6132999 6100 6200 6186 6132999 6100 6200 6186 6132 ----999 4231 6039 6215999 4231 6039 6215999 4231 6039 6215999 4231 6039 6215;;;;

    ********

    DATADATADATADATA recode_missingrecode_missingrecode_missingrecode_missing;;;;setsetsetset famincfamincfamincfaminc;;;;array inc[12] faminc1array inc[12] faminc1array inc[12] faminc1array inc[12] faminc1 ---- faminc12; * array inc(*)faminc12; * array inc(*)faminc12; * array inc(*)faminc12; * array inc(*) famincfamincfamincfaminc:;:;:;:;do i = 1 to 12; * do i =1 to dim(inc);do i = 1 to 12; * do i =1 to dim(inc);do i = 1 to 12; * do i =1 to dim(inc);do i = 1 to 12; * do i =1 to dim(inc);

    if inc[if inc[if inc[if inc[iiii]=]=]=]=----999 then inc[999 then inc[999 then inc[999 then inc[iiii]=.;]=.;]=.;]=.;

    end;end;end;end;dropdropdropdrop iiii;;;;RUN;RUN;RUN;RUN;

  • 7/29/2019 SAS Training Session 3

    5/36

  • 7/29/2019 SAS Training Session 3

    6/36

    Using arrays in SAS**identify patterns across variables using arrays;**identify patterns across variables using arrays;**identify patterns across variables using arrays;**identify patterns across variables using arrays;DATA pattern;DATA pattern;DATA pattern;DATA pattern;

    setsetsetset famincfamincfamincfaminc;;;;length ever $ 4;length ever $ 4;length ever $ 4;length ever $ 4;

    arrayarrayarrayarray AfamincAfamincAfamincAfaminc(12) faminc1(12) faminc1(12) faminc1(12) faminc1----faminc12; /* existingfaminc12; /* existingfaminc12; /* existingfaminc12; /* existing varsvarsvarsvars */*/*/*/arrayarrayarrayarray AlowincAlowincAlowincAlowinc(2:12) lowinc2(2:12) lowinc2(2:12) lowinc2(2:12) lowinc2----lowinc12; /* newlowinc12; /* newlowinc12; /* newlowinc12; /* new varsvarsvarsvars */*/*/*/do m = 2 to 12;do m = 2 to 12;do m = 2 to 12;do m = 2 to 12;

    ifififif AfamincAfamincAfamincAfaminc[m] < ([m] < ([m] < ([m] < (AfamincAfamincAfamincAfaminc[m[m[m[m----1] / 2) then1] / 2) then1] / 2) then1] / 2) then AlowincAlowincAlowincAlowinc[m] = 1;[m] = 1;[m] = 1;[m] = 1;elseelseelseelse AlowincAlowincAlowincAlowinc[m] = 0;[m] = 0;[m] = 0;[m] = 0;

    sum_lowsum_lowsum_lowsum_low = sum(of= sum(of= sum(of= sum(of lowinclowinclowinclowinc:);:);:);:);ifififif sum_lowsum_lowsum_lowsum_low > 0 then ever='Yes';> 0 then ever='Yes';> 0 then ever='Yes';> 0 then ever='Yes';ifififif sum_lowsum_lowsum_lowsum_low = 0 then ever='No';= 0 then ever='No';= 0 then ever='No';= 0 then ever='No';drop mdrop mdrop mdrop m sum_lowsum_lowsum_lowsum_low;;;;

    RUN;RUN;RUN;RUN;**reshaping from wide to long;**reshaping from wide to long;**reshaping from wide to long;**reshaping from wide to long;DATADATADATADATA long_arraylong_arraylong_arraylong_array;;;;

    setsetsetset famincfamincfamincfaminc;;;;arrayarrayarrayarray AfamincAfamincAfamincAfaminc(12) faminc1(12) faminc1(12) faminc1(12) faminc1 ---- faminc12;faminc12;faminc12;faminc12;

    do month = 1 to 12;do month = 1 to 12;do month = 1 to 12;do month = 1 to 12;famincfamincfamincfaminc ==== AfamincAfamincAfamincAfaminc[month];[month];[month];[month];output;output;output;output;

    end;end;end;end;

    drop faminc1drop faminc1drop faminc1drop faminc1----faminc12;faminc12;faminc12;faminc12;RUN;RUN;RUN;RUN;

  • 7/29/2019 SAS Training Session 3

    7/36

    **reshaping from long to wide;**reshaping from long to wide;**reshaping from long to wide;**reshaping from long to wide;

    PROC SORT data=PROC SORT data=PROC SORT data=PROC SORT data=long_arraylong_arraylong_arraylong_array;;;;bybybyby famidfamidfamidfamid;;;;

    RUN;RUN;RUN;RUN;DATADATADATADATA wide_arraywide_arraywide_arraywide_array;;;;

    setsetsetset long_arraylong_arraylong_arraylong_array;;;;bybybyby famidfamidfamidfamid;;;;retain faminc1retain faminc1retain faminc1retain faminc1----faminc12;faminc12;faminc12;faminc12;

    Using arrays in SAS

    FIRST. : indicates the first observation foreach unique value of by-variable;

    LAST. : indicates the last observation for eachunique value of by-variable.

    ----

    ifififif first.famidfirst.famidfirst.famidfirst.famid then do;then do;then do;then do;do i = 1 to 12;do i = 1 to 12;do i = 1 to 12;do i = 1 to 12;

    AfamincAfamincAfamincAfaminc[[[[iiii] = .;] = .;] = .;] = .;end;end;end;end;

    end;end;end;end;AfamincAfamincAfamincAfaminc(month) =(month) =(month) =(month) = famincfamincfamincfaminc;;;;

    ifififif last.famidlast.famidlast.famidlast.famid then output;then output;then output;then output;drop monthdrop monthdrop monthdrop month famincfamincfamincfaminc iiii;;;;

    RUN;RUN;RUN;RUN;

    Note: When using first.var_nameor last.var_namewe must first sort the data set onthe variable of interest.

    Moreover, in the data step we must always precede first.var_nameor last.var_namewith a by var_namestatement.

  • 7/29/2019 SAS Training Session 3

    8/36

    Introduction to SAS Macro language

    7 steps to get started using SAS Macros

    1. Write your program and make sure it works2. Use Macro variables to facilitate text substitution

    3. Use simple Macro functions

    4. Create s m utand s m etfunction to ass information to and from a

    data step5. Make the program into a Macro definition

    6. Use parameters in the Macro and specify the parameters when the

    Macro is called

    7. Use the iterative SAS language within a Macro definition to executecode iteratively.

    SAS Macro Language Documentation

  • 7/29/2019 SAS Training Session 3

    9/36

    Step 1: Write your program and make sure it works

    Step 2: Use Macro variables to facilitate text substitution

    Macro variables:

    All the key words in statements that are related to macro variables ormacro programs are preceded by percent sign %

    Introduction to SAS Macro language

    To refer macro variables in your program, preface the name of themacro variables with an ampersand sign &

    DATADATADATADATA USPopulationUSPopulationUSPopulationUSPopulation;;;;............

    ;;;;

    PROC MEANS data=PROC MEANS data=PROC MEANS data=PROC MEANS data=USPopulationUSPopulationUSPopulationUSPopulation;;;;varvarvarvar population yearpopulation yearpopulation yearpopulation year yearsqyearsqyearsqyearsq;;;;

    RUN;RUN;RUN;RUN;PROC REG data=PROC REG data=PROC REG data=PROC REG data=USPopulationUSPopulationUSPopulationUSPopulation;;;;model Population=Yearmodel Population=Yearmodel Population=Yearmodel Population=Year YearSqYearSqYearSqYearSq;;;;

    RUN;RUN;RUN;RUN;QUIT;QUIT;QUIT;QUIT;

  • 7/29/2019 SAS Training Session 3

    10/36

    Introduction to SAS Macro language**Step2: use macro variables to facilitate text substitution;**Step2: use macro variables to facilitate text substitution;**Step2: use macro variables to facilitate text substitution;**Step2: use macro variables to facilitate text substitution;

    optionsoptionsoptionsoptions symbolgensymbolgensymbolgensymbolgen;;;;

    *defining a macro variable;*defining a macro variable;*defining a macro variable;*defining a macro variable;%let data=%let data=%let data=%let data=uspopulationuspopulationuspopulationuspopulation;;;;%let%let%let%let indvarindvarindvarindvar=year=year=year=year yearsqyearsqyearsqyearsq;;;;

    *using a macro variable;*using a macro variable;*using a macro variable;*using a macro variable;

    Define a macro variable by using %let

    statement;Dispaly macro variable values as text in theSAS log by using %put statement;

    ou eou eou eou e quo esquo esquo esquo es vs s ng evs s ng evs s ng evs s ng e quo esquo esquo esquo es;;;;

    title "the date is &sysdate9 and today is &title "the date is &sysdate9 and today is &title "the date is &sysdate9 and today is &title "the date is &sysdate9 and today is &sysdaysysdaysysdaysysday";";";";title2 'the date is &sysdate9 and today is &title2 'the date is &sysdate9 and today is &title2 'the date is &sysdate9 and today is &title2 'the date is &sysdate9 and today is &sysdaysysdaysysdaysysday';';';';PROC MEANS data=&data;PROC MEANS data=&data;PROC MEANS data=&data;PROC MEANS data=&data;

    varvarvarvar population &population &population &population &indvarindvarindvarindvar;;;;RUN;RUN;RUN;RUN;PROC REG data=&data;PROC REG data=&data;PROC REG data=&data;PROC REG data=&data;

    model Population=&model Population=&model Population=&model Population=&indvarindvarindvarindvar;;;;RUN;RUN;RUN;RUN;QUIT;QUIT;QUIT;QUIT;

    *displaying text in log;*displaying text in log;*displaying text in log;*displaying text in log;%put &sysdate9 is the date on which you invoked SAS.;%put &sysdate9 is the date on which you invoked SAS.;%put &sysdate9 is the date on which you invoked SAS.;%put &sysdate9 is the date on which you invoked SAS.;

    *displaying SAS system macro variables;*displaying SAS system macro variables;*displaying SAS system macro variables;*displaying SAS system macro variables;%put _automatic_;%put _automatic_;%put _automatic_;%put _automatic_;

  • 7/29/2019 SAS Training Session 3

    11/36

    Introduction to SAS Macro language

    There are many functions that are related to macro variables. They include

    string functions, evaluation functions and others.

    Step 3: Use simple Macro functions

    **Step3: use simple Macro functions;**Step3: use simple Macro functions;**Step3: use simple Macro functions;**Step3: use simple Macro functions;

    %let k = 1;%let k = 1;%let k = 1;%let k = 1;%let tot = &k + 1;%let tot = &k + 1;%let tot = &k + 1;%let tot = &k + 1;%put &tot;%put &tot;%put &tot;%put &tot;

    %let tot = %%let tot = %%let tot = %%let tot = %evalevalevaleval(&k + 1);(&k + 1);(&k + 1);(&k + 1);%put &tot;%put &tot;%put &tot;%put &tot;

    %put;%put;%put;%put;

    **%**%**%**%evalevalevaleval is only for integer evaluation;is only for integer evaluation;is only for integer evaluation;is only for integer evaluation;

    %let tot = %%let tot = %%let tot = %%let tot = %evalevalevaleval(&k + 1.234);(&k + 1.234);(&k + 1.234);(&k + 1.234);%let tot = %sysevalf(&k + 1.234);%let tot = %sysevalf(&k + 1.234);%let tot = %sysevalf(&k + 1.234);%let tot = %sysevalf(&k + 1.234);%put &tot;%put &tot;%put &tot;%put &tot;%put;%put;%put;%put;

  • 7/29/2019 SAS Training Session 3

    12/36

    Introduction to SAS Macro language

    Step 4: Create symputand symgetfunction to pass information to and from a

    data step

    CALL SYMPUT (new_macro_variable, value_in_string_format)

    SYMGET (macro_variable')

    Note: that the macro variable here has to be in single quotes.

    Step 5: Make the program into a Macro definitionStep 6: Use parameters in the Macro and specify the parameters when the

    Macro is called

    Start the macro definition with %MACRO macro_name;

    End the macro with %MEND macro_name;

    To invoke the macro definition, use %macro_name

    Note: there is no semicolon at the end of macro definition when the macro is called.

  • 7/29/2019 SAS Training Session 3

    13/36

    Introduction to SAS Macro language*Step4, 5 and Step 6;*Step4, 5 and Step 6;*Step4, 5 and Step 6;*Step4, 5 and Step 6;

    %macro%macro%macro%macro mexamplemexamplemexamplemexample(data,(data,(data,(data, indvarindvarindvarindvar););););

    PROC MEANS data=&data;PROC MEANS data=&data;PROC MEANS data=&data;PROC MEANS data=&data;varvarvarvar population &population &population &population &indvarindvarindvarindvar;;;;output out=stats mean=output out=stats mean=output out=stats mean=output out=stats mean=avgavgavgavg;;;;

    RUN;RUN;RUN;RUN;PROC PRINT data=stats; RUN;PROC PRINT data=stats; RUN;PROC PRINT data=stats; RUN;PROC PRINT data=stats; RUN;

    DATA _null_;DATA _null_;DATA _null_;DATA _null_;set stats;set stats;set stats;set stats;dtdtdtdt=put(today(), mmddyy10.);=put(today(), mmddyy10.);=put(today(), mmddyy10.);=put(today(), mmddyy10.);callcallcallcall symputsymputsymputsymput('date',('date',('date',('date', dtdtdtdt););););callcallcallcall symputsymputsymputsymput('average', put(avg,7.2));('average', put(avg,7.2));('average', put(avg,7.2));('average', put(avg,7.2));

    RUN;RUN;RUN;RUN;

    DATADATADATADATA new&datanew&datanew&datanew&data.;.;.;.;set &data;set &data;set &data;set &data;avgavgavgavg====symgetsymgetsymgetsymget('average')+0;('average')+0;('average')+0;('average')+0;

    RUN;RUN;RUN;RUN;PROC PRINT data=PROC PRINT data=PROC PRINT data=PROC PRINT data=new&datanew&datanew&datanew&data; RUN;; RUN;; RUN;; RUN;

    %mend;%mend;%mend;%mend;

    %%%%mexamplemexamplemexamplemexample((((uspopulation,yearuspopulation,yearuspopulation,yearuspopulation,year yearsqyearsqyearsqyearsq))))

  • 7/29/2019 SAS Training Session 3

    14/36

    Introduction to SAS Macro languageStep 7: Use the iterative SAS language within a Macro definition to execute

    code iteratively

    DATA file1 file2 file3 file4;DATA file1 file2 file3 file4;DATA file1 file2 file3 file4;DATA file1 file2 file3 file4;input a @@;input a @@;input a @@;input a @@;if _n_

  • 7/29/2019 SAS Training Session 3

    15/36

    Longitudinal studies: Studies in which individuals are measuredrepeatedly over time.

    Most commonly used longitudinal analysis models:

    Survival analysis models for studying event occurrence from well-defined

    Applied longitudinal data analysis

    .

    Multilevel models for studying individual change systematic change overtime. Its outcome data is longitudinal continuous data.

    Panel data analysis models for studying cross-sectional time series data changes within subjects over time & difference btw subjects.

    Reasons of using sophisticated models for longitudinal data:

    The repeated observations are usually (positively) correlated.

    Time-varying predictors

  • 7/29/2019 SAS Training Session 3

    16/36

    Recommended reading:

    Applied Survival Analysis by Hosmer and Lemeshow Survival Analysis: Techniques for Censored and Truncated Data by Klein

    and Moeschberger

    Survival data: time to event data

    Survival analysis in SAS PROC PHREG

    Reason of using survival model:

    The distribution of survival data tends to be positively skewed and notlikely to be normal distribution and it may not be possible to find atransformation.

    Time-varying covariates could not be handled.

    In addition, some duration is censored. (censored obs - right truncation,left truncation, right censoring and left censoring)

  • 7/29/2019 SAS Training Session 3

    17/36

    Survival analysis in SAS PROC PHREG

    Survival Model

    Survival function:

    )(1)()( tFtTPtS ==

    ))(log(

    )( tSdtfazar unc on:

    )( dttS

    ))(exp()( tHtS =

    function.hazardcumulativeis)(tH

  • 7/29/2019 SAS Training Session 3

    18/36

    Kaplan-Meier Estimator:

    =ttj j

    j

    jndtS

    )(|

    )1()()( j

    t

    The number of individuals who experiencethe event at time

    The number of individuals who have notyet experienced the event at time )( jt

    )()2()1( .... nttt

    ==>=

    )(log))(log(

    )()()exp()()(

    0

    )exp(

    00

    is the baseline hazard function.)(0 th

    ))(exp( jiT

    xx is the hazard ratio (HR) or incident rate ratio.

  • 7/29/2019 SAS Training Session 3

    19/36

    Example data: telco.csv

    Variable name Variable information

    age Age in yearsmarital Marital status 0=unmarried 1=married

    address Years in current address

    income Household income in thousands

    Survival analysis in SAS PROC PHREG

    ed Level of educations

    1= didnt complete high school 2= high school degree

    3= college degree 4= undergraduate 5= postgraduate

    employ Years with current employer

    reside Number of people in household

    gender Gender 0=male 1=female

    tenure Months with service

    churn Churn within last month

    0 = No 1=Yes

    custcat Customer categories

    1= basic service 2= E-service 3= plus service 4=total service

  • 7/29/2019 SAS Training Session 3

    20/36

    Survival analysis in SAS PROC PHREG

    **step1: import data into working library;**step1: import data into working library;**step1: import data into working library;**step1: import data into working library;**step2: exploring the data**step2: exploring the data**step2: exploring the data**step2: exploring the data ---- univariateunivariateunivariateunivariate Analyses;Analyses;Analyses;Analyses;

    PROC LIFETEST data=sas3.telco plots=(s);PROC LIFETEST data=sas3.telco plots=(s);PROC LIFETEST data=sas3.telco plots=(s);PROC LIFETEST data=sas3.telco plots=(s);time tenure*churn(0);time tenure*churn(0);time tenure*churn(0);time tenure*churn(0);stratastratastratastrata custcatcustcatcustcatcustcat;;;;

    RUN;RUN;RUN;RUN;PROC LIFETEST data=sas3.telco plots=(s);PROC LIFETEST data=sas3.telco plots=(s);PROC LIFETEST data=sas3.telco plots=(s);PROC LIFETEST data=sas3.telco plots=(s);

    PROC LIEFETEST ;

    TIME time_var*cencor_var(list) ;

    STRATA cate orical varlist

    stratastratastratastrata edededed;;;;RUN;RUN;RUN;RUN;PROC LIFETEST data=sas3.telco plots=(s);PROC LIFETEST data=sas3.telco plots=(s);PROC LIFETEST data=sas3.telco plots=(s);PROC LIFETEST data=sas3.telco plots=(s);

    time tenure*churn(0);time tenure*churn(0);time tenure*churn(0);time tenure*churn(0);strata marital;strata marital;strata marital;strata marital;

    RUN;RUN;RUN;RUN;

    PROC LIFETEST data=sas3.telco plots=(s);PROC LIFETEST data=sas3.telco plots=(s);PROC LIFETEST data=sas3.telco plots=(s);PROC LIFETEST data=sas3.telco plots=(s);time tenure*churn(0);time tenure*churn(0);time tenure*churn(0);time tenure*churn(0);strata gender;strata gender;strata gender;strata gender;

    RUN;RUN;RUN;RUN;

    _

    RUN;

    PROC PHREG ;

    MODEL tvar*cvar(list) =predictors;

    TEST var_list;STRATA strata_varlist;

    BASELINE OUT= COVARIATES=;

    RUN;

  • 7/29/2019 SAS Training Session 3

    21/36

  • 7/29/2019 SAS Training Session 3

    22/36

    Survival analysis in SAS PROC PHREG

    **step4: prediction;**step4: prediction;**step4: prediction;**step4: prediction;

    DATA cov_pat;marital = 1;marital = 1;marital = 1;marital = 1;address = 1;address = 1;address = 1;address = 1;

    employ = 3;employ = 3;employ = 3;employ = 3;custcat2 = 0; custcat3 = 1; custcat4 = 0;custcat2 = 0; custcat3 = 1; custcat4 = 0;custcat2 = 0; custcat3 = 1; custcat4 = 0;custcat2 = 0; custcat3 = 1; custcat4 = 0;

    RUN;RUN;RUN;RUN;

    PROC PHREG data=sas3.telco;PROC PHREG data=sas3.telco;PROC PHREG data=sas3.telco;PROC PHREG data=sas3.telco;****

    custcat2=(custcat2=(custcat2=(custcat2=(custcatcustcatcustcatcustcat=2);=2);=2);=2);custcat3=(custcat3=(custcat3=(custcat3=(custcatcustcatcustcatcustcat=3);=3);=3);=3);custcat4=(custcat4=(custcat4=(custcat4=(custcatcustcatcustcatcustcat=4);=4);=4);=4);baseline out=baseline out=baseline out=baseline out=survsurvsurvsurv covariates=covariates=covariates=covariates=cov_patcov_patcov_patcov_pat survival=survival=survival=survival=survsurvsurvsurv //// nomeannomeannomeannomean;;;;

    RUN;RUN;RUN;RUN;

    goptionsgoptionsgoptionsgoptions reset=all;reset=all;reset=all;reset=all;symbolsymbolsymbolsymbol c=red v=triangle h=.8c=red v=triangle h=.8c=red v=triangle h=.8c=red v=triangle h=.8 iiii====stepjllstepjllstepjllstepjll;;;;axis1axis1axis1axis1 label=(a=90 'Survivorship function');label=(a=90 'Survivorship function');label=(a=90 'Survivorship function');label=(a=90 'Survivorship function');PROC GPLOT data=PROC GPLOT data=PROC GPLOT data=PROC GPLOT data=survsurvsurvsurv;;;;

    plotplotplotplot survsurvsurvsurv*tenure=marital /*tenure=marital /*tenure=marital /*tenure=marital / vaxisvaxisvaxisvaxis=axis1;=axis1;=axis1;=axis1;RUN;RUN;RUN;RUN;QUIT;QUIT;QUIT;QUIT;

  • 7/29/2019 SAS Training Session 3

    23/36

    Multilevel modeling in SAS PROC MIXED

    Recommended reading:

    Introduction to Multilevel Modeling by Ita Kreft and Jan de Leeuw

    Multilevel Analysis: An Introduction to Basic and Advanced MultilevelModeling by Tom Snijders and Roel Bosker

    Multilevel Analysis: Techniques and Applications by Joop Hox

    Multilevel data: nesting structured data (example: hsb.sas7bdat)

    Variable name Variable information

    MATHACH student-level math achievement score (outcome variable)

    SES social-economic-status of a student -- student-level

    MEANSES the group mean of SES (school-level)

    SECTOR Indicating if a school is public or catholic (school-level)

    0= public schools 1= catholic schools

  • 7/29/2019 SAS Training Session 3

    24/36

    Multilevel modeling in SAS PROC MIXED

    Nature of nesting:

    One level nesting, two-level nesting, or more!

    Cross nested structures

    Multivariate dependent variable

    :

    Non-linear: PROC NLMIXED (skip)

    Dichotomous: logistic, probit

    Ordinal: logistic

    Multinomial: logisitic

    Count: Poisson, negative binomial

    Censored/limited continuous: Tobit

  • 7/29/2019 SAS Training Session 3

    25/36

    Multilevel modeling in SAS PROC MIXED

    Hierarchical notation:

    jjj

    ijijjjij

    uZ

    rXY

    001000

    10

    ++=

    ++=

    ijijjjijj

    ijjij

    rXuuXZ

    XZY

    +++

    +++=

    1011

    100100

    Mixed model notation:

    jjj 111101

  • 7/29/2019 SAS Training Session 3

    26/36

    Multilevel modeling in SAS PROC MIXED

    PROC MIXED COVTEST ;

    CLASS variables;

    MODEL dep_var = predictors / SOLUTION;RANDOM variables / SUBJECT=var;

    RUN;

    STATEMENT interpretations

    COVTEST produces asymptotic standard errors and Wald Z-tests for the covariance

    parameter estimates.

    CLASS Identifies categorical variables.

    SOLUTION prints the fixed effects estimates in the output.

    RANDOM identifies the elements of the model to be specified as random effects

    SUBJECT Identifies grouping variable

  • 7/29/2019 SAS Training Session 3

    27/36

    Multilevel modeling in SAS PROC MIXED

    PROC SQL;PROC SQL;PROC SQL;PROC SQL;create table hsb2 ascreate table hsb2 ascreate table hsb2 ascreate table hsb2 asselect *, mean(select *, mean(select *, mean(select *, mean(sessessesses) as) as) as) as meansesmeansesmeansesmeanses,,,, sessessesses----mean(mean(mean(mean(sessessesses) as) as) as) as csescsescsescsesfrom sas3.hsbfrom sas3.hsbfrom sas3.hsbfrom sas3.hsb

    group bygroup bygroup bygroup by schoolidschoolidschoolidschoolid;;;;QUIT;QUIT;QUIT;QUIT;*model 1;*model 1;*model 1;*model 1;PROC MIXEDPROC MIXEDPROC MIXEDPROC MIXED covtestcovtestcovtestcovtest data=hsb2;data=hsb2;data=hsb2;data=hsb2;

    classclassclassclass schoolidschoolidschoolidschoolid;;;;

    random intercept /subject=random intercept /subject=random intercept /subject=random intercept /subject=schoolidschoolidschoolidschoolid;;;;RUN;RUN;RUN;RUN;*model 2;*model 2;*model 2;*model 2;PROC MIXEDPROC MIXEDPROC MIXEDPROC MIXED covtestcovtestcovtestcovtest data=hsb2;data=hsb2;data=hsb2;data=hsb2;

    classclassclassclass schoolidschoolidschoolidschoolid;;;;modelmodelmodelmodel mathachmathachmathachmathach==== meansesmeansesmeansesmeanses sector /solutionsector /solutionsector /solutionsector /solution ddfmddfmddfmddfm====bwbwbwbw notestnotestnotestnotest;;;;random intercept /subject=random intercept /subject=random intercept /subject=random intercept /subject=schoolidschoolidschoolidschoolid;;;;

    RUN;RUN;RUN;RUN;*model 3;*model 3;*model 3;*model 3;PROC MIXEDPROC MIXEDPROC MIXEDPROC MIXED covtestcovtestcovtestcovtest data=hsb2;data=hsb2;data=hsb2;data=hsb2;

    classclassclassclass schoolidschoolidschoolidschoolid;;;;modelmodelmodelmodel mathachmathachmathachmathach==== meansesmeansesmeansesmeanses sectorsectorsectorsector csescsescsescses /solution/solution/solution/solution ddfmddfmddfmddfm====bwbwbwbw notestnotestnotestnotest;;;;random interceptrandom interceptrandom interceptrandom intercept csescsescsescses /subject=/subject=/subject=/subject=schoolidschoolidschoolidschoolid;;;;

    RUN;RUN;RUN;RUN;

  • 7/29/2019 SAS Training Session 3

    28/36

    Multilevel modeling in SAS PROC MIXED

    *final model;*final model;*final model;*final model;PROCPROCPROCPROC MIXEDMIXEDMIXEDMIXED covtestcovtestcovtestcovtest data=hsb2;data=hsb2;data=hsb2;data=hsb2;

    classclassclassclass schoolidschoolidschoolidschoolid;;;;modelmodelmodelmodel mathachmathachmathachmathach==== meansesmeansesmeansesmeanses sectorsectorsectorsector csescsescsescses meansesmeansesmeansesmeanses****csescsescsescses sector*sector*sector*sector*csescsescsescses

    /solution/solution/solution/solution ddfmddfmddfmddfm====bwbwbwbw notestnotestnotestnotest;;;;random interceptrandom interceptrandom interceptrandom intercept csescsescsescses /subject=/subject=/subject=/subject=schoolidschoolidschoolidschoolid;;;;

    RUNRUNRUNRUN;;;;

    PROC UNIVARIATE data=hsb2;PROC UNIVARIATE data=hsb2;PROC UNIVARIATE data=hsb2;PROC UNIVARIATE data=hsb2;

    RUN;RUN;RUN;RUN;

    DATADATADATADATA toplottoplottoplottoplot;;;;set hsb2;set hsb2;set hsb2;set hsb2;ifififif meansesmeansesmeansesmeanses=0.327 then do;ms=0.327;ms=0.327;ms=0.327;ms=0.327;strata="strata="strata="strata="HigHigHigHig"; end;"; end;"; end;"; end;

    else do;else do;else do;else do;ms=0.032; strata="Med" ; end;ms=0.032; strata="Med" ; end;ms=0.032; strata="Med" ; end;ms=0.032; strata="Med" ; end;

    predicted=12.1282+5.3367*ms+1.2245*sector+2.9407*cses+1.0345*ms*cpredicted=12.1282+5.3367*ms+1.2245*sector+2.9407*cses+1.0345*ms*cpredicted=12.1282+5.3367*ms+1.2245*sector+2.9407*cses+1.0345*ms*cpredicted=12.1282+5.3367*ms+1.2245*sector+2.9407*cses+1.0345*ms*c

    sessessesses----1.6388*sector*1.6388*sector*1.6388*sector*1.6388*sector*csescsescsescses;;;;RUN;RUN;RUN;RUN;

  • 7/29/2019 SAS Training Session 3

    29/36

    Multilevel modeling in SAS PROC MIXED

    goptionsgoptionsgoptionsgoptions reset=all;reset=all;reset=all;reset=all;

    symbol1 v=nonesymbol1 v=nonesymbol1 v=nonesymbol1 v=none iiii=join c=red ;=join c=red ;=join c=red ;=join c=red ;symbol2 v=nonesymbol2 v=nonesymbol2 v=nonesymbol2 v=none iiii=join c=blue ;=join c=blue ;=join c=blue ;=join c=blue ;axis1 order=(axis1 order=(axis1 order=(axis1 order=(----4 to 3 by 1) minor=none label=("Group Centered SES");4 to 3 by 1) minor=none label=("Group Centered SES");4 to 3 by 1) minor=none label=("Group Centered SES");4 to 3 by 1) minor=none label=("Group Centered SES");axis2 order=(0 to 22 by 2) minor=none label=(a=90 "Math Achievementaxis2 order=(0 to 22 by 2) minor=none label=(a=90 "Math Achievementaxis2 order=(0 to 22 by 2) minor=none label=(a=90 "Math Achievementaxis2 order=(0 to 22 by 2) minor=none label=(a=90 "Math AchievementScore");Score");Score");Score");

    a a =a a =a a =a a = op oop oop oop o ;;;;

    by strata;by strata;by strata;by strata;plot predicted*plot predicted*plot predicted*plot predicted*csescsescsescses = sector /= sector /= sector /= sector / vaxisvaxisvaxisvaxis = axis2= axis2= axis2= axis2 haxishaxishaxishaxis = axis1;= axis1;= axis1;= axis1;

    RUN;RUN;RUN;RUN;QUIT;QUIT;QUIT;QUIT;

  • 7/29/2019 SAS Training Session 3

    30/36

    Panel data analysis in SAS PROC TSCSREG

    Variables

    Cases(nt) x1 x2 x3 xj

    11 . . . .

    12 . . . .

    . . . .

    1t . . . .

    21 . . . .

    22 . . . .

    . . . .

    2t . . . .

    31 . . . .

    32 . . . .

    . . . .

    3t . . . .

    .

    .

    .

    .

    .

    .

    .

    .

    nt . . . .

  • 7/29/2019 SAS Training Session 3

    31/36

    Panel data analysis in SAS PROC TSCSREG

    Panel data:

    also called cross-sectional time series data with multiple cases (people,nations, firms, etc) for two or more time periods.

    Cross sectional information: difference btw subjects, btw subjecteffects.

    Time series: changes within subjects over time, within-subject effects.

    Two effects models:

  • 7/29/2019 SAS Training Session 3

    32/36

    Panel data analysis in SAS PROC TSCSREG

    Panel data analysis models in SAS

    Regression Procedures

    PROC REG

    LSDV1

    LSDV2

    LSDV3

    w/o dummy

    /NOINT

    RESTRICT

    PROC TSCSREG

    Fixed effect (within effect) /FIXONE

    Two-way fixed (within effect) /FIXTWO

    Random effect /RANONE

    Two-way random /RANTWO

    PROC TSCSREG ;

    ID cross_sectional_id_var time_series_id_vari;

    MODEL depedent_var = regressors / options;

    RUN;

  • 7/29/2019 SAS Training Session 3

    33/36

    Panel data analysis in SAS PROC TSCSREG

  • 7/29/2019 SAS Training Session 3

    34/36

    Panel data analysis in SAS PROC TSCSREG

    Example data: The Demand for Liquid Assets (liqassets)

    Variable name Variable information

    State CA,DC,FL,IL,NY,TX,WA

    year 1949 - 1959

    d Per Capita Demand Deposits

    t Per Capita Time Deposits

    s Per Capita S & L Association Shares

    y Permanent Per Capita Personal Income

    rd Service Charge on Demand Deposits

    rt Interest on Time Deposits

    rs Interest on S & L Association Shares

  • 7/29/2019 SAS Training Session 3

    35/36

  • 7/29/2019 SAS Training Session 3

    36/36

    Thanks!

    CAC statistical WIKI page:http://research2.smu.edu.sg/CAC/StatisticalComputing/Wiki/SAS.aspx

    Statistical consultation service: [email protected]