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

I have a very simple problem. I'm looking to pad the front of a character value with 0's until it hits a certain length (8). And I do NOT want to use the "z." format. I figured it would be simple like this:

 

data hello_world;
infile datalines;
input ID $;
cards;
12
234
3456
;;
run;

data hallow_world;
  set hello_world;
  do while(length(ID)<8);
  cats('0',ID);
  end;
run;

but is throwing an error. Seems like the only part I have wrong is what is within the do while loop.

 

So please help - someone can I do this within a "do while"? Also tried like this new_var=cats('0',ID); but it seemed to go into an infinite loop.

 

Most of the solutions I see online already use the "z." format and I want to avoid this.

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Or if you want to LOOP 

 


data hello_world;
infile datalines;
input ID $;
cards;
12
234
3456
;;
run;

data want;
set hello_world;
length want_id $8;
want_id=id;
do while(length(want_id)<8);
 want_id=cats('0',want_id);
end;
run;

View solution in original post

7 REPLIES 7
novinosrin
Tourmaline | Level 20

HI @Time_Looper47 

Simple arithmetic



data hello_world;
infile datalines;
input ID $;
cards;
12
234
3456
;;
run;

data want;
set hello_world;
length want_id $8;
want_id=cats(repeat('0',7-length(id)),id);
run;
novinosrin
Tourmaline | Level 20

Or if you want to LOOP 

 


data hello_world;
infile datalines;
input ID $;
cards;
12
234
3456
;;
run;

data want;
set hello_world;
length want_id $8;
want_id=id;
do while(length(want_id)<8);
 want_id=cats('0',want_id);
end;
run;
jklaverstijn
Rhodochrosite | Level 12

No need for a loop. How about this?

 

 

id_zero = cats(substr("00000000", 1, 8-length(id)), id);

 

 

Hope this helps,

-- Jan.

PaigeMiller
Diamond | Level 26

If you make ID a numeric variable, this is simple.

 

data hello_world;
infile datalines;
input ID;
format id z8.;
cards;
12
234
3456
;
--
Paige Miller
Ksharp
Super User

data hello_world;
infile datalines;
input ID $;
cards;
12
234
3456
;;
run;

data want;
set hello_world;
length want_id $8;
want_id=id;
want_id=translate(right(want_id),'0',' ');
run;
Time_Looper47
Obsidian | Level 7

@Ksharp This ended up being the only one that worked with my real data actually. Thanks!

gamotte
Rhodochrosite | Level 12

Hello,

 

Another solution.

 

new_ID="00000000";
substr(new_id,9-length(id))=id;

 

Edit :  If some ids have leading blanks :

 

substr(new_id,9-length(strip(id)))=strip(id);

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 7 replies
  • 2059 views
  • 10 likes
  • 6 in conversation