BookmarkSubscribeRSS Feed
lhsumdalum
Obsidian | Level 7

Hi, 

 

I need to identify all palindromes within a directory. I use a proc contents and proc sort to identify the datasets within a directory, like so: 

 

proc contentss data = dPath._all_ out = dFiles (keep = memname);
run; 

proc sort data = dFiles nodupkey; 
by memname;
run;

I want to identify palindromes within this directory. As it stands, the macro parameter would take in a string and reverse it. However, I would like to do this for all rows/columns for any dataset. How would I adapt the following logic to work for all columns/rows in a dataset? 

 

%macro palindrome (parameter = ?); 

%let string = %sysfunc(reverse(%sysfunc(compress([WHAT TO PUT HERE?],,sp);
%let reverse = %sysfunc(compress([WHAT TO PUT HERE?]);

%if %upcase(&string.) = %upcase(&reverse.)  %then %do;
ods output = "/palindrome"
%end; 

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

And who told you that you need to use macro?  All that does is generate Base SAS which is the programming language and Base SAS has this functionality:

data want;
  set have;
  if strip(reverse(string))=strip(string) then palindrome="Yes";
run; 
lhsumdalum
Obsidian | Level 7

Thank you for the reply, I plan to use macros because I need to do this for all datasets within a directory. So, instead of the user inputting the string to check if there is a palindrome, I need that to be done dynamically, i.e. identify any palindromes within a dataset. 

RW9
Diamond | Level 26 RW9
Diamond | Level 26
data _null_;
  set sashelp.vcolumn (where=(libname="WORK"));
  by memname;
  if first.memname then call execute('data '||strip(memname)||'; set '||strip(memname)||';');
  call execute('if strip(reverse('||strip(name)||'))=strip('||strip(name)||') then '||strip(name)||'_palindrome="Yes";');
  if last.memname then call execute(';run;');
run;

Will do the palindrome check for every variable in every dataset in the work library. If you just want one dataset, then update the the where clause to be:

(where=(libname="WORK" and memname="TMP"))

For example only does TMP in WORK library, upcase is important.

 

 

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
  • 3 replies
  • 1694 views
  • 0 likes
  • 2 in conversation