mpml_wt.sas

mpml_wt.sas — text/x-sas-syntax, 7 kB (7,857 bytes)

File contents

*-----------------------------------------------------------------------------------------------------------;
* PROGRAM	mpml_wt.sas                                                                                 ;
* FUNCTION	A SAS macro to compute weights for multilevel modeling software packages that need weights  ;
*		scaled by the method given for MPLUS in the online document:  Asparouhov, T. (2004),        ;
*		Weighting for unequal probability of selection in multilevel modeling. Mplus Web Notes      ;
*		No. 8.                                                                                      ;
* ESTIMATION TECHNIQUE:  Multilevel Psuedo Maximum Likelihood (MPML)                                        ;
* LIMITATIONS	Weights are scaled for use with two-level models.                                           ;
* REFERENCE:  	Asparouhov, T. (2004). Weighting for unequal probability of selection in multilevel         ;
*		modeling. Mplus Web Notes: No. 8                                                            ;
* NOTE:		Although this reference discusses several methods of scaling the weights, instructions      ;
*		are given for only two methods of scaling the weights that have been implemented in MPLUS   ;
* WRITTEN BY:	Kim Chantala                                                                                ;
* DATE:		December 8, 2004                                                                            ;
* MOD-DATE      12Oct2005: Dan Blanchette                                                                   ;
* MOD-DATE      29Apr2011: Dan Blanchette                                                                   ;
*                made default output data set name be mpml_wt like pwigls does.                             ; 
*                                                                                                           ;
*-----------------------------------------------------------------------------------------------------------;


