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
Solved

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

Posted on 2011-02-24
5
11,258 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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
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…

766 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