BookmarkSubscribeRSS Feed
dustychair
Pyrite | Level 9

Hi all,

I am trying to calculate the correlation between 4 variables (listening scores, writing scores, speaking scores, reading scores) with macro. It only calculates the correlation between the same variable (for example, the correlation between listening score and listening score ) with below macro. Could you help to find out why?

 

Thank you,

 

%macro com(c);

%do i=1 %to 12;

proc corr data=on_cl_&c._onl_gr_&i.;
var &c._scale_score ;
ods output PearsonCorr=corr_&c._gr_&i.;
run;
data corr_&c._gr_&i.;
set corr_&c._gr_&i.;
grade=&i.;
run;
%end;
%mend;
%com (listening);
%com (reading);
%com (speaking);
%com (writing);

1 REPLY 1
PaigeMiller
Diamond | Level 26

You have only one variable listed in the VAR statement of PROC CORR each time the macro runs. SAS can't read your mind and figure out you really want four variables in the PROC CORR statement.

 

You can do a lot of debugging yourself, and identify this problem, if you place the command options mprint; at the beginning of your program, run it again, and look at the LOG and see what code has been generated by your macro.

 

You need all four of your variables in the VAR statement to get a correlation matrix. To get this, you will need to merge four data sets together. Then, here's an example:

 

options mprint;
%macro com(c);
%do i=1 %to 12;
proc corr data=on_cl_onl_gr_&i.;
var 
%do j=1 %to %sysfunc(countw(&c));
   %scan(&c,&j,%str( ))_scale_score
%end;
;   
ods output PearsonCorr=corr_gr_&i.;
run;
data corr_gr_&i.;
set corr_gr_&i.;
grade=&i.;
run;
%end;
%mend;
%com(listening reading speaking writing)

 

You would also benefit from a better data structure. It seems that you have data sets named with one of (listening reading speaking writing) in the data set name, and I would recommend you avoid this. If you combine all four of your variables into one data set, then all 12 (indexed by &i) into a single large data set, you would not need a macro at all.

 

Lastly, it would seem that you haven't followed the first rule of macro writing, that is to create code for one case (like when &i=1) that works WITHOUT macros and without macro variables. Once you have that, you have working template from which you can create a macro. If you can't start with working code without macros and without macro variables, then your macro will never work.

 

 

 

 

--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 616 views
  • 0 likes
  • 2 in conversation