Solved

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

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle function works in 11g but not in 12c 21 72
oracle query help 18 99
Trigger usage 2 59
add and run outdated exchange database to an Exchange server 1 11
Database tuning – How to start and what to tune. This question is frequently asked by many people, both online and offline. There is no hard and fast rule-of-thumb for performance tuning, however, before beginning the tuning process one should a…
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
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…

867 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

15 Experts available now in Live!

Get 1:1 Help Now