Statistical programming, matrix languages, and more

Obtain different values between two vectors in PROC IML

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Obtain different values between two vectors in PROC IML

Hi,

I have two vectors like

A = {6,4,3,2,5,1};

B = {2,3};

I want to get another vector which contains the different elements comparing A and B. So I tried using SETDIF function.

C = setdif(A,B);

which returns the vector C is {1 4 5 6}. But, what I want is the not-sorted vector {6 4 5 1}.

Does anyone know how I could get the vector I want?

Thanks very much!


Accepted Solutions
Solution
‎02-28-2014 09:47 AM
Frequent Contributor
Posts: 130

Re: Obtain different values between two vectors in PROC IML

I am thinking there should be a simpler solution than this:

C = remove( A, loc( ( (repeat(A, 1, nrow(B) )=t(B)) [ ,+] ) > 0) );

But I can't see any.   This works for your example, but note that it will retain any repeated values in A, so A={6,4,3,2,5,1,4} would return {6 4 5 1 4}.

View solution in original post


All Replies
Solution
‎02-28-2014 09:47 AM
Frequent Contributor
Posts: 130

Re: Obtain different values between two vectors in PROC IML

I am thinking there should be a simpler solution than this:

C = remove( A, loc( ( (repeat(A, 1, nrow(B) )=t(B)) [ ,+] ) > 0) );

But I can't see any.   This works for your example, but note that it will retain any repeated values in A, so A={6,4,3,2,5,1,4} would return {6 4 5 1 4}.

Occasional Contributor
Posts: 9

Re: Obtain different values between two vectors in PROC IML

Thank you, IanWakeling. I have searched for hours yesterday before I posted the question here and I didn't find anything simpler than your solution here. Actually I would like to retain repeated values in the vector C so thanks again.

SAS Super FREQ
Posts: 3,406

Re: Obtain different values between two vectors in PROC IML

Use the ELEMENT function:

A = {6,4,3,2,5,1};

B = {2,3};

C = A[ loc(^element(A,B)) ];

See also Testing for equality of sets - The DO Loop

Frequent Contributor
Posts: 130

Re: Obtain different values between two vectors in PROC IML

That's perfection. Thank you Rick.

Occasional Contributor
Posts: 12

Re: Obtain different values between two vectors in PROC IML

Thank you, Rick. That is awesome.

SAS Super FREQ
Posts: 3,406

Re: Obtain different values between two vectors in PROC IML

Occasional Contributor
Posts: 12

Re: Obtain different values between two vectors in PROC IML

I saw it. I subscribed your blog. Very helpful.

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 705 views
  • 3 likes
  • 4 in conversation