BookmarkSubscribeRSS Feed
Kiko
Fluorite | Level 6

Hello 

 

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!

4 REPLIES 4
Reeza
Super User

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.

Kiko
Fluorite | Level 6

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 

 

 

ballardw
Super User

Try

 

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.

 

Kurt_Bremser
Super User

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1320 views
  • 0 likes
  • 4 in conversation