question?
Avoid macros entirely for this problem. Macros just automate a working process. They are of little use unless you can program the result without using macros.
Take a look at three functions: REVERSE, STRIP, and UPCASE.
Make that four functions. You'll also need COMPRESS. (Once you have COMPRESS, you may not need STRIP anymore.)
Hint: this will be a very short program, once you apply the right tools in the right way.
Able was I ere I saw Elba
data pal;
input word $25.;
datalines;
Eye
nicola
Pop
roger
debra
sunil
veena
mirna
Noon
Level
Radar
Kayak
Rotator
john
malayalam
mark
steve
keith
naveen
jacob
Brett
Poornima
Badrinarayanan
Nepun
Parthasarathy
;
/*Below is the logic to check for Palindromes*/
data want;
set pal;
length result $30;
if mod(length(strip(word)),2)=0 then
do;
do i=1 to divide(length(strip(word)),2);
if substr(lowcase(word),i,1) ne substr(lowcase(reverse(strip(word))),i,1) then
do;
result=' Not palindrome';
output;
go to next;
end;
else result='palindrome';
if i=divide(length(strip(word)),2) and result='palindrome' then
output;
end;
end;
else if mod(length(strip(word)),2)=1 then
do;
do i=1 to median(1,length(strip(word)))-1;
if substr(lowcase(word),i,1) ne substr(lowcase(reverse(strip(word))),i,1) then
do;
result='Not palindrome';
output;
go to next;
end;
else result='palindrome';
if i=median(1,length(strip(word)))-1 and result='palindrome' then
output;
end;
end;
next:
run;
The specification is extreme sparse. So first thing to do is creating an input-dataset and a second dataset with the expected solution. This allows you to use proc compare to verify that the generated solution matches the expected result. As soon as you have those two datasets we can think about how to solve this.
I would use PROC FCMP to write a function, e.g.:
proc fcmp outlib=work.funcs.str;
function palindrome(str $,options $);
str=left(str);
options=upcase(options);
if index(options,'I') then
str=upcase(str);
l=length(str);
if l=1 then
return(1);
half=floor(l/2);
if substr(str,1,half)=reverse(substr(str,l-half+1,half)) then
return(1);
return(0);
endsub;
run;
options cmplib=work.funcs;
data test;
input str $;
p=palindrome(str,'');
pi=palindrome(str,'i');
cards;
a
aga
aggA
aggb
;run;
It is more like Computer Science student's question. Do you major CS ?
And I highly suggest to use REVERSE() proposed by @Astounding
data _null_;
x="Able was I ere I saw Elba Hint: this will be a very short program, once you apply the right tools in the right way";
do i=1 to length(x);
do j=i+1 to length(x);
temp=substr(x,i,j-i+1);
if temp=strip(reverse(temp)) and length(temp)>4
/* 4 is the Palindromes's least length, you could change it.*/
then put 'Found: ' temp=;
end;
end;
run;
If you want case insensitive, put UPCASE() around it.
if upcase(temp)=upcase(strip(reverse(temp))) and length(temp)>4
This is fun. I've augmented @Ksharp's approach to add case sensitive/insensitive checks, and also eliminate word spaces/punctuation from affecting the result. After all...
A palindrome is a word, phrase, number or sequence of words that reads the same backwards as forwards. Punctuation and spaces between the words or lettering is allowed.
data _null_;
x="Able was I ere I saw Elba Hint: this will be a very short program, once you apply the right tools in the right way";
/* 4 is the Palindromes's least length, you could change it.*/
min = 4;
do i=1 to length(x);
do j=i+1 to length(x);
original = substr(x,i,j-i+1);
temp=compress(original,,'ka');
temp_ci=upcase(compress(original,,'ka'));
if length(original) >= min then
do;
if temp=strip(reverse(temp)) then
put 'Case sensitive: ' original=;
if temp_ci = strip(reverse(temp_ci)) then
put 'Case insensitive: ' original=;
end;
end;
end;
run;
I'll bet that more improvements are possible.
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.