Hi everyone.
I need to separate character an numbers from a string variable.
For example, I have the string 'CL42N69A30' and I want to create a new variable equal to 'CL 42 N 69 A 30'.
In oracle the function "regexp_replace('CL42N69A30', '([[:digit:]]+)([[:alpha:]]+)', '\1 \2')" returns the string "CL42 N69 A30", when I combined "([[:digit:]]+)([[:alpha:]]+)" with "([[:alpha:]]+)([[:digit:]]+)" I have the string that I wanted "CL 42 N 69 A 30".
Any ideas on how to do this?
Thank you everybody
So that does what you asked it to do, it places a space after every digit.
But that regex doesn't look anything like the example in your original question.
452 data separa;
453 texto = "CL42N69A30";
454 separa = PRXCHANGE('s/(\d)/$1 /', -1, texto);
455 put (_all_) (=);
456 run;
texto=CL42N69A30 separa=CL4 2 N6 9 A3 0
I suspect that
[:alpha:]
has a different meaning in the SAS's regular expression than in your other system. Try telling to search for non digits instead.
492 data separa; 493 texto = "CL42N69A30"; 494 separa = PRXCHANGE('s/([^[:digit:]]+)/$1 /', -1,PRXCHANGE('s/([[:digit:]]+)/$1 /', -1, 494! texto)); 495 put (_all_) (=); 496 run; texto=CL42N69A30 separa=CL 42 N 69 A 30
So did you try that regular expression with the SAS functions that process regular expressions?
Hi Tom, thank's for answer me.
I tried whit the function PRXCHANGE, but the results that not that I wanted.
@elmendamian wrote:
Hi Tom, thank's for answer me.
I tried whit the function PRXCHANGE, but the results that not that I wanted.
Please post that code and then someone (I'm allergic to regex) can help ensure you've specified the code correctly.
Figuring out the pattern is usually the hardest part anyways.
Hi Reeza, thank you for answer me.
This is the SAS code:
data separa;
texto = "CL42N69A30";
separa = PRXCHANGE('s/(\d)/$1 /', -1, texto);
run;
So that does what you asked it to do, it places a space after every digit.
But that regex doesn't look anything like the example in your original question.
452 data separa;
453 texto = "CL42N69A30";
454 separa = PRXCHANGE('s/(\d)/$1 /', -1, texto);
455 put (_all_) (=);
456 run;
texto=CL42N69A30 separa=CL4 2 N6 9 A3 0
I suspect that
[:alpha:]
has a different meaning in the SAS's regular expression than in your other system. Try telling to search for non digits instead.
492 data separa; 493 texto = "CL42N69A30"; 494 separa = PRXCHANGE('s/([^[:digit:]]+)/$1 /', -1,PRXCHANGE('s/([[:digit:]]+)/$1 /', -1, 494! texto)); 495 put (_all_) (=); 496 run; texto=CL42N69A30 separa=CL 42 N 69 A 30
Yes Tom, the parameter '\d' insert a space between number, and the parameter '\u' insert a space every character of the string.
I need to insert a space between number and letter but I don't know how.
@elmendamian wrote:
Yes Tom, the parameter '\d' insert a space between number, and the parameter '\u' insert a space every character of the string.
I need to insert a space between number and letter but I don't know how.
Then how did you know how to generate the other regular expression that you started with in your question?
It is probably easier to just put spaces around the digit strings and use LEFT() function to remove any extra space that it might generate if the string starts with digits.
data separa;
texto = "CL42N69A30";
separa = left(PRXCHANGE('s/(\d+)/ $1 /', -1, texto));
run;
497 data separa; 498 texto = "CL42N69A30"; 499 separa = left(PRXCHANGE('s/(\d+)/ $1 /', -1, texto)); 500 put (_all_) (=); 501 run; texto=CL42N69A30 separa=CL 42 N 69 A 30
AWESOME!!!!
This is that I wanted.
Thank you very much Tom.
You should look at the SAS regular expression functions PRXChange or Call PRXChange and possibly PRXparse
A major difference besides the name of the function is that the SAS function places the variable to parse after the instructions.
Note: If you start inserting blanks into an existing SAS variable you may exceed the currently defined length of variable resulting in truncation.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.