Programming the statistical procedures from SAS

Little's MCAR Macro

Reply
Highlighted
New Contributor
Posts: 3

Little's MCAR Macro

Hi, 

Could you please provide working macro for Little MCAR Test in SAS. The Macro which is already there , contains stack of errors. 

I am attaching snippet of error and code i have used to run Base Sas.

 

Thanks,

Bharat

Super User
Posts: 11,121

Re: Little's MCAR Macro

When dealing with macros you want to set:

Options MPRINT SYMBOLGEN;

and then run the macro.

Post the LOG with code and the resulting diagnostics into a code box opened using the forum {I} menu icon to preserve plain text formatting.

 

You mentioned something about a "snippet of error" but that wasn't included in the attachment.

You may also need to describe what this is actually supposed to do. I'm not interested in running what a "Little MCAR Test" may require in terms of your data set or calculations.

Did your source for this macro include any information about the data requirements? Did you verify that your data met those requirements?

New Contributor
Posts: 3

Re: Little's MCAR Macro

 

Sorry for missing the attachment. To check whether missingness in dataset is due to MCAR , ran the macro code which was there on the net. I have the data set in the required format to run the macro. I am new to running macros, it would be of great help if you could me on the same. Please find attached data set which is an input data for macro. Thanks in advance. Mcar ErrorMcar Error

%macro mcartest;
/* PROVIDE VALUES FOR THE FOLLOWING MACRO VARIABLES */
%let datafile = "C:\Users\Bharat\Desktop\7037 Project\hbat_missing_new"; * FILE PATH FOR RAW DATA;
%let numvars = 26; * NUMBER OF VARS IN DATA FILE;
%let misscode = .; * SPECIFY MISSING VALUE CODE;
%let varnames = v1_miss v2_miss v3_miss v4_miss v5_miss; * SPECIFY VARIABLE NAMES;
%let seednum = 564321; * SPECIFY RANDOM SEED;
/* DO NOT ALTER THE CODE BELOW */
data one;
infile &datafile ;
input &varnames ;
array m[&numvars] &varnames ; array r[&numvars] r1 - r&numvars ;
do i = 1 to &numvars;
if m[i] = &misscode then m[i] = .;
end; drop i;
do i = 1 to &numvars;
r[i] = 1;
if m[i] = . then r[i] = 0;
21
end; drop i;
proc sort;
by r1-r&numvars;
proc mi data = one seed = &seednum nimpute = 0 noprint;
var &varnames;
em outem = emcov;
proc iml;
use one; read all var {&varnames} into y;
read all var {%do i = 1 %to &numvars; r&i %end;} into r;
use emcov; read all var {&varnames} into em;
mu = em[1,]; sigma = em[2:nrow(em),];
/* ASSIGN AN INDEX VARIABLE DENOTING EACH CASE’S PATTERN */
jcol = j(nrow(y), 1 , 1);
do i = 2 to nrow(y);
rdiff = r[i,] - r[i - 1,];
if max(rdiff) = 0 & min(rdiff) = 0 then jcol[i,] = jcol[i - 1,];
else jcol[i,] = jcol[i - 1,] + 1;
end;
/* NUMBER OF DISTINCT MISSING DATA PATTERNS */
j = max(jcol);
/* PUT THE NUMBER OF CASES IN EACH PATTERN IN A COL VECTOR M */ /*
PUT THE MISSING DATA INDICATORS FOR EACH PATTERN IN A MATRIX RJ */
m = j(j, 1, 0); rj = j(j, ncol(r), 0);
do i = 1 to j;
count = 0;
do k = 1 to nrow(y);
if jcol[k,] = i then do;
count = count + 1;
end;
22
if jcol[k,] = i & count = 1 then rj[i,] = r[k,];
m[i,] = count;
end;
end;
/* COMPUTE D^2 STATISTIC FOR EACH J PATTERN */
d2j = j(j, 1, 0);
do i = 1 to j;
/* OBSERVED VALUES FOR PATTERN J */
yj = y[loc(jcol = i),loc(rj[i,] = 1)];
/* VARIABLE MEANS FOR PATTERN J */
ybarobsj = yj[+,]/nrow(yj);
/* D = P X Pj MATRIX OF INDICATORS (SEE P. 1199) */
Dj = j(ncol(y), rj[i,+], 0);
count = 1; do k = 1 to ncol(rj);
if rj[i,k] = 1 then do;
Dj[k, count] = 1;
count = count + 1;
end;
end;
/* REDUCE EM ESTIMATES TO CONTAIN OBSERVED ELEMENTS */
muobsj = mu * Dj; sigmaobsj = t(Dj) * sigma * Dj;
/* THE CONTRIBUTION TO THE D^2 STATISTIC FOR EACH OF THE J PATTERNS
*/
d2j[i,] = m[i,] * (ybarobsj - muobsj) * inv(sigmaobsj) * t(ybarobsj
- muobsj);
end;
23
/* THE D^2 STATISTIC */
d2 = d2j[+,];
/* DF FOR D^2 */
df = rj[+,+] - ncol(rj); p = 1 - probchi(d2,df);
/* PRINT ANALYSIS RESULTS */
file print; put "Number of Observed Variables = " (ncol(rj)) 3.0;
put "Number of Missing Data Patterns = " (j) 3.0; put; put "Summary
of Missing Data Patterns (0 = Missing, 1 = Observed)"; put; put
"Frequency | Pattern | d2j"; put; do i = 1 to nrow(rj);
put (m[i,]) 6.0 " | " @;
do j = 1 to ncol(rj);
put (rj[i,j]) 2.0 @;
end;
put " | " (d2j[i,]) 8.6;
end; put; put "Sum of the Number of Observed Variables Across
Patterns (Sigma psubj) = " (rj[+,+]) 5.0; put; put "Little’s (1988)
Chi-Square Test of MCAR"; put; put "Chi-Square (d2) = " (d2)
10.3; put "df (Sigma psubj - p) = " (df) 7.0; put "p-value
= " (p) 10.3;
%mend mcartest;
%mcartest;
run;


New Contributor
Posts: 3

Re: Little's MCAR Macro

I wasn't able to upload the data set file , as it threw an error and said data type isn't supported
Super User
Posts: 11,121

Re: Little's MCAR Macro


BharatKoti wrote:
I wasn't able to upload the data set file , as it threw an error and said data type isn't supported

 

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

Super User
Posts: 11,121

Re: Little's MCAR Macro

In the body of the code:

r[i] = 1;
if m[i] = . then r[i] = 0;
21                                  /*<= this is not valid code, likely a line number from somewhere, remove the 21*/
end; drop i;

You also apparently have incomplete data, the error about "too few variables" says that something is expecting more variables than you supplied.

 

Super User
Posts: 19,100

Re: Little's MCAR Macro

Where ever you got that macro from (copy and paste from PDF???) garbled the text somehow, messing up the macro code.

 

Find a better source.

http://www.appliedmissingdata.com/littles-mcar-test.sas

 

It's also old, you may want to look at PROC MI.

https://stats.idre.ucla.edu/sas/seminars/multiple-imputation-in-sas/mi_new_1/

Ask a Question
Discussion stats
  • 6 replies
  • 105 views
  • 0 likes
  • 3 in conversation