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

Hello,

 

I would like to check repeted string in macro varible value.

 

%let chk_string= normal worst normal;

%let chk_string2=normal worst worst;

 

I need put warning message to log when macro valible CHK_SRING have repted string value like below , no need to cehck case sensitivity

 

%put warning chk_string have repted value 'Normal';

%put warning chk_string have repeted value 'worst';

 

can any one help on this one.

 

Thank you,

Rajasekhar.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
%let string= normal normal normal normal normal worst normal normal worst basic;

%macro t;
%let words=;
  %do i=1 %to %sysfunc(countw(&string));
    %let word=%scan(&string,&i);   
    %let s=%sysfunc(findw(&words,&word)); 

    %if  &s = 0  %then  	%let words=&words &word;
     %else  %put ====> Word &word is duplicated. ;  

  %end;
%mend;
%t

View solution in original post

6 REPLIES 6
ChrisNZ
Tourmaline | Level 20

Does the validation need to be case sensitive?

Also it'd be nice if you'd fix the numerous typos in your post including the title and variable name.

 

I would like to check repeted string in macro varible value.

 

%let chk_string= normal worst normal;

%let chk_string2=normal worst worst;

 

I need put warning message to log when macro valible CHK_SRING have repted string value like below.

 

%put warning chk_string have repted value 'Normal';

%put warning chk_string have repeted value 'worst';

 

can any one help on this one.

raja777pharma
Fluorite | Level 6
Hi Chris,

No need validate case sensitive.
Title updated.

Kurt_Bremser
Super User

Do it in a data step, and wrap the code into a macro for convenience:

%let chk_string= normal worst normal;

%macro check_dup(string);
data _null_;
sec_string = "&string."; /* just to set the length */
sec_string = "";
do i = 1 to countw("&string.");
  sub_string = scan("&string.",i);
  if indexw(sec_string,strip(sub_string))
  then put "Warning " sub_string "is duplicated";
  else sec_string = catx(" ",sec_string,sub_string);
end;
run;
%mend;

%check_dup(&chk_string.)
ChrisNZ
Tourmaline | Level 20

This works:

%let string= normal normal normal normal normal worst normal normal worst basic;

%macro t;
  %local string1 i word;
  %let string1=&string;
  %do i=1 %to %sysfunc(countw(%superq(string1)));
    %let word=%scan(&string1,&i);                      
    %let string1=%sysfunc(prxchange(s/\b&word\b//,1,&string1));                     
    %if ^%length(&string1) %then %return;
    %if %index(&string1,&word) %then %do ;                 
       %put ====> Word &word is duplicated.;                                                          
       %let string1=%sysfunc(prxchange(s/\b&word\b//,-1,&string1));
       %let i=1;
     %end;
  %end;
%mend;
%t

====> Word normal is duplicated.
====> Word worst is duplicated.

Also it'd be very nice if you could fix the numerous typos in your post including the variable name.

Ksharp
Super User
%let string= normal normal normal normal normal worst normal normal worst basic;

%macro t;
%let words=;
  %do i=1 %to %sysfunc(countw(&string));
    %let word=%scan(&string,&i);   
    %let s=%sysfunc(findw(&words,&word)); 

    %if  &s = 0  %then  	%let words=&words &word;
     %else  %put ====> Word &word is duplicated. ;  

  %end;
%mend;
%t
Tom
Super User Tom
Super User

@Ksharp Good answer. 

Practice including a description of your approach along with the code. Perhaps something like:

 

Process the list word by word and build a list of the distinct words found to use in testing for duplicates.

 

Remember to create LOCAL macro variables.

%local words i word s;
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
  • 6 replies
  • 1647 views
  • 6 likes
  • 5 in conversation