?
Solved

How do I pad a SAS text variable with leading zeros?

Posted on 2011-02-24
5
Medium Priority
?
12,568 Views
Last Modified: 2013-11-16
Hello,

How do I pad a SAS text variable with leading zeros to the length of 10?

ex. var1 contains '123456'

Need it to be '0000123456'

Thanks,
SASnewbie
0
Comment
Question by:SASnewbie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:Aloysius Low
ID: 34970496
a quick way would be:
REVERSE(SUBSTR(TRIM(LEFT(REVERSE(text))) || "0000000000", 1, 10))

basically reverse the text and add 10 zeros to the string, then take the 1st 10 characters and reverse the string back

alternatively, if you are sure the text are all numbers, you could convert the text to numeric first, then use the z10. format:
PUT(INPUT(text, best.), z10.)
1
 

Author Comment

by:SASnewbie
ID: 34970731
Hi Iowaloysius,

Thanks for responding so quickly.

It is a text field that always contains numbers.

Would you give me the steps for the alternative method using the z10 starting with converting the text variable to a number variable?

0
 
LVL 14

Accepted Solution

by:
Aloysius Low earned 2000 total points
ID: 34970764
the code example is actually already given above.

data [your output];
  set [your input];
  padded_value = PUT(INPUT([text variable to be converted and padded], best.), z10.);
run;
0
 

Author Comment

by:SASnewbie
ID: 34970848
Thank you.
0
 

Author Comment

by:SASnewbie
ID: 34973024
Hi Iowaloysius,

This works perfectly!

data outputfilename;
  set inputfilename;
  var1= PUT(INPUT(var1, best.), z10.);
run;

Thanks,
SASnewbie
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question