Help using Base SAS procedures

How to Format Leading Zeros

Reply
Contributor
Posts: 24

How to Format Leading Zeros

I am working on a snippet where I am formatting SSN. In the past, the Z format worked, but it has not worked lately. I tried using picture (for example, picture ssfmt other = '999999999'Smiley Wink, but that hasn't worked as well. I am getting an error that states that the format was not found or could not be loaded.

proc format;

picture ssfmt low-high = '999999999';

run;

ID starts of with a character of 20 length. Then I do the following:

length newid $9.;

newid =idnumber;

newssn = input(newid, ssfmt.);

Any pointers?

Thanks,

Respected Advisor
Posts: 3,799

Re: How to Format Leading Zeros

Why no use the SSN format.

data _null_;
   ssn=
000090201;
  
put ssn ssn.;
  
run;

000-09-0201
Contributor
Posts: 24

Re: How to Format Leading Zeros

Posted in reply to data_null__

Thanks for the reply, but I am getting the error that the format could not be found or loaded.

Super User
Posts: 5,516

Re: How to Format Leading Zeros

Is it possible that your variable is character, but in previous applications it was numeric?

Contributor
Posts: 24

Re: How to Format Leading Zeros

Posted in reply to Astounding

It is a character variable what I am using.

Respected Advisor
Posts: 3,799

Re: How to Format Leading Zeros

If it is character then perhaps you should read it (input function) into a numeric variable then apply SSN format to the new variable.

Super User
Posts: 5,516

Re: How to Format Leading Zeros

As you noticed, SAS complains when you try to apply a numeric format (such as ssn. or z9.) to a character variable.  Since your variable is defined as 20 characters long, you can replace it with the proper value using the tools that data_null_ suggested:

idnumber = put(input(left(idnumber),9.), ssn.);

If you want to keep IDNUMBER as is, you could always assign this value to a new variable instead.

Good luck.

Contributor
Posts: 24

Re: How to Format Leading Zeros

Posted in reply to Astounding

Thank you! That worked and I did a compress to get rid of the hyphens.

Super User
Posts: 5,516

Re: How to Format Leading Zeros

Ah, if you don't want the hyphens it's just slightly different:

idnumber = put(input(left(idnumber),9.), z9.);


Contributor
Posts: 24

Re: How to Format Leading Zeros

Posted in reply to Astounding

Thank you! I'll never understand when Z works and when it doesn't.

Respected Advisor
Posts: 3,799

Re: How to Format Leading Zeros

It works when the data is NUMERIC.  Does not work for character data.

Ask a Question
Discussion stats
  • 10 replies
  • 756 views
  • 3 likes
  • 3 in conversation