BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
DrAbhijeetSafai
Lapis Lazuli | Level 10
I want to know how to add leading spaces in SAS for values of a character variable. Kindly note that I do not want to remove spaces but want to add 5 white spaces in the leading section of values of a character variable. Thanks in advance.
Dr. Abhijeet Safai
Certified Base and Clinical SAS Programmer
Associate Data Analyst
Actu-Real
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

This is certainly a rather unusual request and I assume you want to do this for some reporting/indention.

You can easily add leading blanks just by concatenating such blanks to the variable BUT depending on the procedure and especially the output destination used these leading blanks might still not print as you'd like it.

Consider the following code:

data test;
  length var2 var3 $10;
  format var3 $char10.;
  var='abcd';
  var2='     '||var;
  var3='     '||var;
run;
ods listing;
proc print data=test;
run;

Listing destination:

Patrick_1-1657536713532.png

HTML destination (the SAS EG default):

Patrick_2-1657536843688.png

 

So if this is about reporting/printing then eventually it's more about using styles instead of adding blanks to the internal value of SAS variables.

 

Let us know a bit more in detail what you have and what you need and we can go from there.

 



 

View solution in original post

3 REPLIES 3
andreas_lds
Jade | Level 19

Is the variable long enough to store five additional chars? Something like this should do:

var = cat(repeat(' ', 4), var);

Please note, that html and log may hide those leading spaces.

 

Patrick
Opal | Level 21

This is certainly a rather unusual request and I assume you want to do this for some reporting/indention.

You can easily add leading blanks just by concatenating such blanks to the variable BUT depending on the procedure and especially the output destination used these leading blanks might still not print as you'd like it.

Consider the following code:

data test;
  length var2 var3 $10;
  format var3 $char10.;
  var='abcd';
  var2='     '||var;
  var3='     '||var;
run;
ods listing;
proc print data=test;
run;

Listing destination:

Patrick_1-1657536713532.png

HTML destination (the SAS EG default):

Patrick_2-1657536843688.png

 

So if this is about reporting/printing then eventually it's more about using styles instead of adding blanks to the internal value of SAS variables.

 

Let us know a bit more in detail what you have and what you need and we can go from there.

 



 

DrAbhijeetSafai
Lapis Lazuli | Level 10

Thank you @Patrick for the quick response. I was also thinking on the same lines but it was not working at that time. However, after sometime the solution given by you worked, however, I had not seen your response at that time. It just worked (maybe there was some other issue) and now I am seeing your response. Thanks a lot once again for the quick response. It is surely helpful. 

- Dr. Abhijeet Safai

Dr. Abhijeet Safai
Certified Base and Clinical SAS Programmer
Associate Data Analyst
Actu-Real

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 3643 views
  • 2 likes
  • 3 in conversation