Hello
I have a sas macro varaible that get multiple values seaprated by plus sign.
For example
%let Vector=2001+2001+2002+2003+2003+2003+2004;
My question is how to create from this macro variable another macro varaible that have distinct values.
So in this example I should get"
%let NewVector=2001+2002+2003+2004;
Thanks
Jow
you can try something as below
%let Vector=2001+2001+2002+2003+2003+2003+2004;
data have;
var="&vector";
count=countw(var,'+');
do i= 1 to count;
var2=scan(var,i,'+');
output;
end;
run;
proc sql;
select distinct var2 into: vector separated by '+' from have;
quit;
%put &vector;
you can try something as below
%let Vector=2001+2001+2002+2003+2003+2003+2004;
data have;
var="&vector";
count=countw(var,'+');
do i= 1 to count;
var2=scan(var,i,'+');
output;
end;
run;
proc sql;
select distinct var2 into: vector separated by '+' from have;
quit;
%put &vector;
Perhaps you can show how you build that "vector" to begin with? Perhaps that step could be modified to not have duplicates.
That's why you do NOT store data in macro variables. With data in datasets, it's a simple select distinct or proc sort with nodupkey.
A regular expression search and replace can remove repeated adjacent years.
options nosource;
%let years = 2001+2001+2002+2003+2003+2003+2004;
%let rxid = %sysfunc(prxparse(s/((\d+)\+)(\1)+/\1/));
%let times = -1;
%put NOTE: before, &=years;
%syscall prxchange(rxid,times,years);
%syscall prxfree(rxid);
%put NOTE: after, &=years;
%symdel years rxid times;
Log
NOTE: before, YEARS=2001+2001+2002+2003+2003+2003+2004
NOTE: after, YEARS=2001+2002+2003+2004
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.