- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
So did you try that regular expression with the SAS functions that process regular expressions?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi Tom, thank's for answer me.
I tried whit the function PRXCHANGE, but the results that not that I wanted.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi Reeza, thank you for answer me.
This is the SAS code:
data separa;
texto = "CL42N69A30";
separa = PRXCHANGE('s/(\d)/$1 /', -1, texto);
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
AWESOME!!!!
This is that I wanted.
Thank you very much Tom.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.