My issue is this: I need to identify palindromes for all datasets in a specified directory. I am able to successfully flag the palindromes for case sensitive and case insensitive situations. I would like to output the specific element that is a palindrome to a separate dataset. Currently, I am only able to output the entire row with the palindrome in it.
data palindrome_set (drop = i) palindrome_case_sensitive palindrome_case_insensitive;
set reverse_rows;
array palindrome[*] _all_ ;
do i = 1 to dim(palindrome);
palindrome_cs = (trim(palindrome[i]) eq reverse(trim(palindrome[i])));
/* if palindrome_cs = 1 then output palindrome[i]; WANT TO OUTPUT SPECIFIC ELEMENT, NOT ENTIRE ROW*/
palindrome_cis = (lowcase(trim(palindrome[i])) eq reverse(lowcase(trim(palindrome[i]))));
end;
output palindrome_set;
if palindrome_cs = 1 then output palindrome_case_sensitive; *WANT TO OUTPUT SPECIFIC ELEMENT, NOT ENTIRE ROW;
if palindrome_cis = 1 then output palindrome_case_insensitive; *WANT TO OUTPUT SPECIFIC ELEMENT, NOT ENTIRE ROW;
Here is the output for that code:
Question: How would I be able to output the specific element that causes the flag to be set to 1 and not the entire row?
Revise how the KEEP and OUTPUT statements work. I think this might work :
data palindrome_case_sensitive palindrome_case_insensitive;
set reverse_rows;
array p[*] _all_ ;
do i = 1 to dim(p);
if trim(p[i]) eq reverse(trim(p[i])) then do;
palindrome = p[i];
output palindrome_case_sensitive;
end;
if trim(lowcase(p[i])) eq reverse(lowcase(trim(p[i]))) then do;
palindrome = lowcase(p[i]);
output palindrome_case_insensitive;
end;
end;
keep palindrome;
run;
(untested)
Use KEEP=variable to keep
data palindrome_set (drop = i) palindrome_case_sensitive(keep= your vars) palindrome_case_insensitive(Keep=your vars);
set reverse_rows;
array palindrome[*] _all_ ;
do i = 1 to dim(palindrome);
palindrome_cs = (trim(palindrome[i]) eq reverse(trim(palindrome[i])));
/* if palindrome_cs = 1 then output palindrome[i]; WANT TO OUTPUT SPECIFIC ELEMENT, NOT ENTIRE ROW*/
palindrome_cis = (lowcase(trim(palindrome[i])) eq reverse(lowcase(trim(palindrome[i]))));
end;
output palindrome_set;
if palindrome_cs = 1 then output palindrome_case_sensitive; *WANT TO OUTPUT SPECIFIC ELEMENT, NOT ENTIRE ROW;
if palindrome_cis = 1 then output palindrome_case_insensitive;
Revise how the KEEP and OUTPUT statements work. I think this might work :
data palindrome_case_sensitive palindrome_case_insensitive;
set reverse_rows;
array p[*] _all_ ;
do i = 1 to dim(p);
if trim(p[i]) eq reverse(trim(p[i])) then do;
palindrome = p[i];
output palindrome_case_sensitive;
end;
if trim(lowcase(p[i])) eq reverse(lowcase(trim(p[i]))) then do;
palindrome = lowcase(p[i]);
output palindrome_case_insensitive;
end;
end;
keep palindrome;
run;
(untested)
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.