/* Generate data */ data random; call streaminit(12345); /* Set the random number generator value */ do i = 1 to 1000; /* Perform a loop from 1 to 1000 */ y = rand("Normal",0,1); /* Draw a random number from a std. normal */ z = y + 2*rand("Normal",0,1); /* Draw from a std. normal and relate the previous value */ output; /* Output to the data set "random" */ end; /* Tell SAS that loop is finished */ drop i; /* Drop the loop index, i */ run; /* Calculate correlation and scatter plots; option "cov" produces covariance matrix */ proc corr data=random cov plots=matrix; run; /* Show how correlation changes as you add more or less dispersion */ proc iml; /* Correlation */ start Correl(A); n = nrow(A); /** assume no missing values **/ C = A - A[:,]; /** center the data **/ cov = (C` * C) / (n-1); /** covariance **/ print cov; stdCol = sqrt(C[##,] / (n-1)); /** std deviation of columns **/ print stdcol; stdC = C / stdCol; return( (stdC` * stdC) / (n-1) ); finish; /* Variable names to be displayed*/ varNames = {"y" "z"}; use random; read all into rand; corr1 = Correl(rand); /* Call the correlation function and output result into "corr1" */ print corr1[c=varnames r=varnames]; /* Variables move in opposite directions */ yz1 = {-5 5}; rand = rand // yz1; corr2 = Correl(rand); print corr2[c=varnames r=varnames]; /* Numerator (Cov) is going down and denomenator (SD) is going up --> corr goes down */ /* Variables move in opposite directions */ yz2 = {-10 10}; rand = rand // yz2; corr3 = Correl(rand); print corr3[c=varnames r=varnames]; /* Numerator (Cov) is going down and denomenator (SD) is going up --> corr goes down */ /* Variables start moving in same direction */ yz3 = {10 10}; rand = rand // yz3; corr4 = Correl(rand); print corr4[c=varnames r=varnames]; /* Numerator (Cov) is going up and denomenator (SD) is going down, but not as fast as the covariance is increasing --> corr goes up */ quit;