Hi Dr. Wicklin,

I simulated data with 10 samples with known distributions. Now I hope to use the Iman-Conover transformation to simulate correlated data within each SampleId. I tried %macro loop, but seems not working. Could you please give me some suggestions? Thank you in advance! Below are my codes.

Karen Shore

%let n_cont=1; /*number of continuous variables*/;

%let n_class=1; /*number of classification variables*/;
%let n_level_class=3; /*number of levels for each classification variable*/;

%let n_binary=1; /*number of binary variables*/;
%let n_level_binary=2; /*number of levels for each binary variable*/;

%let n=50; /*sample size*/;

%let p=0.05; /*missing rate*/;

%let num_samples=10;

***************************************************************************************;
data dat_overall;

array x{&n_cont} x1-x&n_cont;
array c{&n_class} c1-c&n_class;
array b{&n_binary} b1-b&n_binary;

call streaminit(1234);

/*simulate the model*/
do sampleid=1 to &num_samples;
do i=1 to &n;
do j=1 to &n_cont; /*continuous variables for i_th observation*/;
x{j}=rand("Uniform"); /*uncorrelated uniform*/;
end;

do j=1 to &n_class; /*classification variables for i_th observation*/;
c{j}=ceil(&n_level_class*rand("Uniform")); /*discrete uniform*/;
end;

do j=1 to &n_binary; /*binary variables for i_th observation*/;
b{j}=ceil(&n_level_binary*rand("Uniform")); /*binary*/;
end;
output;
end;
end;
drop i j;

run;

%macro trial;
data
%do k=1 %to &num_samples;
dat&k
%end;;

set dat_overall;

%do k=1 %to &num_samples;
if sampleid=&k then output dat&k;
%end;
run;
%mend;
%trial;

*computes the Iman-Conover transformation was computed*;

%macro try;
proc iml;
load module=(imanconovertransform); /* load the function definition */

/* Step 1: Read in the data */
varnames={'x1' 'c1' 'b1'};
%do k=1 %to &num_samples;
use dat&k; read all var varnames into x;
%end;
close;

/* Step 2: specify target rank correlation */
/* X1 X2 X3 */
c={1.00 -0.02 0.05, /* X1 */
-0.02 1.00 0.02, /* X2 */
0.05 0.02 1.00}; /* X3 */

/* Step 3: Strategically reorder the columns of X.
The new ordering has the specified rank corr (approx)
and preserves the original marginal distributions. */
w=imanconovertransform(x, c);
rankcorr=corr(w, "Spearman");
print rankcorr[format=6.3 r=varnames c=varnames];

/* write new data to a SAS data set */
newvarnames={'new_y' 'new_x1' 'new_c1' 'new_b1'};

%do k=1 %to &num_samples;
create dat&k from w[c=newvarnames];
append from w;
%end;
close;
quit;
%mend try;