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
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.
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
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.
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.
Many are very experienced and skilled in programming, But you are the best instructor on the forum!
Haikuo
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
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?
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.