BookmarkSubscribeRSS Feed
marleeakerson
Calcite | Level 5

Hello, 

 

I need some help with important an excel document where some observations have leading zeros and some do not. For example, take the following sample dataset: 

ID         Variable

1               002

2               617

3               012

4               G317

 

How do I import this into sas while keeping the leading zeros? 

 

Thank you!

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Are you using PROC IMPORT? It guesses, and sometimes it guesses wrong, and thus you can't specifically control the import and have it always behave the way you want.

 

If you can convert the Excel file to .csv, then you can read it in using custom data step code, and force this column to be character and then the leading zeros will be retained.

--
Paige Miller
ballardw
Super User

Have you tried reading the data already? If you have values like G317 as shown I would expect Proc Import to read the column as text. You may want to use the SCANTEXT option to make sure the length set holds all the values.

 

Or how did you expect to read the file?

andreas_lds
Jade | Level 19

I may depend on how the column "variable" is defined in excel. If it is configured to be text, proc import reads it as text retaining the leading zeros. 

 

EDIT: Forgot to mention: it is really time to post the code you have used to read the file.

Tom
Super User Tom
Super User

If you are really using PROC IMPORT to read from an XLSX (or even an XLS) file then SAS will NOT remove the leading zeros.  If they are gone then that was caused in Excel.  Most likely the cells with the missing leading zeros have mistakenly been created a numeric values in the Excel sheet instead of the strings that they actually are.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 579 views
  • 0 likes
  • 6 in conversation