DATA Step, Macro, Functions and more

Inserting number of spaces in between a string

Reply
Occasional Contributor
Posts: 5

Inserting number of spaces in between a string

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.

PROC Star
Posts: 1,190

Re: Inserting number of spaces in between a string

Do something like this

 

data test;
	string="MESSAGE";
	newstring=cat(substr(string, 1, 4), '      ', substr(string, 5) );
run;
Super Contributor
Posts: 346

Re: Inserting number of spaces in between a string

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;
Frequent Contributor
Posts: 109

Re: Inserting number of spaces in between a string

Posted in reply to user24feb
%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. 

Contributor
Posts: 37

Re: Inserting number of spaces in between a string

Hi,

 

 Data want;

Format string $20.;

string="Message";

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

run;

Ask a Question
Discussion stats
  • 4 replies
  • 121 views
  • 2 likes
  • 5 in conversation