Desktop productivity for business analysts and programmers

How can I convert a character value with leading zeros to numeric and maintain the leading zeros?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 82
Accepted Solution

How can I convert a character value with leading zeros to numeric and maintain the leading zeros?

I have a data item (is SSN) that is defined with character format. The ssn does NOT have any dashes, just numbers.   I need to convert this to numeric without losing the leading zeros.  I do not see any functions that would do this.  Any suggestions on how I can accomplish this?

Thanks!


Accepted Solutions
Solution
‎03-02-2012 01:34 PM
New Contributor
Posts: 3

How can I convert a character value with leading zeros to numeric and maintain the leading zeros?

Hi, you can convert the ssn into numeric but define the format as Zw.d. eg:

data test;

x='0001451';

run;

data t1;

set test;

format y z7.;

y=x*1;

run;

View solution in original post


All Replies
PROC Star
Posts: 7,437

Re: How can I convert a character value with leading zeros to numeric and maintain the leading zeros?

Slightly more involved than I originally posted.  You have to use a mixture of put and input functions:

data have;

  input x $;

  z=put(input(x,9.),z9.);

  cards;

1

11

111

;

Contributor
Posts: 40

How can I convert a character value with leading zeros to numeric and maintain the leading zeros?

Blogged about this topic just last Friday ... check it out ... http://www.bi-notes.com/2012/02/sas-coding-adding-leading-zeros-z-format/

Solution
‎03-02-2012 01:34 PM
New Contributor
Posts: 3

How can I convert a character value with leading zeros to numeric and maintain the leading zeros?

Hi, you can convert the ssn into numeric but define the format as Zw.d. eg:

data test;

x='0001451';

run;

data t1;

set test;

format y z7.;

y=x*1;

run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 11059 views
  • 1 like
  • 4 in conversation