DATA Step, Macro, Functions and more

Replace character with CRLF with prxchange

Reply
Occasional Contributor
Posts: 5

Replace character with CRLF with prxchange

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

Super User
Posts: 10,848

Re: Replace character with CRLF with prxchange

Posted in reply to tommymene86

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;
Occasional Contributor
Posts: 5

Re: Replace character with CRLF with prxchange

Hi thanks for the tip, but I was actually trying to get the same result of colE, but using regular expressions...
Trusted Advisor
Posts: 1,322

Re: Replace character with CRLF with prxchange

Posted in reply to tommymene86

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.

Occasional Contributor
Posts: 5

Re: Replace character with CRLF with prxchange

Posted in reply to FreelanceReinhard
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?
Trusted Advisor
Posts: 1,322

Re: Replace character with CRLF with prxchange

Posted in reply to tommymene86

@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.

 

Occasional Contributor
Posts: 5

Re: Replace character with CRLF with prxchange

[ Edited ]
Posted in reply to FreelanceReinhard

@FreelanceReinhard 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...

Ask a Question
Discussion stats
  • 6 replies
  • 102 views
  • 0 likes
  • 3 in conversation