Calcite | Level 5

## Jack Knife for a single variable

While running the below code I am getting an error saying: ERROR: (execution) Matrix has not been set to a value.
Please find below the code and the log.

proc iml;
/* If x is univariate, you can construct a matrix where
each column contains a jackknife sample.
Use for univariate column vector x when n < 20000 */
start JackSampMat(x);
n = nrow(x);
B = j(n-1, n,0);
do i = 1 to n;
B[,i] = remove(x, i)`; /* transpose to column vevtor */
end;
return B;
finish;

/* Input: matrix where each column of X is a bootstrap sample.
Return a row vector of statistics, one for each column. */
start EvalStatMat(x);
return std(x); /* <== Example: return std dev of each sample */
finish;

x = {58,67,74,74,80,89,95,97,98,107}; /* Weight gain (g) for 10 rats */

/* optional: visualize the matrix of jackknife samples */
*M = JackSampMat(x);
*print M[c=("S1":"S10") r=("1":"9")];

/* Jackknife method for univariate data */
/* 1. compute observed statistic */
T = EvalStatMat(x);
/* 2. compute same statistic on each jackknife sample */
T_LOO = EvalStatMat( JackSampMat(x) ); /* LOO = "Leave One Out" */
/* 3. compute mean of the LOO statistics */
T_Avg = mean( T_LOO` ); /* transpose T_LOO */
/* 4 & 5. compute jackknife estimates of bias and standard error */
biasJack = (n-1)*(T_Avg - T);
stdErrJack = sqrt( (n-1)/n * ssq(T_LOO - T_Avg) );
result = T || T_Avg || biasJack || stdErrJack;
print result[c={"Estimate" "Mean Jackknife Estimate" "Bias" "Std Error"}];

Log says:

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
72
73 proc iml;
74 /* If x is univariate, you can construct a matrix where
75 each column contains a jackknife sample.
76 Use for univariate column vector x when n < 20000 */
77 start JackSampMat(x);
78 n = nrow(x);
79 B = j(n-1, n,0);
80 do i = 1 to n;
81 B[,i] = remove(x, i)`;
81 ! /* transpose to column vevtor */
82 end;
83 return B;
84 finish;
NOTE: Module JACKSAMPMAT defined.
85
86 /* Input: matrix where each column of X is a bootstrap sample.
87 Return a row vector of statistics, one for each column. */
88 start EvalStatMat(x);
89 return std(x);
89 ! /* <== Example: return std dev of each sample */
90 finish;
NOTE: Module EVALSTATMAT defined.
91
92 x = {58,67,74,74,80,89,95,97,98,107};
92 ! /* Weight gain (g) for 10 rats */
93
94 /* optional: visualize the matrix of jackknife samples */
95 *M = JackSampMat(x);
96 *print M[c=("S1":"S10") r=("1":"9")];
97
98 /* Jackknife method for univariate data */
99 /* 1. compute observed statistic */
100 T = EvalStatMat(x);
101 /* 2. compute same statistic on each jackknife sample */
102 T_LOO = EvalStatMat( JackSampMat(x) );
102 ! /* LOO = "Leave One Out" */
103 /* 3. compute mean of the LOO statistics */
104 T_Avg = mean( T_LOO` );
104 ! /* transpose T_LOO */
105 /* 4 & 5. compute jackknife estimates of bias and standard error */
106 biasJack = (n-1)*(T_Avg - T);
ERROR: (execution) Matrix has not been set to a value.

operation : - at line 106 column 14
operands : n, *LIT1005

n 0 row 0 col (type ?, size 0)

*LIT1005 1 row 1 col (numeric)

1

statement : ASSIGN at line 106 column 1
107 stdErrJack = sqrt( (n-1)/n * ssq(T_LOO - T_Avg) );
ERROR: (execution) Matrix has not been set to a value.

operation : - at line 107 column 22
operands : n, *LIT1006

n 0 row 0 col (type ?, size 0)

*LIT1006 1 row 1 col (numeric)

1

statement : ASSIGN at line 107 column 1
108 result = T || T_Avg || biasJack || stdErrJack;
109 print result[c={"Estimate" "Mean Jackknife Estimate" "Bias" "Std Error"}];
110
111 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
123

1 ACCEPTED SOLUTION

Accepted Solutions
SAS Super FREQ

## Re: Jack Knife for a single variable

N is only defined locally, that is within the JackSampMat module.  To reference it outside the module you will need to define it globally by adding the GLOBAL clause to the module definition.

