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

I'm looking to compare two datasets that have just two variables each(Name & Num). Rather than return a listing of the differences between the datasets, what I'm after is a listing of identical obervations in the two datasets. 

 

DATA ONE;                  
INPUT NAME NUM $ ;         
CARDS;                     
FRED   1234                
WILMA  4567                
BARNEY 8369                
BAMBAM 7894                
;                          
RUN;                       
                           
DATA TWO;                  
INPUT NAME NUM $ ;         
CARDS;                     
FRED   1234                
WILMA  8369                
BETTY  7894                
;                        
RUN;                                 
                                     
PROC SORT DATA=ONE NODUPKEY;         
BY NAME NUM;                         
RUN;                                 
                                     
PROC SORT DATA=TWO NODUPKEY;         
BY NAME NUM;                         
RUN;                                 
                                     
DATA FINAL;                          
PROC COMPARE BASE=ONE COMPARE=TWO;   
RUN;                                 
OUTPUT:                              

I can't seem to find anything that will allow me to report on identical oberservations. 

 

Any suggestions?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

First you'll likely get better results if Name is character.

Try this;

DATA ONE;                  
INPUT NAME $ NUM $ ;         
CARDS;                     
FRED   1234                
WILMA  4567                
BARNEY 8369                
BAMBAM 7894                
;                          
RUN;                       
                           
DATA TWO;                  
INPUT NAME $ NUM $ ;         
CARDS;                     
FRED   1234                
WILMA  8369                
BETTY  7894                
;                        
RUN;                                 
                                     
PROC SORT DATA=ONE NODUPKEY;         
BY NAME NUM;                         
RUN;                                 
                                     
PROC SORT DATA=TWO NODUPKEY;         
BY NAME NUM;                         
RUN;    

data three;
   merge one (in=inone)
         two (in=intwo);
   by name num;
   if inone and intwo;
run; 

View solution in original post

2 REPLIES 2
Haikuo
Onyx | Level 15

Try DUPOUT= option in Proc Sort,

 

data _stack;
set one two;
run;

proc sort data=_stack dupout=_wanted_dup nodupkey;
by _all_;
run;
ballardw
Super User

First you'll likely get better results if Name is character.

Try this;

DATA ONE;                  
INPUT NAME $ NUM $ ;         
CARDS;                     
FRED   1234                
WILMA  4567                
BARNEY 8369                
BAMBAM 7894                
;                          
RUN;                       
                           
DATA TWO;                  
INPUT NAME $ NUM $ ;         
CARDS;                     
FRED   1234                
WILMA  8369                
BETTY  7894                
;                        
RUN;                                 
                                     
PROC SORT DATA=ONE NODUPKEY;         
BY NAME NUM;                         
RUN;                                 
                                     
PROC SORT DATA=TWO NODUPKEY;         
BY NAME NUM;                         
RUN;    

data three;
   merge one (in=inone)
         two (in=intwo);
   by name num;
   if inone and intwo;
run; 

sas-innovate-2024.png

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.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 432 views
  • 0 likes
  • 3 in conversation