BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lhsumdalum
Obsidian | Level 7

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: 

Palindrome Flags WorkPalindrome Flags WorkOutputs Entire Row, Want Specific Element that causes Palindrome Flag to = 1Outputs Entire Row, Want Specific Element that causes Palindrome Flag to = 1

 

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? 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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)

 

PG

View solution in original post

3 REPLIES 3
SuryaKiran
Meteorite | Level 14

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;

 

Thanks,
Suryakiran
lhsumdalum
Obsidian | Level 7
So to get the exact element that triggered the palindrome_flag I just need to put (keep = palindrome[i])? I don't believe that will work.
PGStats
Opal | Level 21

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)

 

PG

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1698 views
  • 1 like
  • 3 in conversation