/***************************************************************************************************************** SAS file name: find_value_array.sas File location: __________________________________________________________________________________________________________________ Purpose: To demonstrate how to use the Whichn, Whichc and In Operator to search for a value in an array or collection of variables. Author: Peter Clemmensen Creation Date: 23/03/2020 This program supports the blog post "" on SASnrd.com *****************************************************************************************************************/ /* Iterate values of an array with a loop */ data test; array n {10} (1 5 4 9 7 8 4 5 2 6); do i = 1 to dim (n); if n [i] = 5 then do; found = 1; v = vname(n [i]); leave; end; end; run; /* Use the Whichn and Whichc Functions */ data whichn; array n {10} (1 5 4 9 7 8 4 5 2 6); i = whichn (5, of n [*]); v = vname(n [i]); run; data whichc; array c {10} $ ('a' 'e' 'd' 'i' 'g' 'h' 'd' 'e' 'b' 'f'); i = whichc ('e', of c [*]); v = vname(c [i]); run; /* Use the In Operator */ data test_num; array n {10} (1 5 4 9 7 8 4 5 2 6); if 5 in n then found = 1; /* found = (5 in n); */ run; data test_char; array c {10} $ ('a' 'e' 'd' 'i' 'g' 'h' 'd' 'e' 'b' 'f'); if 'e' in c then found = 1; /* found = ('e' in c); */ run;