Hi all,
My aim is to remove a specific set of characters from a string. I want to specify the set of characters to be removed by using a variable, instead of explicitly writing the characters in the prx function.
The code chunk below works, but instead of specifying 'W28' inside the prxchange function, I want to list a variable because the value could be different for every row that this function is iterated over.
data temp;
input id 1 ICD $ 3-5 MoreICDs $ 7-17;
datalines;
1 W28 W75 V86 X47
2 W75 W28 W75 X47
3 C67 W75 V86 X47
;
run;
data temp2;
set temp;
string1=prxchange('s/W28*//i',-1,MoreICDs);
*put string1=;
run;
What I want is something similar to the code chunk below where I replaced the value, 'W28' with the variable, ICD, inside the prxchange function but this syntax is not valid, obviously.
data temp; input id 1 ICD $ 3-5 MoreICDs $ 7-17; datalines; 1 W28 W75 V86 X47 2 W75 W28 W75 X47 3 C67 W75 V86 X47 ; run; data temp2; set temp;
length FinalICDs $11; FinalICDs=prxchange('s/',ICD,'*//i',-1,MoreICDs); run;
The output I want is:
ID ICD MoreICDs FinalICDs
1 W28 W28 W75 X47 W75 X47
2 W75 W28 W75 X47 W28 X47
3 C67 W75 V86 X47 W75 V86 X47
I've researched for awhile now and hoping someone in this community can help.
Thanks!
John
This would work:
data temp2;
set temp;
length FinalICDs $11;
FinalICDs=left(compbl(tranwrd(moreICDs, ICD, "")));
run;
and be more efficient, I guess, than an uncompiled prx pattern.
data temp;
input id 1 ICD $ MoreICDs $ 7-17;
datalines;
1 W28 W28 W75 X47
2 W75 W28 W75 X47
3 C67 W75 V86 X47
;
run;
data want;
set temp;
k=findw(MoreICDs,strip(icd));
if k>0 then substr(MoreICDs,k,3)=' ';
MoreICDs=compbl(MoreICDs);
drop k;
run;
/*Or keeping both Moreicds and the new want variable*/
data want;
set temp;
k=findw(MoreICDs,strip(icd));
want=MoreICDs;
if k>0 then substr(want,k,3)=' ';
want=compbl(want);
drop k;
run;
This would work:
data temp2;
set temp;
length FinalICDs $11;
FinalICDs=left(compbl(tranwrd(moreICDs, ICD, "")));
run;
and be more efficient, I guess, than an uncompiled prx pattern.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.