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

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. 
 
Any help, better approaches to do this, or direction to resources would be greatly appreciated!
1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

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;

 

 

View solution in original post

4 REPLIES 4
Reeza
Super User
Out of curiousity, what do you get out of looping it 1 by 1 rather than just passing all? Do your variables have systemic names as shown?

I would probably suggesting running it once in a massive run and then parsing through the output parts of interest only which you can capture via ODS tables instead.
Otherwise, look up some macro utilities to add a prefix to macro variable lists and one to add suffix.
Prefix macro;
http://www.datasavantconsulting.com/roland/Spectre/utilmacros/prefix.sas
Suffix Macro:
http://www.datasavantconsulting.com/roland/Spectre/utilmacros/suffix.sas

http://www.datasavantconsulting.com/roland/Spectre/maclist2.html#util


proc corr data=d1 spearman;
var item1_T:;
with ref:;
run;
houcr
Calcite | Level 5

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!)

 

 

ChrisNZ
Tourmaline | Level 20

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;

 

 

houcr
Calcite | Level 5
Thanks! this works great with minimal modification needed to existing code
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
  • 4 replies
  • 1478 views
  • 3 likes
  • 3 in conversation