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

Hi experts,

 

 I have a requirement to compare 200+ datasets with other datasets.

 The variable are different for each dataset and i need to compare them one by one and store the results in excel file

The Results I need are 

1) No of observations between both datasets

2) no of Variables

3) any unmatched observations

 

I know we need to sort the datasets before comparing, I am looking for Macro which will do this as manually it takes time and not efficient.

Any idea or solution will be appreciated

 

Thanks,

Vicks

1 ACCEPTED SOLUTION

Accepted Solutions
Sajid01
Meteorite | Level 14

Hello @vickys 
As suggested by @andreas_lds I recommend that you write the core code for comparison.
I am giving the core what I would use.
I amusing sashelp.class as the base, I have created another dataset class2 for comparison

data class2;
set sashelp.class;
if mod(_n_,3) ne 0 then do;
age=age+1;
height=height+1;
weight=weight-1;
end;
run;
proc compare base=sashelp.class compare=class2;
id name;
ods select Compare.CompareSummary;
run;

You will find the details you wanted to  in the results tab as follows


                                                       The COMPARE Procedure                                                        
                                            Comparison of SASHELP.CLASS with WORK.CLASS2                                            
                                                           (Method=EXACT)                                                           
                                                                                                                                    
                                                        Observation Summary                                                         
                                                                                                                                    
                                            Observation      Base  Compare  ID                                                      
                                                                                                                                    
                                            First Obs           1        1  Name=Alfred                                             
                                            First Unequal       1        1  Name=Alfred                                             
                                            Last  Unequal      19       19  Name=William                                            
                                            Last  Obs          19       19  Name=William                                            
                                                                                                                                    
                                  Number of Observations in Common: 19.                                                             
                                  Total Number of Observations Read from SASHELP.CLASS: 19.                                         
                                  Total Number of Observations Read from WORK.CLASS2: 19.                                           
                                                                                                                                    
                                  Number of Observations with Some Compared Variables Unequal: 13.                                  
                                  Number of Observations with All Compared Variables Equal: 6.                                      
                                                                                                                                    
                                                                                                                                    
                                                     Values Comparison Summary                                                      
                                                                                                                                    
                                  Number of Variables Compared with All Observations Equal: 1.                                      
                                  Number of Variables Compared with Some Observations Unequal: 3.                                   
                                  Total Number of Values which Compare Unequal: 39.                                                 
                                  Maximum Difference: 1.                                                                            
                                                                                                                                    
                                                                                                                                    
                                                   Variables with Unequal Values                                                    
                                                                                                                                    
                                                Variable  Type  Len  Ndif   MaxDif                                                  
                                                                                                                                    
                                                Age       NUM     8    13    1.000                                                  
                                                Height    NUM     8    13    1.000                                                  
                                                Weight    NUM     8    13    1.000 

You can output the result to an external file using ods for example to output to a csv file you could do like this


ods csv File=file_name_andpath;
/* include proc compare code here*/
ods csv close;

Please test and let us know if you have any issues.

Once you have tested this successfully, you can include the above in your macrocode.
We can discuss the macro part once you are successful up to this point.

 

 

View solution in original post

2 REPLIES 2
andreas_lds
Jade | Level 19

Before you start to write a macro, write the code necessary to compare two datasets, including the creation of the excel-file. Then you can identify those parts that have to be dynamic easily. As last part you have to create the iteration over the datasets that have to be compared. Having the names / pairs in a dataset seems to be a good starting point for that step.

 

One thing will make the comparison difficult: "The variable are different for each dataset ... ." So, how do you know which variable to compare with?

 

The first two points of your result can be answered by querying sashelp.vtable.

Sajid01
Meteorite | Level 14

Hello @vickys 
As suggested by @andreas_lds I recommend that you write the core code for comparison.
I am giving the core what I would use.
I amusing sashelp.class as the base, I have created another dataset class2 for comparison

data class2;
set sashelp.class;
if mod(_n_,3) ne 0 then do;
age=age+1;
height=height+1;
weight=weight-1;
end;
run;
proc compare base=sashelp.class compare=class2;
id name;
ods select Compare.CompareSummary;
run;

You will find the details you wanted to  in the results tab as follows


                                                       The COMPARE Procedure                                                        
                                            Comparison of SASHELP.CLASS with WORK.CLASS2                                            
                                                           (Method=EXACT)                                                           
                                                                                                                                    
                                                        Observation Summary                                                         
                                                                                                                                    
                                            Observation      Base  Compare  ID                                                      
                                                                                                                                    
                                            First Obs           1        1  Name=Alfred                                             
                                            First Unequal       1        1  Name=Alfred                                             
                                            Last  Unequal      19       19  Name=William                                            
                                            Last  Obs          19       19  Name=William                                            
                                                                                                                                    
                                  Number of Observations in Common: 19.                                                             
                                  Total Number of Observations Read from SASHELP.CLASS: 19.                                         
                                  Total Number of Observations Read from WORK.CLASS2: 19.                                           
                                                                                                                                    
                                  Number of Observations with Some Compared Variables Unequal: 13.                                  
                                  Number of Observations with All Compared Variables Equal: 6.                                      
                                                                                                                                    
                                                                                                                                    
                                                     Values Comparison Summary                                                      
                                                                                                                                    
                                  Number of Variables Compared with All Observations Equal: 1.                                      
                                  Number of Variables Compared with Some Observations Unequal: 3.                                   
                                  Total Number of Values which Compare Unequal: 39.                                                 
                                  Maximum Difference: 1.                                                                            
                                                                                                                                    
                                                                                                                                    
                                                   Variables with Unequal Values                                                    
                                                                                                                                    
                                                Variable  Type  Len  Ndif   MaxDif                                                  
                                                                                                                                    
                                                Age       NUM     8    13    1.000                                                  
                                                Height    NUM     8    13    1.000                                                  
                                                Weight    NUM     8    13    1.000 

You can output the result to an external file using ods for example to output to a csv file you could do like this


ods csv File=file_name_andpath;
/* include proc compare code here*/
ods csv close;

Please test and let us know if you have any issues.

Once you have tested this successfully, you can include the above in your macrocode.
We can discuss the macro part once you are successful up to this point.

 

 

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
  • 1104 views
  • 2 likes
  • 3 in conversation