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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

6 REPLIES 6
Doc_Duke
Rhodochrosite | Level 12

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

learner_sas
Quartz | Level 8

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.

Astounding
PROC Star

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.

Haikuo
Onyx | Level 15

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

Haikuo

learner_sas
Quartz | Level 8

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

Astounding
PROC Star

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?

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 6 replies
  • 1156 views
  • 3 likes
  • 4 in conversation