如何替换字符串
how to replace string
如果第 'all' 列包含第 'sch' 列中的字符串,则该字符串将被 'rep' 中的字符串替换。 'new_all' 列是我所期望的。
data a0;
input sch . rep . ;
cards;
map_clm map_claim
xyz_ttt xyz
drug_clm drug_clm_test
fee_sch fee_sch_test
;
run;
data a1;
input all . new_all .;
cards;
from abc.xyz_ttt d2 left from abc.xyz d2 left
D1.Xwlk,abc.xyz_TTT left D1.xwlk,abc.xyz left
d1.x,abc.map_clms,d2.dos d1.x,abc.map_clms,d2.dos
ABC.XYZ_Ttt left join d1 ABC.xyz left join d1
,tt.drug_CLM, tt.Xyz_ttt ,tt.drug_clm_test, tt.xyz
d3.DOS,t2.fee_SCH,tt.day fd3.DOS,t2.fee_sch_test,tt.day
;
run;
我假设您要将 all
列转换为 new_all
列,使用 a0
数据集中的值进行 describe/control 转换。
%macro do_it;
* Set up some macro variable arrays to hold the from/to pairs;
data _null_;
set a0 end=end;
call symput("sch" || _n_, sch);
call symput("rep" || _n_, rep);
if end then call symput("max", _n_);
run;
* Process the data;
data want;
length new_all_new $ 200;
set a1;
* Copy the incoming value to the output;
new_all_new = all;
* Apply each of the transformations in turn;
%do i = 1 %to &max;
new_all_new = tranwrd(new_all_new, "&&sch&i", "&&rep&i");
%end;
* Did we get the expected value?;
if new_all_new ne new_all then do;
put "ERROR: Did not get expected value: " all= new_all= new_all_new=;
end;
run;
%mend;
%do_it;
上面的代码应该非常接近,虽然我现在无法测试它。此代码区分大小写;您的预期数据表明您希望不区分大小写地应用转换,同时还保留字符串其余部分的大小写。实施起来有点棘手;如果确实需要,正则表达式搜索和替换 (prxparse("s/&&sch&i/&&rep&i/");
) 可能是最好的方法。
还有一个问题是您想要替换 'map_clm' 而不是 'map_clms',这也表明正则表达式可能是最干净的解决方案。
无论如何,我认为这段代码为您的工作提供了一个合理的起点。很高兴你有内置的预期值来测试。
您真的在以编程方式修改 SQL 代码吗?
case when t1.all CONTAINS t1.sch
then t1.rep
end
如果您跳过 'else' 语句,如果不满足条件,您将获得新列的缺失值 (null)。如果要保留 t1.all 值,则必须在 end
.
之前添加 else t1.all
语句
如果第 'all' 列包含第 'sch' 列中的字符串,则该字符串将被 'rep' 中的字符串替换。 'new_all' 列是我所期望的。
data a0;
input sch . rep . ;
cards;
map_clm map_claim
xyz_ttt xyz
drug_clm drug_clm_test
fee_sch fee_sch_test
;
run;
data a1;
input all . new_all .;
cards;
from abc.xyz_ttt d2 left from abc.xyz d2 left
D1.Xwlk,abc.xyz_TTT left D1.xwlk,abc.xyz left
d1.x,abc.map_clms,d2.dos d1.x,abc.map_clms,d2.dos
ABC.XYZ_Ttt left join d1 ABC.xyz left join d1
,tt.drug_CLM, tt.Xyz_ttt ,tt.drug_clm_test, tt.xyz
d3.DOS,t2.fee_SCH,tt.day fd3.DOS,t2.fee_sch_test,tt.day
;
run;
我假设您要将 all
列转换为 new_all
列,使用 a0
数据集中的值进行 describe/control 转换。
%macro do_it;
* Set up some macro variable arrays to hold the from/to pairs;
data _null_;
set a0 end=end;
call symput("sch" || _n_, sch);
call symput("rep" || _n_, rep);
if end then call symput("max", _n_);
run;
* Process the data;
data want;
length new_all_new $ 200;
set a1;
* Copy the incoming value to the output;
new_all_new = all;
* Apply each of the transformations in turn;
%do i = 1 %to &max;
new_all_new = tranwrd(new_all_new, "&&sch&i", "&&rep&i");
%end;
* Did we get the expected value?;
if new_all_new ne new_all then do;
put "ERROR: Did not get expected value: " all= new_all= new_all_new=;
end;
run;
%mend;
%do_it;
上面的代码应该非常接近,虽然我现在无法测试它。此代码区分大小写;您的预期数据表明您希望不区分大小写地应用转换,同时还保留字符串其余部分的大小写。实施起来有点棘手;如果确实需要,正则表达式搜索和替换 (prxparse("s/&&sch&i/&&rep&i/");
) 可能是最好的方法。
还有一个问题是您想要替换 'map_clm' 而不是 'map_clms',这也表明正则表达式可能是最干净的解决方案。
无论如何,我认为这段代码为您的工作提供了一个合理的起点。很高兴你有内置的预期值来测试。
您真的在以编程方式修改 SQL 代码吗?
case when t1.all CONTAINS t1.sch
then t1.rep
end
如果您跳过 'else' 语句,如果不满足条件,您将获得新列的缺失值 (null)。如果要保留 t1.all 值,则必须在 end
.
else t1.all
语句