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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag

View solution in original post

4 REPLIES 4
Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag
ballardw
Super User

Perhaps you can show how you build that "vector" to begin with? Perhaps that step could be modified to not have duplicates.

RichardDeVen
Barite | Level 11

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
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
  • 1292 views
  • 2 likes
  • 5 in conversation