BookmarkSubscribeRSS Feed
tommymene86
Fluorite | Level 6

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

6 REPLIES 6
Ksharp
Super User

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;
tommymene86
Fluorite | Level 6
Hi thanks for the tip, but I was actually trying to get the same result of colE, but using regular expressions...
FreelanceReinh
Jade | Level 19

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.

tommymene86
Fluorite | Level 6
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?
FreelanceReinh
Jade | Level 19

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

 

tommymene86
Fluorite | Level 6

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 2402 views
  • 0 likes
  • 3 in conversation