Hi,
Whilst you have already been provided with a number of solutions, I have tried to break down each part of the regular expression in the comments in the below data step:
data have;
infile datalines truncover;
input fac_code $char50.;
datalines;
K111I001 C01
A01B001
;
data want(drop = substitute);
/* define perl regular expression for substitution (assigned once in data step) */
substitute = cat(
's/' /* substitution specification start: */
,'(\d)' /* replace any digit */
,'([A-Z])' /* that is followed by an uppercase letter */
,'/' /* with */
,'$1' /* the digit that was found */
,' ' /* followed by a new space character */
,'$2' /* and the uppercase letter that was found */
,'/' /* substitution specification end */
,'o' /* compile-once (i.e., not every obs) */
);
/* use Do-Whitlock to loop through obs */
do until(last_obs);
set have end = last_obs;
fac_code_clean = prxchange(substitute, -1, fac_code);
output;
end;
/* prevent unnecessary return to start of data step */
stop;
run;
Thanks & kind regards,
Amir.
... View more