Obsidian | Level 7

## Search for last Value in numeric or character array

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?

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: Search for last Value in numeric or character array

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.

8 REPLIES 8
Lapis Lazuli | Level 10

## Re: Search for last Value in numeric or character array

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:

SAS Help Center: SCAN Function

Obsidian | Level 7

## Re: Search for last Value in numeric or character array

Thanks for your proposal. But i understand the SCAN function the following way: The program gives you the value inside of what i define it to look for. But i only want him to give me the position at which the last numeric value is inside of my array. Can i do that too with SCAN and which example of your source (1,2,3,4 or 5) shows that? Im trying to understand what is done there and how i could use it to solve my problem

## Re: Search for last Value in numeric or character array

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.

Obsidian | Level 7

## Re: Search for last Value in numeric or character array

Thanks that works 🙂
Super User

## Re: Search for last Value in numeric or character array

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.

Obsidian | Level 7

Super User

## Re: Search for last Value in numeric or character array

@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;``````
Obsidian | Level 7

## Re: Search for last Value in numeric or character array

Thank you for taking the time to help me 🙂
Discussion stats
• 8 replies
• 1201 views
• 4 likes
• 5 in conversation