Compare 2 Data Files
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | * This is a general routine for comparing 2 files, for example pre- and post- cleaning; * or data entry by different operators; or partial data delivery compared to final delivery and so on. * For each variable it lists all the differences. * It produces similar output to the stata routine cf3. * cheers Simon /* Compare two files and lists all differences */ /* Simon Freidin 09/05/2006 */ /* Cases are matched by ID */ /* Output is variable by variable. */ /* Typical output: */ /* ID var1_old var1_new */ /* */ /* 32747 3 2 */ /* 32676 4 5 */ /* 36472 2 3 */ /* 47699 5 4 */ set printback=no mprint=no length=none. file handle oldfile /name='substitute path and filename'. file handle newfile /name='substitute path and filename'. define checkvar (!pos=!tokens(1)). match files file=oldfile /rename= (!1=!concat(!1,'_old')) /in=inold /file=newfile /rename= (!1=!concat(!1,'_new')) /in=innew /keep=!concat(!1,'_old') !concat(!1,'_new') ID /by ID. sel if inold and innew. /* select cases in both files */ display labels var=!concat(!1,'_new'). sel if !concat(!1,'_old') ne !concat(!1,'_new'). list /ID !concat(!1,'_old') !concat(!1,'_new'). !enddefine. /* drop ID and variables not in new file here */ /* drop any other variables you don't want to compare */ get file=oldfile /drop= ID . n of cases 1. oms /destination viewer=no. flip. omsend. write outfile="c:\temp\checkvar.sps"/"checkvar " case_lbl ".". exe. include "c:\temp\checkvar.sps". |
Related pages
...