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.
%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
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.
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.)
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.
%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
@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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.