BookmarkSubscribeRSS Feed
yadaw
Fluorite | Level 6

Hi guys,

 

I want to add particular number of white spaces in a string.

Like suppose we have string "MESSAGE" .

I want X number of spaces after 4th character of "MESSAGE" then how can i do the same.

 

Output should be like "MESS          AGE"  => X spaces between MESS and AGE.

 

Thanks in advance.

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Do something like this

 

data test;
	string="MESSAGE";
	newstring=cat(substr(string, 1, 4), '      ', substr(string, 5) );
run;
user24feb
Barite | Level 11

You can avoid typing 4 spaces manually with repeat:

 

Data Want;
  Format string $20.;
  string="MESSAGE"; 
  Substr(string, 5) = Repeat(' ', 3) || Substr(string, 5);  
Run;
Satish_Parida
Lapis Lazuli | Level 10
%macro InsertSpace(location=,no_spaces=)
Data Want;
set Have;
  /*Assuming the Variable opt for Transformation is string
     and it have enough length to contain the spaces
      */
  Substr(string, &location.) = Repeat(' ', &no_spaces.) || Substr(string, &location.);  
Run;
%mend InsertSpace;

%InsertSpace(location=5,no_spaces=3);

@user24feb This is little modification to your code. 

srinath3111
Quartz | Level 8

Hi,

 

 Data want;

Format string $20.;

string="Message";

substr(string,5)=repeat(' ',3) || substr(string,5);

run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 12248 views
  • 2 likes
  • 5 in conversation