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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.