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!
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}.
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}.
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.
Use the ELEMENT function:
A = {6,4,3,2,5,1};
B = {2,3};
C = A[ loc(^element(A,B)) ];
That's perfection. Thank you Rick.
Thank you, Rick. That is awesome.
I blogged about this problem: Finding elements in one vector that are not in another vector - The DO Loop
I saw it. I subscribed your blog. Very helpful.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.