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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.