工作中,雙側(cè)編程會(huì)涉及結(jié)果的Compare,目的是為了確認(rèn)雙側(cè)編程結(jié)果一致。對(duì)于不一致的結(jié)果,查明原因后,進(jìn)行程序更新。
這一過程中,最先處理的是,數(shù)據(jù)集比較結(jié)果不一致如何展示的問題。
Compare過程步比較結(jié)果,可以通過以下4種方式進(jìn)行展示:
- SAS日志
- 自動(dòng)宏變量SYSINFO的返回值
- 過程步的Results輸出
- 結(jié)果輸出到數(shù)據(jù)集
1. SAS日志
當(dāng)使用WARNING、PRINTALL、ERROR選項(xiàng)時(shí),Compare過程步會(huì)在SAS日志中輸出不一致的描述。以ERROR選項(xiàng)為例,演示程序如下:
data base;
set sashelp.class;
run;
data comp;
set sashelp.class;
if _n_ = 1 then height =100;
run;
proc compare base = base comp=comp error;
run;
SAS日志輸出結(jié)果如下:

2. 自動(dòng)宏變量SYSINFO的返回值
Compare過程步運(yùn)行結(jié)束之后,會(huì)在自動(dòng)宏變量SYSINFO中返回一個(gè)值,這個(gè)返回值記錄了比較結(jié)果的信息。在Compare過程步運(yùn)行之后以及其他過程步運(yùn)行之前,通過檢查SYSINFO的返回值,可以獲取不一致的具體描述信息。很多公司Compare的宏程序也是利用SYSINFO宏變量輸出比較的結(jié)果。
SAS官方文檔中,對(duì)返回值有具體的描述,Code具體值為2的(n-1)次方,n對(duì)應(yīng)具體描述的位序。(來源:SAS Help Center: Results: PROC COMPARE)

如果在一次比較中,以上16種情形出現(xiàn)不止一個(gè),那么SAS會(huì)輸出各出現(xiàn)情形對(duì)應(yīng)Code值的求和。
舉例1,變量值不同:
data base;
set sashelp.class;
run;
data comp;
set sashelp.class;
if _n_ = 1 then height =100;
run;
proc compare base = base comp=comp;
run;
%let comres=&sysinfo.;
%put Compare resulst code: &comres.;
日志輸出如下:

輸出結(jié)果為4096,對(duì)應(yīng)第13種情況(2的12次方),A value comparision was unequal。
舉例2,變量值以及Label不同:
data base;
set sashelp.class;
run;
data comp;
set sashelp.class;
if _n_ = 1 then height =100;
label weight = "W";
run;
proc compare base = base comp=comp;
run;
%let comres=&sysinfo.;
%put Compare resulst code: &comres.;
日志輸出如下:

輸出結(jié)果為4128,4096+32,對(duì)應(yīng)第6和第13種情形,Variable has different label,A value comparison was unequal。
關(guān)于多種情形如何定位區(qū)分的問題,SAS文檔中提供了一種二進(jìn)制匹配確認(rèn)的方法:
%let rc=&sysinfo;
data _null_;
/* 1. Test for data set label */
if &rc = '1'b then
put '<<<< Data sets have different labels';
/* 2. Test for data set types */
if &rc = '1.'b then
put '<<<< Data set types differ';
/* 3. Test for variable informats */
if &rc = '1..'b then
put '<<<< Variable has different informat';
/* 4. Test for variable formats */
if &rc = '1...'b then
put '<<<< Variable has different format';
/* 5. Test for length */
if &rc = '1....'b then
put '<<<< Variable has different lengths between the base data set
and the comparison data set';
/* 6. Test for label */
if &rc = '1.....'b then
put '<<<< Variable has different label';
/* 7. Test for base observation */
if &rc = '1......'b then
put '<<<< Base data set has observation not in comparison data set';
/* 8. Test for comparison observation */
if &rc = '1.......'b then
put '<<<< Comparison data set has observation not in base';
/* 9. Test for base BY group */
if &rc = '1........'b then
put '<<<< Base data set has BY group not in comparison';
/* 10. Test for comparison BY group */
if &rc = '1.........'b then
put '<<<< Comparison data set has BY group not in base';
/* 11. Variable in base data set not in compare data set */
if &rc ='1..........'b then
put '<<<< Variable in base data set not found in comparison data set';
/* 12. Comparison data set has variable not in base data set */
if &rc = '1...........'b then
put '<<<< Comparison data set has variable not contained in the
base data set';
/* 13. Test for values */
if &rc = '1............'b then
put '<<<< A value comparison was unequal';
/* 14. Conflicting variable types */
if &rc ='1.............'b then
put '<<<< Conflicting variable types between the two data sets
being compared';
/* 15. Test for BY variables */
if &rc = '1..............'b then
put '<<<< BY variables do not match';
/* 16. Fatal error*/
if &rc ='1...............'b then
put '<<<< Fatal error: comparison not done';
run;
上一個(gè)Compare過程步返回結(jié)果為4128,運(yùn)行以上代碼后,SAS日志顯示如下,所有不一致情形都會(huì)輸出。SAS這樣設(shè)計(jì)很巧妙,每一個(gè)返回值對(duì)應(yīng)的情形都能夠清晰輸出。

3. 過程步的Results輸出
在SAS中運(yùn)行Compare過程步后,Results頁面也會(huì)有輸出結(jié)果的匯總,匯總的內(nèi)容有以下幾種:
- Data Set Summary
- Variables Summary
- Observation Summary
- Values Comparison Summary
- Value Comparison Results
- Table of Summary Statistics
- Comparison Results for Observations (Using the TRANSPOSE Option)
示例代碼:
data base;
set sashelp.class;
run;
data comp;
set sashelp.class;
if _n_ = 1 then height =100;
label weight = "W";
run;
proc compare base = base comp=comp;
run;
Results頁面結(jié)果如下:

Results頁面中的內(nèi)容也可以輸出到外部文檔中:
ods rtf file = "E:\99_Test\Test\Compare_results.rtf";
proc compare base = base comp=comp;
run;
ods rtf close;
結(jié)果輸出到指定目標(biāo)文件中:

外部文件內(nèi)容截取部分:

關(guān)于如何保留或刪除特定匯總結(jié)果,具體參考SAS官方文檔: SAS Help Center: Results: PROC COMPARE。
4. 結(jié)果輸出到數(shù)據(jù)集(out=選項(xiàng))
這個(gè)方式在之前的文章中介紹過,具體參考SAS編程:分享數(shù)據(jù)集Compare的小經(jīng)驗(yàn),輸出的結(jié)果數(shù)據(jù)集為,相比較兩個(gè)數(shù)據(jù)集變量值不同的記錄。
我常用的選項(xiàng)設(shè)置如下:
proc compare base = base comp = comp out=df
outbase outcomp outdif outnoequal;
run;
輸出數(shù)據(jù)集結(jié)果如下:

總結(jié)
文章介紹了,SAS中Compare過程步結(jié)果輸出的4種方式,讀者可以結(jié)合自己的工作需求,進(jìn)行“私人定制”。
同時(shí),各家公司Compare宏程序大都也是基于以上幾種方式進(jìn)行輸出,希望能夠幫助讀者理解本公司宏程序運(yùn)行機(jī)制。
感謝閱讀, 歡迎關(guān)注!
若有疑問,歡迎評(píng)論交流!