BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SASdevAnneMarie
Barite | Level 11

Hello,

 

I'm trying to replace more than one space by ^n,

 

But my code doesn't work:

 

data OBJ1;

set OBJ2;

rx = prxparse('s/(\w+ \w*) +.*/^n/');
call prxchange(rx,-1,OBJECTIVES);

run;

 

Do you know please some options to replace more than two spaces?

Thank you for your help!

 

Best regards,

Marie

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

Please try the below code , in the example below in objectives variable we have sample1 followed by two spaces number 2 and then number 3 with one space and it replaced only the two spaces between 1 and 2 with ^n

 

data OBJ1;
OBJECTIVES='sample1  2 3 4  5  6';
new=prxchange('s/\b\s{2,}\b/^n/oi',-1,OBJECTIVES);
run;
Thanks,
Jag

View solution in original post

4 REPLIES 4
Jagadishkatam
Amethyst | Level 16

Please try the below code , in the example below in objectives variable we have sample1 followed by two spaces number 2 and then number 3 with one space and it replaced only the two spaces between 1 and 2 with ^n

 

data OBJ1;
OBJECTIVES='sample1  2 3 4  5  6';
new=prxchange('s/\b\s{2,}\b/^n/oi',-1,OBJECTIVES);
run;
Thanks,
Jag
yabwon
Onyx | Level 15

Small update if I may, to cover leading and trailing spaces:

data OBJ1;
  OBJECTIVES='   sample1  2 3 4    5          6   ';
  new=prxchange('s/(^|\b)\s{2,}(\b|$)/^n/oi',-1,OBJECTIVES);
  put _all_;
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



smantha
Lapis Lazuli | Level 10

What happened to plain old cmpbl (compressible function).

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 849 views
  • 0 likes
  • 4 in conversation