SAS (Base SAS)+
*==============================================================================;
* Step 1: production DM (7 subjects) ;
*==============================================================================;
data dm;
infile cards truncover dlm="|";
length domain $2 usubjid $5 sex $3;
input usubjid $ sex $ age;
domain = "DM";
label usubjid = "Unique Subject Identifier";
cards;
1001|M|72
1002|F|65
1003|F|45
1004|M|40
1005|F|35
1006|F|6
1007|F|5
;
run;
*==============================================================================;
* Step 2: QC DM (6 subjects) — one mismatch, two missing, one extra ;
*==============================================================================;
data qc_dm;
infile cards truncover dlm="|";
length domain $2 usubjid $5 sex $3;
input usubjid $ sex $ age;
domain = "DM";
cards;
1001|M|72
1002|F|65
1003|F|45
1004|M|40
1005|M|35
1008|M|2
;
run;
*==============================================================================;
* Step 3: compare ;
*==============================================================================;
proc compare base = dm compare = qc_dm listall;
id usubjid;
run;
*------------------------------------------------------------------------------;
* Step 4 (optional): inspect the return code ;
*------------------------------------------------------------------------------;
%put NOTE: PROC COMPARE sysinfo = &sysinfo;- To compare two datasets in SAS, we run
PROC COMPAREwithbase=andcompare=arguments. The one named onbase=is the reference. - Every section of the report then reads as “how does COMPARE differ from BASE”, so we need to keep that framing in mind when explaining the output to a reviewer.
- We typically add an
IDstatement so SAS knows which set of variables identifies a row. Without it, PROC COMPARE lines up records by position, which is rarely what we want. - With
id usubjid, SAS matches on subject and then reports value mismatches per variable for the matched rows. - For the most thorough listing, we add the
listalloption. That asks for every available report — variables only in one side, observations only in one side, and the value-by-value comparison. - Verbose, but it leaves nothing to assumptions.
- Things we confirm in the listing:
- Variables summary — are all expected variables present in both datasets with matching types and lengths?
- Observation summary — how many rows are in BASE only, in COMPARE only, and in both?
- Value comparison results — for matched rows, which variables differ and for which subjects?
- When PROC COMPARE sits inside a validation macro, we can also check the return code
&sysinfo. - A value of
0means everything matched; any non-zero value means at least one discrepancy. - Useful when we want the program to fail automatically on any mismatch.