Desktop productivity for business analysts and programmers

padding blanks on variable values

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 89
Accepted Solution

padding blanks on variable values

Hello All,

I have a character variable which has values of different length . I want to make all of them of same length. I used

if length(x_var) = 3 then x_new_var = x_var||"  ";

else if ....4 then ...;

I but the original length is not changing. I am wondering if any of you can help.

Ram


Accepted Solutions
Solution
‎04-08-2013 04:25 PM
Super User
Posts: 6,935

Re: padding blanks on variable values

Posted in reply to learner_sas

To speed up your learning of SAS, think about how you could test this for yourself.  For example, you could code:

data test;

length a $ 5;

a='ABC';

newvar = a || '*';

put newvar=;

run;

Good luck.

View solution in original post


All Replies
Trusted Advisor
Posts: 2,127

Re: padding blanks on variable values

Posted in reply to learner_sas

Ram,

I think that this is just a definition problem.  All character variables in SAS are fixed length.  The LENGTH function measures the number of characters, from the left, to the last non-blank character on the right.  Therefore, your x_var and x_new_var would both show as having the same LENGTH when you use that function.

Doc Muhlbaier

Duke

Frequent Contributor
Posts: 89

Re: padding blanks on variable values

Thanks Doc@duke,

I need to padd blank when a variable value is not equal to length of  5. Do you think I can safely assume that length of variable is 5 if i define length of the variable in the beginning of the statement as 5 regardless of the length given by length function.

Solution
‎04-08-2013 04:25 PM
Super User
Posts: 6,935

Re: padding blanks on variable values

Posted in reply to learner_sas

To speed up your learning of SAS, think about how you could test this for yourself.  For example, you could code:

data test;

length a $ 5;

a='ABC';

newvar = a || '*';

put newvar=;

run;

Good luck.

Respected Advisor
Posts: 3,187

Re: padding blanks on variable values

Posted in reply to Astounding

Many are very experienced and skilled in programming, But you are the best instructor on the forum!

Haikuo

Frequent Contributor
Posts: 89

Re: padding blanks on variable values

Posted in reply to learner_sas

Hello Astounding,

I used your dataset but my problem still persists.

14         GOPTIONS ACCESSIBLE;

15         data test;

16         length a $ 5;

17         a='ABC';

18         newvar = a || '*';

19         newvar1 = a||' ';

20         put newvar:'end';

21         put a :'end';

22         put newvar1:'end' ;

23         run;

ABC  * end

ABC end

ABC end

when I did put statement why  both of the ABC's(a and newvar1) has same length considering i added a blank on newvar1. Is there any way to achieve right padding on newvar1 with one more blank.I re run the process by removing length statement still I had same output.

Thanks,

Ram Rimal

Super User
Posts: 6,935

Re: padding blanks on variable values

Posted in reply to learner_sas

Ram,

You did actually add a blank.  You could see this by trying:

newvar2 = newvar1 || '*';

put newvar;

put newvar2;

The problem is that the PUT statement doesn't write out all the blanks.  By default, it writes all the nonblank characters, and then leaves a single blank following.

What is your final objective here?  If you would like to write a report that contains blanks, it's easy:

put @1 a  @7 'end';

When all is said and done, what would you like to achieve?

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 223 views
  • 3 likes
  • 4 in conversation