Hi
I expect to create 'new' variable from the 'old' variable.
old | new |
00163T10 | 000163T10 |
755F10 | 000755F10 |
00846U12560 | 00846U12560 |
1555 | 000001555 |
if the length of strings is less or equal than 9 then I expect to fill 0 to the front to make the length as 9, such as row 1,2, and 4.
if the length of strings is larger than 9 then I expect to ignore it, such as row 3.
data table1; infile cards truncover; input old $50. ; cards; 00163T10 755F10 00846U12560 1555 ;;;;
Could you please give me some suggestions about this?
Thanks in advance.
Hi @Alexxxxxxx It's fairly straightforward with PUT and TRANSLATE functions combo
data table1;
infile cards truncover;
input
old $50.
;
cards;
00163T10
755F10
00846U12560
1555
;;;;
data want;
set table1;
length need $50;
if lengthn(old)<9 then need=translate(put(old,$9. -r),'0',' ');
else need=old;
run;
@Alexxxxxxx Is this what you're looking for?
data table1;
infile cards truncover;
input
old $50.
;
cards;
00163T10
755F10
00846U12560
1555
;;;;
data want;
set table1;
new = strip(old);
if length(new) < 9 then do;
do until (length(new) = 9);
new = '0'||new;
end;
end;
run;
@Alexxxxxxx wrote:
Hi
I expect to create 'new' variable from the 'old' variable.
old new 00163T10 000163T10 755F10 000755F10 00846U12560 00846U12560 1555 000001555 if the length of strings is less or equal than 9 then I expect to fill 0 to the front to make the length as 9, such as row 1,2, and 4.
if the length of strings is larger than 9 then I expect to ignore it, such as row 3.
data table1; infile cards truncover; input old $50. ; cards; 00163T10 755F10 00846U12560 1555 ;;;;Could you please give me some suggestions about this?
Thanks in advance.
One way:
data table1; infile cards truncover; input old $50. ; length new $ 50.; if length(old) le 9 then new=cats(repeat('0',8-length(old)),old); else new = old; cards; 00163T10 755F10 00846U12560 1555 ;;;;
The Repeat function will repeat the string the number of times indicated. So if you "repeat" 0 one time the result is 00. So the calculation uses 8 instead of the likely more expected 9- length of the value. Length, if you weren't aware returns the number of used characters in a string which would include leading spaces if any. CATS combines string values stripping spaces.
Hi @Alexxxxxxx It's fairly straightforward with PUT and TRANSLATE functions combo
data table1;
infile cards truncover;
input
old $50.
;
cards;
00163T10
755F10
00846U12560
1555
;;;;
data want;
set table1;
length need $50;
if lengthn(old)<9 then need=translate(put(old,$9. -r),'0',' ');
else need=old;
run;
Two ways as a function result. First
new = substr(repeat('0',8) || old, length(old)+10-max(length(old),9));
Second @ballardw way via IFC
new = ifc ( length(old) > 9, old, cats(repeat('0',8-length(old)), old) );
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.