Monday, April 12, 2010

Convert a Number to a String With Leading Zeros

Suppose you have a list of numbers and want to convert them to a string with leading zeros. Here is the SAS code which does that:

data demo;
input a;

/* convert a number to a 7-character long string*/
b1 = put(a, z7.);

/*use compress function to handle missing values*/
b2 = compress(b1,'.');

/* combine two statements together */
b = compress(put(a, z7.), '.');

datalines;
1234
876
.
; run;