Solved

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

Posted on 2011-02-24
5
12,090 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 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

724 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