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

Hi,

 

Iam looking for check two tables and check all the columns and produce the reuslt

 

 

Example :

 

data table1;
input col1 $ col2 $ col3 $ col4 $ col5 $;
cards;
value11 value12 value13 value14 value15
value21 value22 value23 value24 value25
value31 value32 value33 value34 value35
value41 value42 value43 value44 value45
value51 value52 value53 value54 value55
;
run;
 
 
data table2;
input col1 $ col2 $ col3 $ col4 $ col5 $;
cards;
value11 value12 value13 value14 value15
value21 value22 value23 value24 value25
value31 value32 value33 value34 value35
value41 value42 value00 value44 value45
value51 value52 value53 value00 value55
;
run;
 
 
result iam expecting :
 
col1 col2 col3 col4 col5 result
value11 value12 value13 value14 value15 All_columns_matched
value21 value22 value23 value24 value25 All_columns_matched
value31 value32 value33 value34 value35 All_columns_matched
value41 value42 value00 value44 value45 All_columns_not_matched
value51 value52 value53 value00 value55 All_columns_not_matched
1 ACCEPTED SOLUTION

Accepted Solutions
hashman
Ammonite | Level 13

@mmkr: What @Reeza said. If you just want to compare record by record, proc COMPARE is your friend, for example:

data table1 ;                                                                                                                           
  input (col1-col5) ($) ;                                                                                                               
  cards ;                                                                                                                               
value11 value12 value13 value14 value15                                                                                                 
value21 value22 value23 value24 value25                                                                                                 
value31 value32 value33 value34 value35                                                                                                 
value41 value42 value43 value44 value45                                                                                                 
value51 value52 value53 value54 value55                                                                                                 
;                                                                                                                                       
run;                                                                                                                                    
                                                                                                                                  
                                                                                                                                        
data table2 ;                                                                                                                           
  input (col1-col5) ($) ;                                                                                                               
  cards ;                                                                                                                               
value11 value12 value13 value14 value15                                                                                                 
value21 value22 value23 value24 value25                                                                                                 
value31 value32 value33 value34 value35                                                                                                 
value41 value42 value00 value44 value45                                                                                                 
value51 value52 value53 value00 value55                                                                                                 
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
proc compare noprint base=table1 comp=table2 out=diff ;                                                                                 
run ;                                                                                                                                   
                                                                                                                                        
data want (drop = _:) ;                                                                                                                 
  merge table2 diff (rename=(col1-col5=_c1-_c5)) ;                                                                                      
  if findc (catx (of _c:), "X") then result = "All columns not matched" ;                                                               
  else  result = "All columns matched" ;                                                                                                
run ;                                 

Kind regards

Paul D.

 

View solution in original post

9 REPLIES 9
Reeza
Super User
PROC COMPARE - have you tried that yet? Does it not do what you want?
mmkr
Quartz | Level 8

It's doesn't giving me exact result ..it's actually showing in log result window 

 

I tried to put out= resulttablename but it is displaying only the differences as XX Mark

Reeza
Super User

@mmkr wrote:

It's doesn't giving me exact result ..it's actually showing in log result window 

 

I tried to put out= resulttablename but it is displaying only the differences as XX Mark


It's a good idea to mention things like that in your original post. In generaly, the PROC COMPARE output isn't great, but you can capture the ODS table and filter it as desired. I think @hashman solution should work, if it doesn't I suggest that approach.

 

Here's some instructions and explanations on how to capture output that is shown.
https://blogs.sas.com/content/sastraining/2017/03/31/capturing-output-from-any-procedure-with-an-ods...

hashman
Ammonite | Level 13

@mmkr: It does if you do it correctly.

But if you don't like proc COMPARE for some reason, in this particular case you can do it in a single step using just MERGE:

data table1 ;                                                                                                                           
  input (col1-col5) ($) ;                                                                                                               
  cards ;                                                                                                                               
value11 value12 value13 value14 value15                                                                                                 
value21 value22 value23 value24 value25                                                                                                 
value31 value32 value33 value34 value35                                                                                                 
value41 value42 value43 value44 value45                                                                                                 
value51 value52 value53 value54 value55                                                                                                 
;                                                                                                                                       
run;                                                                                                                                    
                                                                                                                                        
data table2 ;                                                                                                                           
  input (col1-col5) ($) ;                                                                                                               
  cards ;                                                                                                                               
value11 value12 value13 value14 value15                                                                                                 
value21 value22 value23 value24 value25                                                                                                 
value31 value32 value33 value34 value35                                                                                                 
value41 value42 value00 value44 value45                                                                                                 
value51 value52 value53 value00 value55                                                                                                 
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
data want (drop = _:) ;                                                                                                                 
  merge table1 table2 (rename=(col1-col5=_c1-_c5)) ;                                                                                    
  if catx (":", of col:) ne catx (":", of _c:) then result = "All columns not matched" ;                                                
  else  result = "All columns matched" ;                                                                                                
run ;                      

In general, it's more reliable to compare the COL and _C pairwise one by one, lest the CATX delimited should happen to be an endpoint of some value (though this snag can be circumvented). That is:

data want (drop = _:) ;                                                                                                                 
  merge table1 table2 (rename=(col1-col5=_c1-_c5)) ;                                                                                    
  array col col: ;                                                                                                                      
  array _c _c: ;                                                                                                                        
  do _i_ = 1 to dim (col) until (col ne _c) ;                                                                                           
  end ;                                                                                                                                 
  if _i_ <= dim (col) then result = "All columns not matched" ;                                                                         
  else  result = "All columns matched" ;                                                                                                
run ;                              

Kind regards

Paul D. 

hashman
Ammonite | Level 13

@mmkr: What @Reeza said. If you just want to compare record by record, proc COMPARE is your friend, for example:

data table1 ;                                                                                                                           
  input (col1-col5) ($) ;                                                                                                               
  cards ;                                                                                                                               
value11 value12 value13 value14 value15                                                                                                 
value21 value22 value23 value24 value25                                                                                                 
value31 value32 value33 value34 value35                                                                                                 
value41 value42 value43 value44 value45                                                                                                 
value51 value52 value53 value54 value55                                                                                                 
;                                                                                                                                       
run;                                                                                                                                    
                                                                                                                                  
                                                                                                                                        
data table2 ;                                                                                                                           
  input (col1-col5) ($) ;                                                                                                               
  cards ;                                                                                                                               
value11 value12 value13 value14 value15                                                                                                 
value21 value22 value23 value24 value25                                                                                                 
value31 value32 value33 value34 value35                                                                                                 
value41 value42 value00 value44 value45                                                                                                 
value51 value52 value53 value00 value55                                                                                                 
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
proc compare noprint base=table1 comp=table2 out=diff ;                                                                                 
run ;                                                                                                                                   
                                                                                                                                        
data want (drop = _:) ;                                                                                                                 
  merge table2 diff (rename=(col1-col5=_c1-_c5)) ;                                                                                      
  if findc (catx (of _c:), "X") then result = "All columns not matched" ;                                                               
  else  result = "All columns matched" ;                                                                                                
run ;                                 

Kind regards

Paul D.

 

mmkr
Quartz | Level 8

thank you so much 

 

It worked

mmkr
Quartz | Level 8

One followup question 

 

how to write all columns instead of _C  ?

Because all my actual column names are not in some unique ....

can i write some thing like _all_col ? is there any keyword ?

 

 

 

if findc (catx (of _c:), "X")

 

mmkr
Quartz | Level 8

super ! Working fine 

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!
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
  • 9 replies
  • 937 views
  • 5 likes
  • 3 in conversation