10-20-2017 03:53 PM - edited 10-20-2017 03:55 PM
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?
10-20-2017 03:57 PM
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.
10-20-2017 04:20 PM
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
10-20-2017 05:59 PM
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.
10-21-2017 02:54 AM
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.