DATA Step, Macro, Functions and more

Output Element of Array that Meets Condition

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

Output Element of Array that Meets Condition

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.PNGPalindrome Flags Workpalindrome_case_sens.PNGOutputs 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? 


Accepted Solutions
Solution
‎03-12-2018 04:15 PM
Esteemed Advisor
Posts: 5,475

Re: Output Element of Array that Meets Condition

Posted in reply to lhsumdalum

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


All Replies
Valued Guide
Posts: 558

Re: Output Element of Array that Meets Condition

Posted in reply to lhsumdalum

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
Occasional Contributor
Posts: 17

Re: Output Element of Array that Meets Condition

Posted in reply to SuryaKiran
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.
Solution
‎03-12-2018 04:15 PM
Esteemed Advisor
Posts: 5,475

Re: Output Element of Array that Meets Condition

Posted in reply to lhsumdalum

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
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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