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

Hello, I am using SAS 9.4

and I am trying to compare two columns of data and make a new column that helps me identify if any data in column A is not in column B, (identified by 1=in column, 0=not in column)

 

Set: Have

 

A            B                 

Bo         Ape                    

Po          Bo                     

Ro         Cool                   

Fo         Lo                        

No         So                        

Mo         Tape                    

So          Yes                    

 

 

set: want

 

A            B                  match (is data in A in B?)

Bo         Ape                    1

Po          Bo                     0

Ro         Cool                   0

Fo         Lo                        0 

No         So                        0

Mo         Tape                    0

So          Yes                    1

 

 

So I am basing the data off of A and finding if any of A's data is found anywhere in B (refer to bolded).

How am I to go about this?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Something like:

 

data work.have;
input A $ B $;
datalines;
Bo Ape 
Po Bo 
Ro Cool 
Fo Lo 
No So 
Mo Tape 
So Yes 
;

proc sql;
   create table work.match as
   select a, b,a in (select distinct b from work.have) as match
   from work.have
   ;
quit;

View solution in original post

2 REPLIES 2
ballardw
Super User

Something like:

 

data work.have;
input A $ B $;
datalines;
Bo Ape 
Po Bo 
Ro Cool 
Fo Lo 
No So 
Mo Tape 
So Yes 
;

proc sql;
   create table work.match as
   select a, b,a in (select distinct b from work.have) as match
   from work.have
   ;
quit;
gamotte
Rhodochrosite | Level 12

Hello,

 

A data step solution :

 

proc sql noprint;
    SELECT nobs INTO :nobs TRIMMED
    FROM dictionary.tables
    WHERE libname="WORK" and memname="HAVE";
quit;

data want;
    array Bvals(&nobs.) $ _TEMPORARY_;
    if _N_=1 then do i=1 to &nobs.;
        set have point=i;
        Bvals(i)=B;
    end;

    set have;
    match=(A in Bvals);
run; 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 7794 views
  • 3 likes
  • 3 in conversation