Hey guys
I have an array and it contains a few variables. Now there are some contents inside of those variables that are occuring more than once. I want to know at which Position the last occuring value inside those variables is. I know that you can search with whichn but i dont want the program to return the first matching variable but the last one. Is there a way i can do this?
Hi @Gablz,
@Gablz wrote:
I know that you can search with whichn but i dont want the program to return the first matching variable but the last one. Is there a way i can do this?
To make WHICHN return the position of the last occurrence of the value of interest, just create the array with reversed order of variables.
Example:
data have; input a b c d e f; cards; 3 1 4 1 5 9 ;
Task: Determine the position and the name of the variable containing the last 1.
data want(drop=_p);
set have;
array v[*] f e d c b a;
length varname $32;
_p=whichn(1, of v[*]);
if _p then do;
position=dim(v)-_p+1;
varname=vname(v[_p]);
end;
run;
Result:
a b c d e f varname position 3 1 4 1 5 9 d 4
With similar coding effort you could use a DO loop to traverse the array x[*] a--f.
Have you considered using the SCAN() function and searing from right to left? See the example "Example 1: Using the SCAN Function in SAS and CAS" at this page:
Hi @Gablz,
@Gablz wrote:
I know that you can search with whichn but i dont want the program to return the first matching variable but the last one. Is there a way i can do this?
To make WHICHN return the position of the last occurrence of the value of interest, just create the array with reversed order of variables.
Example:
data have; input a b c d e f; cards; 3 1 4 1 5 9 ;
Task: Determine the position and the name of the variable containing the last 1.
data want(drop=_p);
set have;
array v[*] f e d c b a;
length varname $32;
_p=whichn(1, of v[*]);
if _p then do;
position=dim(v)-_p+1;
varname=vname(v[_p]);
end;
run;
Result:
a b c d e f varname position 3 1 4 1 5 9 d 4
With similar coding effort you could use a DO loop to traverse the array x[*] a--f.
It may not be intuitive but if you define an array in reverse order then value associated with whichn from the reverse order array would be the LAST in the first array. Though that might get a bit confusing in actual use.
data example; input v1- v5; array a (*) v1-v5; array z (*) v5-v1; zz = whichn(1,of z(*)); znum = z[whichn(1,of z(*))]; datalines; 2 1 6 3 1 ;
ZZ has the position in the reversed array, 1 and finds the "last" value of 1 in the 5 variables.
So the question might be which is more important to find, the Value of the variable or the position?
You can add your own Whichn using the A array to compare.
@Gablz wrote:
Hey guys
I have an array and it contains a few variables. Now there are some contents inside of those variables that are occuring more than once. I want to know at which Position the last occuring value inside those variables is. I know that you can search with whichn but i dont want the program to return the first matching variable but the last one. Is there a way i can do this?
Just list the variables in the opposite order.
data want;
set have;
first5 = whichn(5, of var1-var5);
last5 = whichn(5, of var5-var1);
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.