工作中,雙側(cè)編程會涉及結(jié)果的Compare匿垄,目的是為了確認(rèn)雙側(cè)編程結(jié)果一致移宅。對于不一致的結(jié)果,查明原因后椿疗,進(jìn)行程序更新漏峰。
這一過程中,最先處理的是届榄,數(shù)據(jù)集比較結(jié)果不一致如何展示的問題浅乔。
Compare過程步比較結(jié)果,可以通過以下4種方式進(jìn)行展示:
- SAS日志
- 自動宏變量SYSINFO的返回值
- 過程步的Results輸出
- 結(jié)果輸出到數(shù)據(jù)集
1. SAS日志
當(dāng)使用WARNING
、PRINTALL
靖苇、ERROR
選項時席噩,Compare過程步會在SAS日志中輸出不一致的描述。以ERROR
選項為例贤壁,演示程序如下:
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. 自動宏變量SYSINFO的返回值
Compare過程步運行結(jié)束之后悼枢,會在自動宏變量SYSINFO中返回一個值,這個返回值記錄了比較結(jié)果的信息脾拆。在Compare過程步運行之后以及其他過程步運行之前馒索,通過檢查SYSINFO的返回值,可以獲取不一致的具體描述信息名船。很多公司Compare的宏程序也是利用SYSINFO宏變量輸出比較的結(jié)果绰上。
SAS官方文檔中,對返回值有具體的描述渠驼,Code具體值為2的(n-1)次方蜈块,n對應(yīng)具體描述的位序。(來源:SAS Help Center: Results: PROC COMPARE)
如果在一次比較中迷扇,以上16種情形出現(xiàn)不止一個百揭,那么SAS會輸出各出現(xiàn)情形對應(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信峻,對應(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隘庄,對應(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;
上一個Compare過程步返回結(jié)果為4128,運行以上代碼后街州,SAS日志顯示如下兼丰,所有不一致情形都會輸出。SAS這樣設(shè)計很巧妙唆缴,每一個返回值對應(yīng)的情形都能夠清晰輸出鳍征。
3. 過程步的Results輸出
在SAS中運行Compare過程步后,Results頁面也會有輸出結(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=
選項)
這個方式在之前的文章中介紹過,具體參考SAS編程:分享數(shù)據(jù)集Compare的小經(jīng)驗氮双,輸出的結(jié)果數(shù)據(jù)集為碰酝,相比較兩個數(shù)據(jù)集變量值不同的記錄。
我常用的選項設(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)行“私人定制”造挽。
同時碱璃,各家公司Compare宏程序大都也是基于以上幾種方式進(jìn)行輸出弄痹,希望能夠幫助讀者理解本公司宏程序運行機(jī)制饭入。
感謝閱讀, 歡迎關(guān)注肛真!
若有疑問谐丢,歡迎評論交流!