data have;
input x $;
cards;
24
1
3mn8
s27
25
;run;
Now I have to create a variable y of length=6, such as:
000024
000001
003mn8
0000s27
000025
How can I do this? Any help appreciated!
WOW, so many brilliant solutions , Just want to add another solution. ![]()
data have; input x : $4.; cards; 24 1 3mn8 s27 25 ; run; data want; length x $ 6; set have; xx=translate(right(x),'0',' '); run;
Ksharp
y = '000000';
substr(y, 7-length(x)) = x;
But there better not be any blank values for X, or any longer than 6 characters.
Good luck.
data have;
input x $;
cards;
24
1
3mn8
s27
25
;
data want;
set have;
do _n_=1 to 6-length(x);
x=cats('0',x);
end;
run;
proc print;run;
Obs x
1 000024
2 000001
3 003mn8
4 000s27
5 000025
Or this:
data have;
input x $;
cards;
24
1
3mn8
s27
25
;
data want;
set have;
x=repeat('0',6-length(x))||left(x);
run;
proc print;run;
Regards,
Haikuo
There's a pitfall using REPEAT. It starts with your first parameter, then repeats it an ADDITIONAL number of times. You will get one more zero than you bargained for!
Aha! Good catch! I did NOT know that. And I am sorry I did not count it either.
I guess OP should know how to modify my code to overcome this pitfall. ![]()
Haikuo
WOW, so many brilliant solutions , Just want to add another solution. ![]()
data have; input x : $4.; cards; 24 1 3mn8 s27 25 ; run; data want; length x $ 6; set have; xx=translate(right(x),'0',' '); run;
Ksharp
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!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.