BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Martin_blaedel
Calcite | Level 5

Hi 

I am trying (and struggling) to understand SAS syntax. I wanted to do a permutation test on the Pearsons correlation coefficient, and copied the below code(from the SAS page) into my editor. 

I thought I had it figured out, but nothing happens, or at least I don't get an output.

Can you tell me why?

the below code will in my understanding only do the permutation test on teh tobacco and cancer variable. But what if I would like to run the same test with on the correlation between alcohol and Tobacco and again on cancer and alcohol?

Can I do that fairly simple?

Thanks a lot 

OPTIONS LINESIZE=95;

DATA   test;
INFILE 'C:\Users\XXXXXXXXXXX.PRN'  FIRSTOBS=2;
INPUT  Cancer Tobacco Alcohol;

PROC PRINT;
RUN;

proc iml;
use test;
   read all var "tobacco" into X;
   read all var "cancer" into Y;
close;
 
start CorrCoeff(X, Y, method="Pearson");
   return ( corr(X||Y, method)[1,2] );   /* CORR returns a matrix; extract the correlation of the pair */
finish;
 
/* permutation test for correlation between two variables */
call randseed(12544);        /* set a random number seed */
B = 10000;                   /* total number of permutations */
PCorr = j(B, 1, .);          /* allocate vector for Pearson correlations */
PCorrObs = CorrCoeff(X, Y);  /* correlation for original data */
PCorr[1] = PCorrObs; 
do i = 2 to B;
   permY = ranperm(Y)`;      /* permute order of Y */
   PCorr[i] = CorrCoeff(X, permY); /* correlation between X and permuted Y */
end;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

I would check the documentation for PROC IML and the HISTOGRAM function.

 

You could also create an output data set from your IML, and then run that through PROC SGPLOT, which will give you many options for controlling the appearance.

--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Are there errors in the log? 


What output are you expecting? You would need a PRINT statement to see your data sets written to the output window or results window; you would need to use the CREATE statement to create a SAS data set with results.

--
Paige Miller
Martin_blaedel
Calcite | Level 5

No errors on the log. 

 

Yeah, that is a good question I think I was expecting perhaps a permutation distribution curve, and/or a table with the p value. 

But I kind of suspect what you say about adding a print statement could solve some of the problem.

 

I tried at some point but perhaps I did it wrong. will try again.  

 

Martin_blaedel
Calcite | Level 5

Hi again

 

Now I have corrected the code and i get my histogram. The code looks like the below now. However, I would like to control the incrementation on the X axis on the permutation distribution graph.

Do you by any chance know how to do that?

 

proc iml;
use test;
   read all var "alcohol" into X;
   read all var "cancer" into Y;
close;
 
start CorrCoeff(X, Y, method="Pearson");
   return ( corr(X||Y, method)[1,2] );   /* CORR returns a matrix; extract the correlation of the pair */
finish;
 
/* permutation test for correlation between two variables */
call randseed(12544);        /* set a random number seed */
B = 10000;                   /* total number of permutations */
PCorr = j(B, 1, .);          /* allocate vector for Pearson correlations */
PCorrObs = CorrCoeff(X, Y);  /* correlation for original data */
PCorr[1] = PCorrObs; 
do i = 2 to B;
   permY = ranperm(Y)`;      /* permute order of Y */
   PCorr[i] = CorrCoeff(X, permY); /* correlation between X and permuted Y */
end;

print PCorrObs;
run;


pVal = sum( abs(PCorr)>= abs(PCorrObs) ) / B;   /* proportion of permutations with extreme corr */
print pVal;
 
title "Permutation Test with H0:corr(alcohol,Cancer)=0";
title2 "Distribution of Pearson Correlation (B=1000)";
refStmt = "refline " + char(PCorrObs) + " / axis=x lineattrs=(color=red);";
call histogram(PCorr) other=refStmt label="Pearson Correlation";

run;
quit;

 

 

PaigeMiller
Diamond | Level 26

I would check the documentation for PROC IML and the HISTOGRAM function.

 

You could also create an output data set from your IML, and then run that through PROC SGPLOT, which will give you many options for controlling the appearance.

--
Paige Miller
Martin_blaedel
Calcite | Level 5

Thanks - those are great ideas. 

I will start by looking into that documentation. 

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 866 views
  • 0 likes
  • 2 in conversation