问题描述
文档说,如果您使用的是 end=
或 set
,则 point
语句末尾的 by
选项将不起作用,但下面的代码两者都没有,但我的最后一个 do 块从未运行。有没有办法让它运行?
data my_peeps ;
input
@1 mrn $char10.
;
datalines ;
roy
mary
gene
bobby
joey
dee-dee
sting
evelyn
yo-mama
dude
dude2
bam-bam
;
run ;
data mrn_remaps ;
input
@1 mrn $char10.
@13 should_be $char10.
;
datalines ;
roy teddy
bobby robert
yo-mama mrs_p
dude phil
dude2 phil
bam-bam james
;
run ;
data corrected_peeps ;
length should_be $ 10 ; * <-- needed for hash ;
retain __num_corrections 0 ;
set my_peeps end = last_record ;
if _n_ = 1 then do ;
declare hash corrections(dataset: 'mrn_remaps') ;
corrections.definekey('mrn') ;
corrections.definedata('mrn','should_be') ;
corrections.definedone() ;
call missing (should_be) ;
end ;
* This while loop seems to be whats killing the end= block. ;
do while(corrections.find() = 0) ;
put "INFO: found a merged MRN. BEWARE OF DUPLICATES IN OUTPUT DATASET!" ;
mrn = should_be ;
__num_corrections = __num_corrections + 1 ;
end ;
if last_record then do ;
put 'INFO: Corrected ' __num_corrections 'MRNs.' ;
end ;
drop should_be __num_corrections ;
run ;
proc print ;
run ;
解决方法
Codewise,您可以让.find()
执行更正替换,如果您觉得有必要记录信息,可以让.check
执行。这些更改稍微压缩了代码。它确实需要更改重映射表以包含列 old_mrn
(key) 和 mrn
,而不是 mrn
(key) 和 new_mrn
。
示例:
data my_peeps ;
input
@1 mrn $char10.
;
datalines ;
roy
mary
gene
bobby
joey
dee-dee
sting
evelyn
yo-mama
dude
dude2
bam-bam
;
data mrn_remaps ;
input
@1 old_mrn $char10.
@13 mrn $char10.
;
datalines ;
roy teddy
bobby robert
yo-mama mrs_p
dude phil
dude2 phil
bam-bam james
;
data corrected_peeps ;
retain _count;
set my_peeps end = last_record ;
length old_mrn $ 10 ; * <-- needed for hash ;
if _n_ = 1 then do ;
declare hash corrections(dataset: 'mrn_remaps') ;
corrections.definekey('old_mrn') ;
corrections.definedata('mrn') ;
corrections.definedone();
call missing(old_mrn); * <-- prevent uninitialized NOTE;
end ;
* if corrections.check(key:mrn) then
put 'INFO:' mrn= 'will be corrected';
* increment counter and perform replacement of value in host variable if find succeeds;
_count + (corrections.find(key:mrn) = 0);
if last_record then do ;
put 'INFO: Corrected ' _count 'MRNs.' ;
end ;
drop _:;
run ;