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

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 411 views
  • 2 likes
  • 5 in conversation