BookmarkSubscribeRSS Feed
austinrousevelt
Calcite | Level 5

question?

7 REPLIES 7
Astounding
PROC Star

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

novinosrin
Tourmaline | Level 20
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;
andreas_lds
Jade | Level 19

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.

s_lassen
Meteorite | Level 14

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;
Ksharp
Super User

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;
Ksharp
Super User

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

 

   if upcase(temp)=upcase(strip(reverse(temp))) and length(temp)>4 
ChrisHemedinger
Community Manager

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.

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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