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!
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.
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
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.
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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.