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

Hi guys, I want to add particular number of white spaces in a string. Like suppose we have string "MESSAGE7676ASFD" . I want X number of spaces before numeric of "MESSAGE7676ASFD" then how can i do the same. Output should be like "MESSAGE  7676  ASFD" =>  X spaces between MESS AGE  7676  ASFD . Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
r_behata
Barite | Level 11
/*Regular Expression Definition*/
/*1st Capturing Group (^[A-Za-z])*/
/*^ asserts position at start of a line*/
/*Match a single character present in the list below [A-Za-z]*/
/*+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed */
/*a-z matches a single character in the range between a  and z */
/*2nd Capturing Group ([0-9])*/
/*Match a single character present in the list below [0-9]*/
/*+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed */
/*0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) */
/*3rd Capturing Group ([A-Za-z])*/
/*Match a single character present in the list below [A-Za-z]*/
/*+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed */
/*a-z matches a single character in the range between a  and z */

data want;
	str="MESSAGE7676ASFD";
	str_new=prxchange("s/(^[a-z]+)([0-9]+)([a-z]+)/$1 $2 $3/io",-1,str);

	put (_all_) (=);
run;
40         data want;
41         	str="MESSAGE7676ASFD";
42         	str_new=prxchange("s/(^[a-z]+)([0-9]+)([a-z]+)/$1 $2 $3/io",-1,str);
43         
44         	put (_all_) (=);
45         run;

str=MESSAGE7676ASFD str_new=MESSAGE 7676 ASFD

View solution in original post

3 REPLIES 3
r_behata
Barite | Level 11
/*Regular Expression Definition*/
/*1st Capturing Group (^[A-Za-z])*/
/*^ asserts position at start of a line*/
/*Match a single character present in the list below [A-Za-z]*/
/*+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed */
/*a-z matches a single character in the range between a  and z */
/*2nd Capturing Group ([0-9])*/
/*Match a single character present in the list below [0-9]*/
/*+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed */
/*0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) */
/*3rd Capturing Group ([A-Za-z])*/
/*Match a single character present in the list below [A-Za-z]*/
/*+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed */
/*a-z matches a single character in the range between a  and z */

data want;
	str="MESSAGE7676ASFD";
	str_new=prxchange("s/(^[a-z]+)([0-9]+)([a-z]+)/$1 $2 $3/io",-1,str);

	put (_all_) (=);
run;
40         data want;
41         	str="MESSAGE7676ASFD";
42         	str_new=prxchange("s/(^[a-z]+)([0-9]+)([a-z]+)/$1 $2 $3/io",-1,str);
43         
44         	put (_all_) (=);
45         run;

str=MESSAGE7676ASFD str_new=MESSAGE 7676 ASFD
JNR
Calcite | Level 5 JNR
Calcite | Level 5

You can use the prxchange function for this.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 1368 views
  • 1 like
  • 3 in conversation