BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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?
1 REPLY 1
deleted_user
Not applicable
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*/

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 656 views
  • 0 likes
  • 1 in conversation