I'm working on a macro that calculates cross-sectional correlations for any number of "target" variables at a given timepoint, all correlated with the same reference variables. I had the macro working with hard coded references variables listed out in the WITH statement and then having the macro loop append Timepoint, but I would like to be able to provide just the list of ref vars in the macro call (e.g., ref_list = ref1 ref2 ref3 ref4) and have the macro append the timepoint on to each ref var name for me.
So a macro call for %macro corr (data=d1, item_list = item1 item2 item3 item4, tmpt = T1, ref_list = ref1 ref2 ref3 ref4); would produce the below code for the first iteration:
proc corr data=d1 spearman;
var item1_T1;
with ref1_T1 ref2_T1 ref3_T1 ref4_T1;
run;
I think i need a small loop to go over the elements of ref_list and append the tmpt value to each ref var name and then create a new macro variable, like ref_list_tmpt, to be called at the WITH statement, but haven't been able to figure out how to successfully create this ref_list_tmpt. Like this?
%macro corr ( data = d1
, item_list = item1 item2 item3 item4
, tmpt = T1
, ref_list = ref1 ref2 ref3 ref4 );
%local i with item;
%do i=1 %to %sysfunc(countw(&ref_list));
%let with=&with %scan(&ref_list, &i)_&tmpt.;
%end;
%do i=1 %to %sysfunc(countw(&item_list));
%let item=%scan(&item_list, &i);
proc corr data=&data. spearman;
var &item._&tmpt.;
with &with. ;
run;
%end;
%mend;
Hi - thanks for the response! The variables names are not systematic in either the item_list or the ref_list. We have fixed non-SAS table formats that we need to populate so I was trying to have the produced output (which i do use ODS tables to extract values from and is processed further in currently non-relevant parts of the macro) mirror the individual final tables as much as possible, so if the code is audited it's clear where tabled values come from in output.
I will take a look at the provided links. (the suffix macro does exactly what I needed. THANK YOU!)
Like this?
%macro corr ( data = d1
, item_list = item1 item2 item3 item4
, tmpt = T1
, ref_list = ref1 ref2 ref3 ref4 );
%local i with item;
%do i=1 %to %sysfunc(countw(&ref_list));
%let with=&with %scan(&ref_list, &i)_&tmpt.;
%end;
%do i=1 %to %sysfunc(countw(&item_list));
%let item=%scan(&item_list, &i);
proc corr data=&data. spearman;
var &item._&tmpt.;
with &with. ;
run;
%end;
%mend;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.