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

Hello, 

 

I am not looking for all kinds of ways how to dedup macro values when combining them into one. I have code that does that. Wonder if there is a built SAS sys functon of some sort that does that in one. For example

 

%let new100 = %sysdedup(&old1 &old2 .... &old100 &old102);

 

%sysdedup is just my creation that needs to be replaced with a legit one if any. 

Thanks. 

Jia Xin

 

1 ACCEPTED SOLUTION

Accepted Solutions
jimbarbour
Meteorite | Level 14

@fierceanalytics,

 

I see in @ArtC's book "Carpenter's Complete Guide to the SAS Macro Language, Third Edition" that he includes a macro to remove duplicate values (Program 12.4.7, "DistinctList").  I suspect that if Mr. Carpenter isn't aware of an official SAS macro to remove duplicates that there isn't one.  However, if you do find one, please post the name here; it would be a useful macro.

 

Good luck,

 

Jim

View solution in original post

4 REPLIES 4
jimbarbour
Meteorite | Level 14

@fierceanalytics,

 

I see in @ArtC's book "Carpenter's Complete Guide to the SAS Macro Language, Third Edition" that he includes a macro to remove duplicate values (Program 12.4.7, "DistinctList").  I suspect that if Mr. Carpenter isn't aware of an official SAS macro to remove duplicates that there isn't one.  However, if you do find one, please post the name here; it would be a useful macro.

 

Good luck,

 

Jim

fierceanalytics
Obsidian | Level 7

Jim

Found the mentioned code 12.4.7. Sign on SAS support site. Look for the Macro book. Download 67815_example.zip.  The program is 12.4.7, but as of 9-18-2020, the online example copy puts it under Chapter 12 12.4.6.sas. The macro is %macro DistinctList(list).... Not to sound critical, it is kind of long. 

Thanks, 

Jia Xin

Tom
Super User Tom
Super User

It is not clear what you mean, but I don't think there is anything like that.

Sounds like you want way to generate a macro variable that has the distinct set of words that are in the existing value.

%macro dedup(words);
%local return i word ;
%do i=1 %to %sysfunc(countw(&words,%str( )));
  %let word=%scan(&words,&i,%str( ));
  %if not %sysfunc(indexw(&return,&word,%str( ))) %then
    %let return=&return &word
  ;
%end;
&return
%mend dedup;
%put %dedup(a b c a a b d);

If instead you want to give it a list of the names of macro variables and find the set of distinct values of those macro variables you could re-write it this way.

%macro dedupvars(vars);
%local return i var ;
%do i=1 %to %sysfunc(countw(&vars,%str( )));
  %let var=%scan(&vars,&i,%str( ));
  %if not %sysfunc(indexw(&return,&&&var,%str( ))) %then
    %let return=&return &&&var
  ;
%end;
&return
%mend dedupvars;

%let a=a;
%let b=b;
%let c=a;
%put %dedupvars(a b c);
fierceanalytics
Obsidian | Level 7

I have 6 macros that do this. I was just looking for a built in SAS function to simplify. Thank you. Jia

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1340 views
  • 1 like
  • 3 in conversation