Solved

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

Posted on 2011-02-24
5
10,325 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
  • 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 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Often, when running a query with joins, the results show up "duplicates", and often, those duplicates can be "eliminated" in the results using DISTINCT, for example. Using DISTINCT is simple: just add it after the SELECT keyword, an…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now