DATA Step, Macro, Functions and more

Padding a text variable with leading 0s [SOLVED, MACROISED SOLUTION INSIDE]

Reply
N/A
Posts: 0

Padding a text variable with leading 0s [SOLVED, MACROISED SOLUTION INSIDE]

I'm in the final stages of a data standardisation effort here at work.
There are two data columns, each being of type CHAR. They mostly contain numeric IDs, but some contain letters in the middle of the number strings as well. What I wish to do is ensure that all of the variables contain 10 characters, and if they contain less than that, pad out the left-hand-side with 0s.
If the variables were guaranteed to be wholly numeric I would simply convert to a number and then use PUT with the Zw. format, but that isn't applicable here.

Any suggestions?
N/A
Posts: 0

Re: Padding a text variable with leading 0s

Posted in reply to deleted_user
Nevermind - I discovered the REPEAT function.

Here's a solution in macro format, to stop this topic being a waste of space...
%macro fillfromleftwithzero(table,variable,varlen);
data &table.;
set &table.;
&variable. = REPEAT("0",&varlen.-length(&variable.)) || &variable.;
run;
%mend; /*fillfromleftwithzero*/
Ask a Question
Discussion stats
  • 1 reply
  • 113 views
  • 0 likes
  • 1 in conversation