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

Hi 

 

I expect to create 'new' variable from the 'old' variable.

oldnew
00163T10000163T10
755F10000755F10
00846U1256000846U12560
1555000001555

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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;

View solution in original post

4 REPLIES 4
mklangley
Lapis Lazuli | Level 10

@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;
ballardw
Super User

@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.

novinosrin
Tourmaline | Level 20

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;
RichardDeVen
Barite | Level 11

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) );

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 962 views
  • 2 likes
  • 5 in conversation