I have data in normal form: key1, key2, value. I'd like to compute the correlation of key2 pairs across key1s. For example if key1 are dates and key2 are people and the values are their test scores, I want to correlate pairwise test scores of people over time. I also want to capture the stat output (p-values, nobs) for each correlation coefficient. The end result should be in normal form: key21, key22, corr, p-value, nobs. For example, each row would contain the correlation of test scores of 2 people (identified by key21 and key22) etc. Since proc corr wants the entities to be correlated in columns, I need to pivot the input data, compute the corr, capture the output, and un-pivot the output. Given that data is often stored in normal form, I was hoping there was an easy way to do all this. I can't find it. Here's what I came up with. Let me know of a better way. * pivot normalized data into a table; proc transpose data=Activity out=ActivityPivot prefix=H; by Key1; var Value2Correlate; id Key2; * Save output, including p-values; * Output contains a var named Variable which holds Key2 values; proc corr data=ActivityPivot ; ods output PearsonCorr = IndividualCorr; * un-pivot correlation output to normal form; proc sort data=IndividualCorr; by Variable; proc transpose data=IndividualCorr out=blah; by Variable; * construct a nice output format: Key21 Key22 (identify the pairwise correlation) Corr, p-value, nobs; data CorrCorr; set blah( rename=(Col1=Value) where=(_NAME_ like 'H%')); Key21 = substr( Variable, 2); Key22 = substr( _NAME_, 2); drop Variable _NAME_; run; data CorrN; set blah( rename=(Col1=Value) where=(_NAME_ like 'NH%')); Key21 = substr( Variable, 2); Key22 = substr( _NAME_, 3); drop Variable _NAME_; run; data CorrP; set blah( rename=(Col1=Value) where=(_NAME_ like 'PH%')); Key21 = substr( Variable, 2); Key22 = substr( _NAME_, 3); drop Variable _NAME_; run; proc sql; create table Here.HolderCorr as select a.Key21, a.Key22, a.Value as Correlation, b.Value as PValue, c.Value as N from CorrCorr a join CorrP b on a.Key21 = b.Key21 and a.Key22 = b.Key22 join CorrN c on a.Key21 = c.Key21 and a.Key22 = c.Key22 order by 1,2; PS: why can't I paste code from a text editor? Ctrl-v didn't work. I had to paste it into the html editor, causing me to lose all line breaks.
... View more