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

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
FreelanceReinh
Jade | Level 19

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.

View solution in original post

8 REPLIES 8
svh
Lapis Lazuli | Level 10 svh
Lapis Lazuli | Level 10

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

Gablz
Obsidian | Level 7
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
FreelanceReinh
Jade | Level 19

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.

Gablz
Obsidian | Level 7
Thanks that works 🙂
ballardw
Super User

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
Obsidian | Level 7
Thank you for your answer! 🙂
Tom
Super User Tom
Super User

@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;
Gablz
Obsidian | Level 7
Thank you for taking the time to help me 🙂

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 8 replies
  • 1203 views
  • 4 likes
  • 5 in conversation