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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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