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
Amethyst | Level 16

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



SASdevAnneMarie
Barite | Level 11
Thank you!
smantha
Lapis Lazuli | Level 10

What happened to plain old cmpbl (compressible function).

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1970 views
  • 0 likes
  • 4 in conversation