- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I'm trying to replace a character with the carriage return line feed.
I’m trying to do it with the prxchange function:
data t1;
infile datalines delimiter='#';
input colA &$32. colB &$32.;
datalines;
mon,wed 8:30 - 16:15 # tue 8:30 - 16:30
thu 9:30 - 17:30 # fri 8:30 - 13:00
;
run;
data t2;
set t1;
colC=catx('|',colA,colB);
colD=prxchange('s/\|/\r\n/',-1,colC);/*this doesn't work*/
colE=catx('0D0A'x,colA,colB);/*this works*/
run;
I want that colD be the same of colE (which is calculated with an other function). The problem is that it finds correctly the pipe character, but it fails to replace it with the CRLF (\r\n). It seems like it's not able to perform a regEx replace, just the search...
In the example above, the first observation of colD becomes like this:
mon,wed 8:30 - 16:15\r\ntue 8:30 - 16:30
Instead of becoming like this:
mon,wed 8:30 - 16:15 tue 8:30 - 16:30
What am I doing wrong?
Thanks for the help!
T
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
If you just want print it as CRLF, not real '0D0A'x . you could try this one .
data t1;
infile datalines delimiter='#';
input colA &$32. colB &$32.;
datalines;
mon,wed 8:30 - 16:15 # tue 8:30 - 16:30
thu 9:30 - 17:30 # fri 8:30 - 13:00
;
run;
ods escapechar='~';
data t2;
set t1;
colC=catx('|',colA,colB);
colD=prxchange('s/\|/~n/',1,colC);/*this doesn't work*/
colE=catx('0D0A'x,colA,colB);/*this works*/
run;
proc print;run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi @tommymene86,
You can use macro language to get the special characters into the replacement text:
colD=prxchange("s/\|/%sysfunc(inputc(0D0A,$hex4.))/",-1,colC);
Note the surrounding double quotes instead of single quotes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Given that it's capable of searching using regex, why can't it do the same when it's time to replace?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@tommymene86 wrote:
Your solution works, but as I said to Ksharp, I'm trying to do it by using regular expression language...
Given that it's capable of searching using regex, why can't it do the same when it's time to replace?
In a sense, it is regular expression language. First, the macro processor resolves the %SYSFUNC call to a character string (consisting of the carriage return and line feed characters). Then PRXCHANGE works as usual and uses this character string as replacement text. The need for macro language comes from the fact that the replacement text is largely treated as just characters, so that the possibilities of providing '0D0A'x as replacement text are quite limited.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@FreelanceReinh wrote:In a sense, it is regular expression language. First, the macro processor resolves the %SYSFUNC call to a character string (consisting of the carriage return and line feed characters). Then PRXCHANGE works as usual and uses this character string as replacement text. The need for macro language comes from the fact that the replacement text is largely treated as just characters, so that the possibilities of providing '0D0A'x as replacement text are quite limited.
Thanks again for the explanation, but actually I was willing to use the \r\n metacharacters instead… It’d be much more simple to use regular expressions both in the search part and in the replace part…
But it seems to me that this is not an option...