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

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 644 views
  • 0 likes
  • 1 in conversation