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)
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.