I am using the following code to import an Excel file.
proc import
datafile="&dir.fileA"
DBMS=xlsx out=PARAM replace;
getnames=YES; SHEET='PARAM';
format EndDate ddmmyy10.;
run;
column EndDate is sometimes empty. I need to have it in the sas dataset as date format. when it is empty it is imported as Char and of course I get the following error -
ERROR: You are trying to use the numeric format DDMMYY with the character variable EndDate
Is there a way to import it as date that will work even when the column is empty?
Thank you.
Sorry, but this is not possible, because proc import has to rely on the content of the excel file to define types and lengths. So, if there is nothing in a column proc import (and libname) can't be persuaded to set the type of the column to numeric an apply a date format.
The goods news: the issue is caused by using Excel as data source. If you switch to csv and write the importing step yourself, you won't have problems like this at all.
Thank you. As I need to import this as Excel, and need this col to bo a date col, I ended up adding the following code to change that col to date when it is empty. It seems to work.
data PARAM_1(drop = EndDate2);
format EndDate ddmmyy10.;
set PARAM (rename=(EndDate=EndDate2));
if EndDate2 eq "" then EndDate=input(EndDate2,ddmmyy10.);
else EndDate= EndDate2;
run;
@Taliah wrote:
As I need to import this as Excel
You don't. Any sheet in a .xlsx file can be saved as csv, and you don't even need Excel to do it. Other software like LibreOffice can even do it from the commandline.
In all my professional SAS life, I never imported Excel files in production jobs. Data was always transported via text files, either csv (comma- or semicolon-separated) or with fixed column lengths. If I received Excel files where the data had to align with existing datasets, I also saved the data to text files and read those with data steps, to avoid all the hassle.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: