New Contributor
Posts: 2

# How do I Identify Palindromes?

[ Edited ]

I've been attempting a programming exercise that I found online, but there are no solutions posted. The prompt is as follows: Given any input dataset, identify any palindromes that exist within the set.

There are two types of palindromes: case sensitive, and case insensitive. I am a recent SAS programmer and am looking to hone my skills through new exercises!

Super User
Posts: 6,934

## Re: How do I Identify Palindromes?

[ Edited ]

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

Super User
Posts: 2,061

## Re: How do I Identify Palindromes?

``````data pal;
input word \$25.;
datalines;
Eye
nicola
Pop
roger
debra
sunil
veena
mirna
Noon
Level
Kayak
Rotator
john
malayalam
mark
steve
keith
naveen
jacob
Brett
Poornima
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;``````
Valued Guide
Posts: 629

## Re: How do I Identify Palindromes?

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.

PROC Star
Posts: 275

## Re: How do I Identify Palindromes?

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;``````
Super User
Posts: 10,850

## Re: How do I Identify Palindromes?

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;``````
Super User
Posts: 10,850

## Re: How do I Identify Palindromes?

If you want case insensitive, put UPCASE() around it.

``````   if upcase(temp)=upcase(strip(reverse(temp))) and length(temp)>4
``````
Community Manager
Posts: 3,462

## Re: How do I Identify Palindromes?

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...

palindrome is a wordphrase, 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.

Discussion stats
• 7 replies
• 468 views
• 5 likes
• 7 in conversation