/****************************************************************** * Name: corr2data * * Function: creating a data set with given correlation matrix * * %corr2data(mydata, corrmat=corr, n=200, full='f', corr='f'); * * corrmat: input matrix * * n: number of observations * * full: specifying if the input matrix is a full matrix * * 'T' for full matrix * 'F' for upper or lower triangular * * corr: specifying if the input matrix is a correlation * matrix or a covariance matrix: * * 'T' for correlation matrix and * * 'F' for covariance matrix * *******************************************************************/ %macro corr2data(outdata, corrmat, n, full='T', corr='T'); proc iml; use &corrmat; read all var _num_ into C; rn = nrow(C); cn = ncol(C); if (cn = rn & %upcase(&full) ="F") then do; do i = 1 to rn; do j = i to cn; if i = j & C[i, j] = . then C[i,j] = 1; if i ^= j & C[i,j]=. then C[i,j]=C[j,i]; end; end; end; if %upcase(&corr) = "F" then do; /*converting the covariance to correlation*/ do i = 1 to rn; do j = 1 to cn; if i ^=j then C[i,j] = C[i, j]/(sqrt(C[i,i])*sqrt(C[j,j])); end; end; do i = 1 to rn; C[i,i] = 1; end; end; if (cn = rn & sum(abs(C-t(C))) =0 & min(eigval(C)) > 0 & max(abs(C)) <= 1) then do; p = root(C); dim = nrow(C); myvar = rannor(J(&n, dim, 0)); do i = 1 to dim; myvar[, i] = myvar[,i]-(sum(myvar[,i])/&n); end; XX = (t(myvar)*myvar)/(&n-1); U = root(inv(XX)); Y = myvar*T(U); T = Y*p; create &outdata from T; append from T; end; else print "Check your input matrix, it is not a correlation matrix nor a covariance matrix."; quit; %mend;