# How do I get correlations using SAS?

#### connor

##### New Member
Hi,
I know the code for correlation on SAS is proc corr but the output is very hard to decipher. Is there any way I can get SAS to give me a table like below: (ignore the fullstops, the formatting is all wrong if I dont use the fullstops.

......... Item 1.......... Item 2 ........... Item 3
Item 1 correlation 1 correlation 4 correlation 7
Item 2 correlation 2 correlation 5 correlation 8
Item 3 correlation 3 correlation 6 correlation 9

I have about 30 items of data so it would be very useful if I could.

Thank you.

-Connor

#### jrai

##### New Member
The O/P from proc corr might be confusing to you because in addition to the correlations it also gives the p-values & # of observations used to calculate these values. You can use the NOPROB option to suppress the printing of p-values & # of obs. Additionally you can use NOSIMPLE to suppress the printing of the descriptive statistics. If you want a table in work directory use the ods statement.

ods output PearsonCorr=table1;
proc corr noprob nosimple data=;
var ;
run;
ods output close;

This will give you the desired output in work.table1.

#### connor

##### New Member
Wow I had no idea I could export directly to a table, I've been copying the .lst files from the output and opening them in sas enterprise then saving them as .txt files!

Thanks jrai!!!

Just to check, if i wanted to run proc means instead, how do i modify the code?

My guess is:

ods output table1;
proc means data=;
run;
ods output close;

But i get an error on sas.

help?

thanks!

#### jrai

##### New Member
Unfortunately the SAS documentation doesn't give any ODS table names for proc means. You can use the output statement to create the table.

proc means data=;
var;
output out=table1(drop=_type_ _freq_) mean=avg;
run;
The results will be stored in work.table1 & the means will be stored in variable 'avg'. You can change the variable name. It creates 2 additional variables _type_ & _freq_ which if you don't require you can drop as I did in above code. If you want additional statistics then you'll have to create additional variables. Say I want to add # of obs, sum & stddev then,
output out=table1(drop=_type_ _freq_) mean=avg sum=s n=num_of_obs std=s;

Things change if you use a class variable. Output statement creates an observation for each level where level is the combination of classes. Say I used a class variable 'Gender' & 'grade'. Gender has 2 types: M & F; Grade has 4 types: A,B,C,D. I used following class statement:

Therefore, there are 11 levels:
1) _type_=0: mean for all dataset.
2) _type_=1: mean for all M.
3) _type_=1: mean for all F.
4) _type_=2: mean for all M & A.
5) _type_=2: mean for all M & B.
6) _type_=2: mean for all M & C.
7) _type_=2: mean for all M & D.
8) _type_=2: mean for all F & A.
9) _type_=2: mean for all F & B.
10) _type_=2: mean for all F & C.
11) _type_=2: mean for all F & D.

Note that _type_=1 is the rightmost variable in the class statement, _type_=2 is for rightmost 2 variables & so on.
So your output dataset will have 11 observations. If you want just the 8 observations for MA,MB,MC,MD,FA,FB,FC&FD then use the nway option or specify the _types_ number. See both the options in the following codes:

NWAY:

proc means data= nway;
var;
output out=table1(drop=_type_ _freq_) mean=avg;
run;
NWAY automatically calculates the stats for the highest _type_. So you'll have means for ma,mb,...,fd.

_types_= :

proc means data=;
var;
output out=table1(drop=_type_ _freq_ where=(_types_=2)) mean=avg;
run;
This will also give the same result. With this you can customize the types. Say you want the mean of entire dataset then use _type_=0.

There is a third option also, the Types statement:
proc means data=;
var;
output out=table1(drop=_type_ _freq_) mean=avg;
run;

This will also give the same result but it has an advantage that it is resource efficient relative to where & nway options for large datasets.

#### Mean Joe

##### TS Contributor
ods output table1;
proc means data=;
SAS creates specific names for the tables that are all put together in the displayed output. As in proc corr it creates a table named PearsonCorr. You can see the names of tables that proc means creates by
Code:
ods trace on;
proc means; run;
ods trace off; *turn it off right afterward to keep your log from getting too cluttered;
Then go into the log and see the table names.

#### connor

##### New Member
Thank you to all of you!!!

-connor