%macro mpml_wt(input_set=input_set, 
	       psu_id=psu_id, 
	       fsu_id=fsu_id, 
	       psu_wt=psu_wt, 
               fsu_wt=fsu_wt,
	       output_set=mpml_wt, 
	       mpml_wta=mpml_wta,
/******
	       f_wt_std=f_wt_std,
               mean_wt=mean_wt,
               n_sample=n_sample,
******/
               replace=,
               pmeans=0);

 ** preserve these to restore at end **;
 %let s_dsn=&sysdsn.;
 %let s_last=&syslast.;
 %let notes=%sysfunc(getoption(notes));
 %let obs=%sysfunc(getoption(obs));

 %** if libref supplied with dataset name figure out what the libref and dataset name are **;
 %if %index(&input_set.,.) %then %do;
  %let in_lib =%substr(&input_set.,1,%index(&input_set.,.)-1);
  %let decpos =%index(&input_set.,.);
  %let in_dsn =%substr(&input_set.,&decpos. + 1,%length(&input_set.) - &decpos.);
 %end;
 %else %do;
  %let in_lib =WORK;
  %let in_dsn =&input_set.;
 %end;

 %** if libref supplied with dataset name then figure out what the libref and dataset name are **;
 %if %index(&output_set.,.) %then %do;
  %let out_lib =%substr(&output_set.,1,%eval(%index(&output_set.,.)-1));
  %let decpos =%index(&output_set.,.);
  %let out_dsn =%substr(&output_set.,&decpos. + 1,%length(&output_set.) - &decpos.));
 %end;
 %else %do;
  %let out_lib =WORK;
  %let out_dsn =&output_set.;
 %end;


 options obs=MAX;   ** Reason for maximizing it is because user could have
                     * set it lower than the number of variables in the dataset. **;
 options nonotes;   ** Shut off notes while program is running in order to reduce log size. **;


 ************ test a few things first **************;
 *-------------------------------------------------*;
 %* test that psu_wt and fsu_wt variables are numeric **;
 %let done=0;
 data _null_;
  set sashelp.vcolumn end=lastobs;
  where  compress(upcase(libname)) = compress(upcase("&in_lib."))
     and compress(upcase(memname)) = compress(upcase("&in_dsn."))
     and compress(upcase(memtype)) = compress(upcase("data"));
  if  compress(upcase(name)) = compress(upcase("&psu_wt."))  and lowcase(type) = "char" then do;
    put "%upcase(error): MPML_WT macro was not run.  Variable &psu_wt. needs to be a numeric variable. ";
    call symput('done',1);
  end;
  if  compress(upcase(name)) = compress(upcase("&fsu_wt."))  and lowcase(type) = "char" then do;
    put "%upcase(error): MPML_WT macro was not run.  Variable &fsu_wt. needs to be a numeric variable. ";
    call symput('done',1);
  end;

 run;

 %if &done=1 %then %goto done;

 %do i =  1 %to 2;
 %let wt =%scan(&fsu_wt. &psu_wt.,&i,%nrstr( ));
  data _null_;
   set &input_set.;
   where (&wt. = 0);
   if _n_ = 1 then put "WARNING: Your sample weight &wt. contains zero values.";
  run;
 %end;

 %if %length(&replace.) = 0  and
      %cmpres(%upcase("&input_set.")) ^= %cmpres(%upcase("&output_set.")) %then %do;
   %if %sysfunc(exist(&out_lib..&out_dsn.)) %then %do;
     %put %upcase(error): MPML_WT macro was not run.  The output dataset &output_set. already exists.  Use the replace option if you want to replace it.;
     %goto done;
   %end;
 %end;

 %* test that variables do not already exist **;
 %let done=0;
 data _null_;
  set sashelp.vcolumn;
  where  compress(upcase(libname)) = compress(upcase("&in_lib."))
     and compress(upcase(memname)) = compress(upcase("&in_dsn."))
     and compress(upcase(memtype)) = upcase("data")  and
      index(upcase(" &mpml_wta. _f_wt_std _mean_wt _n_sample "),upcase(" "||compress(name)||" "));

  if _n_ = 1 then do;
    call symput('done',1);
    put "%upcase(error): MPML_WT macro was not run.  The following variables are created "
            "in this program so either drop them or rename them: ";
  end;
  put "  " name;

 run;
 %if &done=1 %then %goto done;

 %** test that input datset has observations **;
 %let done=0;
 data _null_;
  set sashelp.vtable;
  where  compress(upcase(libname)) = compress(upcase("&in_lib."))
     and compress(upcase(memname)) = compress(upcase("&in_dsn."))
     and compress(upcase(memtype)) = (upcase("data"));
  if nobs = 0 then do;
   put "ERROR:  MPML_WT macro was not run.  The input data set &input_set. contains "
          "no observations.";
   call symput('done',1);
  end;

 run;
 %if &done = 1 %then %goto done;


 %* now on with the show! *;
 
 proc sort data=&input_set.
            out=&output_set.; 
  by &psu_id.; 
 run;
 
 
 proc univariate data=&output_set. noprint;
  by &psu_id.;
  var &fsu_wt.;
  output out=___tm___  mean=_mean_wt n=_n_sample;
 run;
 
 data &output_set  
   %if &pmeans = 0 %then %do;
    (drop=/* _fsu_wt */  _f_wt_std _mean_wt _n_sample)
   %end;
   ;
  merge &output_set.
        ___tm___;
  by &psu_id.;
 
  _f_wt_std = &fsu_wt. / _mean_wt;
  label _f_wt_std = "Standardized Within-PSU (&fsu_wt./&psu_wt.) level 1 weight";
 
  &mpml_wta. = _f_wt_std * &psu_wt.;
  label &mpml_wta. = "2-level Scaled ML Sampling weight, method A";
 
  label _mean_wt  = "Avg of &fsu_wt. in &psu_id.";
  label _n_sample = "Number of &fsu_id. in &psu_id.";
 
 run;
 
 proc datasets nodetails nolist nowarn;
  delete ___tm___ ;
 run;

 proc sort data=&output_set; by &fsu_id.; run;

 %if &pmeans = 1 %then %do;
   proc means data=&output_set.; 
    var &psu_wt.  &fsu_wt.  &mpml_wta. _f_wt_std  _mean_wt _n_sample;
   run;
   data &output_set  
    set &output_set (drop= /* _fsu_wt */ _f_wt_std _mean_wt _n_sample);
   run;
 %end;

 options notes;  ** turn notes on **;

 ** if got to here then it ran successfully **;
 %put ;
 %put NOTE:  The MPML_WT macro successfully created the data set: &output_set. ;
 %put ;

 options nonotes;  ** turn notes back off **;


 %** goto here when done **;
 %done:;

 ** Make sure that the last dataset created in work is the users dataset. **;
 %let sysdsn=&s_dsn;
 %let syslast=&s_last;

 options obs=&obs. &notes.;  ** Restore options. **;


%mend mpml_wt;

Wink Plone Theme by Quintagroup © 2013.

Personal tools
This is themeComment for Wink theme