Hello,
I have a variable named "fac_code" which contains code delimited by space. However, there are some errors in code where there is no delimiter between code. The code starts with a letter and followed by some digits. I would like to fix it by adding space between codes. For example,
fac_code
K111I001 C01
A01B001
I want it to be
K111 I001 C01
A01 B001
I did something like
data a;
set a;
if prxmatch('/[A-Z](\d+)[A-Z](\d+)/)', fac_code)>0 then
fac_code_clean = catx(' ', char(fac_code,2)) but it does not work
Any help is appreciated! Thanks in advance
data have; input x $80.; cards; K111I001 C01 A01B001 ; data want; set have; want=compbl(prxchange('s/([a-z]\d+)/\1 /i',-1,x)); run;
What is the plain language rule for adding blanks?
Hi @ballakaay
Try this
data have;
length fac_code $20; /* Declare a length that allows adding spaces */
fac_code='K111I001 C01'; output;
fac_code='A01B001'; output;
run;
data want;
set have;
fac_code=compress(fac_code); /* Remove existing spaces */
RegularExpressionId = prxparse('s/\w\d{2,}/ $0/');
call prxchange(RegularExpressionId, -1, fac_code); /* Insert delimiting space */
fac_code=strip(fac_code);
put fac_code=;
run;
data have; input x $80.; cards; K111I001 C01 A01B001 ; data want; set have; want=compbl(prxchange('s/([a-z]\d+)/\1 /i',-1,x)); run;
Thank you so much! That is exactly what I want
One way:
data HAVE;
CODE='K111I001 C01 '; output;
CODE='A01B001 '; output;
run;
data WANT;
set HAVE;
CODE = prxchange('s/(\d)([A-Z]\d*)/\1 \2/', -1, CODE);
putlog CODE=;
run;
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.
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.