proc iml;
/* If x is univariate, you can construct a matrix where
each column contains a jackknife sample.
Use for univariate column vector x when n < 20000 */
start JackSampMat(x) global(n);
n = nrow(x);
B = j(n-1, n,0);
do i = 1 to n;
B[,i] = remove(x, i)`; /* transpose to column vevtor */
end;
return B;
finish;

/* Input: matrix where each column of X is a bootstrap sample.
Return a row vector of statistics, one for each column. */
start EvalStatMat(x);
return std(x); /* <== Example: return std dev of each sample */
finish;
x = {58,67,74,74,80,89,95,97,98,107}; /* Weight gain (g) for 10 rats */

/* optional: visualize the matrix of jackknife samples */
*M = JackSampMat(x);
*print M[c=("S1":"S10") r=("1":"9")];

/* Jackknife method for univariate data */
/* 1. compute observed statistic */
T = EvalStatMat(x);
/* 2. compute same statistic on each jackknife sample */
T_LOO = EvalStatMat( JackSampMat(x) ); /* LOO = "Leave One Out" */
/* 3. compute mean of the LOO statistics */
T_Avg = mean( T_LOO` ); /* transpose T_LOO */
/* 4 & 5. compute jackknife estimates of bias and standard error */
biasJack = (n-1)*(T_Avg - T);
stdErrJack = sqrt( (n-1)/n * ssq(T_LOO - T_Avg) );
result = T || T_Avg || biasJack || stdErrJack;
print result[c={"Estimate" "Mean Jackknife Estimate" "Bias" "Std Error"}];

5 REPLIES 5
SAS Super FREQ

## Re: Jack Knife for a single variable

N is only defined locally, that is within the JackSampMat module.  To reference it outside the module you will need to define it globally by adding the GLOBAL clause to the module definition.

proc iml;
/* If x is univariate, you can construct a matrix where
each column contains a jackknife sample.
Use for univariate column vector x when n < 20000 */
start JackSampMat(x) global(n);
n = nrow(x);
B = j(n-1, n,0);
do i = 1 to n;
B[,i] = remove(x, i)`; /* transpose to column vevtor */
end;
return B;
finish;

/* Input: matrix where each column of X is a bootstrap sample.
Return a row vector of statistics, one for each column. */
start EvalStatMat(x);
return std(x); /* <== Example: return std dev of each sample */
finish;
x = {58,67,74,74,80,89,95,97,98,107}; /* Weight gain (g) for 10 rats */

/* optional: visualize the matrix of jackknife samples */
*M = JackSampMat(x);
*print M[c=("S1":"S10") r=("1":"9")];

/* Jackknife method for univariate data */
/* 1. compute observed statistic */
T = EvalStatMat(x);
/* 2. compute same statistic on each jackknife sample */
T_LOO = EvalStatMat( JackSampMat(x) ); /* LOO = "Leave One Out" */
/* 3. compute mean of the LOO statistics */
T_Avg = mean( T_LOO` ); /* transpose T_LOO */
/* 4 & 5. compute jackknife estimates of bias and standard error */
biasJack = (n-1)*(T_Avg - T);
stdErrJack = sqrt( (n-1)/n * ssq(T_LOO - T_Avg) );
result = T || T_Avg || biasJack || stdErrJack;
print result[c={"Estimate" "Mean Jackknife Estimate" "Bias" "Std Error"}];

Diamond | Level 26

## Re: Jack Knife for a single variable

```106 biasJack = (n-1)*(T_Avg - T);
ERROR: (execution) Matrix has not been set to a value.

operation : - at line 106 column 14
operands : n, *LIT1005

n 0 row 0 col (type ?, size 0)```
It is telling you that N has 0 rows and 0 columns, and so you can't do any math with such a matrix. N is not defined. It doesn't matter that earlier in the program, you defined N within a specific module named JackSampMat, that N is only available within JackSampMat.

But don't bother writing your own Jackknife code. SAS's @Rick_SAS has done this for you at https://blogs.sas.com/content/iml/2017/06/21/jackknife-estimate-standard-error-sas.html

PS: @am557 in the future, please post code by clicking on the "running man" icon, and please post your log by clicking on the </> icon (as I did), this preserves the formatting of the code or log, and makes it more readable. Thanks!
--
Paige Miller
Super User

## Re: Jack Knife for a single variable

You didn't define variable N at global scope .
Better post it at IML forum . @Rick_SAS is there.
Super User

## Re: Jack Knife for a single variable

@Ksharp wrote:
Better post it at IML forum . @Rick_SAS is there.

Moved.

SAS Super FREQ

## Re: Jack Knife for a single variable

The code is copied from "Jackknife estimates in SAS,"

but you did not copy the line

`n = nrow(X);`

From The DO Loop