How to add leading zero

Posts: 27

How to add leading zero

[ Edited ]



I am trying to read data from a csv file and want to add the leading zero to the values w. fewer than three digits (character) For instance, 95 should be 095, 14 should be 014, 215 should be 215 etc. I tried formatting the column w. custom (000) , however when I tried saving the csv file it didn't get saved. Can someone help me how to do this in SAS? 


Thank you!

Super User
Posts: 23,343

Re: How to add leading zero

Are you reading data or exporting data? And when exporting, make sure you look at the text file, not the csv in Excel. Excel will strip leading zeros.


Post your code as well please with an example of the issue.

Posts: 27

Re: How to add leading zero

Thank you for your reply. I am trying to read data (csv.) into SAS. I want to create a few age-related variables using the existing age variable (tAge is the existing age variable)


New_Age=input(substr (tAge, 2,2),2.); 

Age_Level = substr(tAge, 1,1);


Without leading zero, the calculation is off for the values w/ fewer than three digits. For instance:


if tAge=525 then Age_Level=5 and New_Age=25 

If tAge=402 then Age_Level=4 and New_Age=2 


if tAge=95 then I want to add leading zero (095) so New_Age returns 95 and Age_Level returns 0 



Super User
Posts: 13,358

Re: How to add leading zero



newage = mod(tAge,100);

Age_level = int(tAge/100);


Sometimes this type of "need" indicates that a variable was read incorrectly. I suspect that your source data may have had leading zeroes and however that was read converted them to numeric which will not have leading 0 for values greater than 1.


Super User
Posts: 9,925

Re: How to add leading zero

To display a number with leading zeroes, use the Z. format. To store numbers with leading zeroes, you have to put them into character variables.

Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 4 replies
  • 4 in conversation