I am somewhat new to SAS programming and am attempting to code up a permutation test for a dependent sample test situation using the difference of the observations from two groups. I would like to simplify and generalize my code by developing a macro similar to the `allperm()` function but instead of using the difference values themselves, this macro would output permutations using a negative or positive version of each difference value. For example, let's say the data look like this where four subjects are each measured twice (once in each of two conditions): ``` data one; input sub y1 y2; datalines; 1 244 225 2 255 247 3 253 249 4 253 254 run; ``` Because the permutation test need only use the difference of the observations, we can compute that as follows: ``` data two; set two; d = y1 - y2; run; ``` Then the test could be performed with the following IML code: ``` proc iml; use two; read all var "d" into Y; close; N = 2**nrow(Y); S = j(N, 1, .); i = 1; do a = 1 to 2; do b = 1 to 2; do c = 1 to 2; do d = 1 to 2; P = j(4, 1, .); P[1] = 2 * (a - 1.5) * Y[1]; P[2] = 2 * (b - 1.5) * Y[2]; P[3] = 2 * (c - 1.5) * Y[3]; P[4] = 2 * (d - 1.5) * Y[4]; S[i] = abs(mean(P)); i = i + 1; end; end; end; end; E = 0; do i = 1 to N; if S[i] >= mean(Y) then E = E + 1; end; print(E / N); run; ``` As stated in the introduction, I would like to write a macro to replace the nested do loops similar to what the `allperm()` function does. This function should be generalized in the sense that it would work for a one-dimensional matrix of any length. Here are some example input and desired outputs where the difference values are Y = {19 8 4 -1}; signperm(Y, 1) => {-19 -8 -4 1} /* first iteration produces the "negative" versions of the input */ signperm(Y, 2) => {-19 -8 -4 -1} /* because the later values change sign more rapidly, the second iteration produces the "positive" version of the last value */ signperm(Y, 4) => {-19 -8 4 -1} signperm(Y, 😎 => {-19 8 4 -1} signperm(Y, 16) => {19, 8, 4, -1} /* the last iteration produces all the "positive" versions of the input */ I suspect a recursive macro would be in order, but I keep getting tripped up when writing the macro syntax.
... View more