Today I'm going to share a code sample I had to write a few years ago but which still might be useful. The original version did not do very much, it simply enclosed every element of a list between quotes. This article will show you how to generalize it to enclose the elements between nearly anything you might think of.
There were two ways of doing it : a full macro version and a data step version. The full macro version has the only advantage that it can be called several times nearly anywhere, even in a data step, the data step version however, might not function so well if called from a data step or any given SAS procedure but is way more concise and extensible.
The original macro version looked like this :
%macro quoteListElements(list);
%let list=a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
%let i=1;
%let var=%qscan(%quote(&list),&i);
%let list2=;
%do %while ("&var" ne "");
%let list2=&list2%sysfunc(quote(&var));
%let i=%eval(&i+1);
%let var=%qscan(%quote(&list),&i);
%if("&var" ne "") %then %let list2=&list2,;
%end;
%put &list2;
%mend;
The data step version looked like this :
%macro quoteListElements(list);
data _null_;
list = "&list";
call symput("list",cats("'",prxchange("s/,/','/",-1,list),"'"));
run;
%mend;
The data step version is easily extensible, for example, we can easily switch to double quotes if needed :
%macro quoteListElements(list);
data _null_;
list = "&list";
call symput("list",cats('"',prxchange('s/,/","/',-1,list),'"'));
run;
%mend;
Well, this is good but I'm sure you don't see the difference without looking attentively, so this kind of things may lead to errors, let's make this clearer and a little more generic :
%macro quoteListElements(list,start,end);
data _null_;
list = "&list";
call symput("list",cats("&start",prxchange("s/,/&end,&start/",-1,list),"&end"));
run;
%mend;
