How to Import Multiple Excel Files into a Single SAS Dataset

Deepanshu Bhalla 27 Comments

Suppose you wish to import multiple Excel workbooks, each with the same variable names, from a folder into a SAS library. Afterward, you want to merge the data from all the imported datasets into a single SAS dataset (table).

SAS Macro
%macro MultImp(dir=,out=);

data _null_;
length fref $8 fname $200;
rc = filename(fref,"&dir.");
did = dopen(fref);
out="&out";
if did ne 0
then do;
  do i = 1 to dnum(did);
    fname = catx("/","&dir.",dread(did,i));
    if lowcase(scan(fname,-1,'.')) = 'xlsx' then call execute('
    proc import
  datafile="'||fname||'"
  out=temp
  dbms=xlsx
  replace
;
run;

proc append
  data=temp
  base='||out||'
  force
;
run;
');
  end;
  rc = dclose(did);
end;
rc = filename(fref);
run;

%mend;

%MultImp(dir=/home/deepanshu88us0/MergeFolder,out=merged);
How to use the macro:
  1. Paste the above program into SAS program editor window.
  2. Specify the folder location in the 'dir=' argument and name of the output dataset in the 'out=' argument of the macro. %MultImp(dir=/home/deepanshu88us0/MergeFolder,out=merged);
  3. Run the program
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

27 Responses to "How to Import Multiple Excel Files into a Single SAS Dataset"
  1. Followed as suggested above but did not get my files imported. getting below messages in log..
    WARNING: Argument 1 to function DNUM referenced by the %SYSFUNC or %QSYSFUNC macro function is out of
    range.
    NOTE: Mathematical operations could not be performed during %SYSFUNC function execution. The result
    of the operations have been set to a missing value.

    ReplyDelete
    Replies
    1. Thank you for letting me know the issue. I have fixed the code. It is working fine now!

      Delete
    2. Why these Errors are comming in SAS studio given below:

      ERROR: Insufficient authorization to access PIPE.
      ERROR: Error in the FILENAME statement.

      Delete
  2. Hi, Can you update a article on basic introduction to sas macros?

    ReplyDelete
  3. Please start with introduction to macros.

    ReplyDelete
  4. How can we use the code when some variables may be different (or some files have some variables that others don't have).
    Thanks

    ReplyDelete
  5. Can you update the code to only include the first 5 observations (first 5 files) in the folder and start with the second line of the excel files?

    ReplyDelete
  6. dbms=xlsx should be dbms=excel in my case.

    ReplyDelete
  7. Thanks for providing this useful guide on importing excel files. It's really very useful. Also, I added your blog to my favourite sites list. Keep sharing.

    ReplyDelete
  8. In one of the interview, i was faced the below question, plz help and give me the code:
    example :in daily reports-
    if we have three excel files,
    in each excel - today data is available in two sheets , tomorrow data may available in three sheets .
    so write a code to append three excels into one excel file , in each excel- data available in sheets may vary day by day but columns/variable names structure is same.

    ReplyDelete
  9. The code successfully imports all 13 Excel files but doesn't generate a data set of the all the observations. The output file reflects only the observations in the last imported file. Please advise.

    ReplyDelete
  10. How could this be done with 14 text delimited files instead of Excel files?

    ReplyDelete
  11. OTE: The infile MYFILES is:
    Unnamed Pipe Access Device,

    PROCESS=dir "T:\D33Shr\SYSFCST\load forecasting
    EU\Test\Sandbox\PepcoDCAMonthlySummaryReagan\" /A-D/B/ON,
    RECFM=V,LRECL=32767

    Stderr output:
    The system cannot find the file specified.
    NOTE: 0 records were read from the infile MYFILES.
    NOTE: The data set WORK.LIST has 0 observations and 2 variables.
    NOTE: DATA statement used (Total process time):
    real time 0.14 seconds
    cpu time 0.03 seconds

    ReplyDelete
  12. OTE: The infile MYFILES is:
    Unnamed Pipe Access Device,

    PROCESS=dir "T:\D33Shr\SYSFCST\load forecasting
    EU\Test\Sandbox\PepcoDCAMonthlySummaryReagan\" /A-D/B/ON,
    RECFM=V,LRECL=32767

    Stderr output:
    The system cannot find the file specified.
    NOTE: 0 records were read from the infile MYFILES.
    NOTE: The data set WORK.LIST has 0 observations and 2 variables.
    NOTE: DATA statement used (Total process time):
    real time 0.14 seconds
    cpu time 0.03 seconds

    ReplyDelete
  13. Hello, I followed above suggestion and get this error, could you please help check? Thanks.

    ERROR: Insufficient authorization to access PIPE.
    ERROR: Error in the FILENAME statement.

    By The way, my file is saved in Linux, not in the local c drive.

    ReplyDelete
  14. Trying to import 23 Excel files but no log errors or no output

    ReplyDelete
  15. By using this code, it keeps adding new lines each time I run, instead of replacing the actual content.
    Is there any way to replace instead of always re-adding the same info as I run? ie.: Delete all data and add the new set of data from updated files in folder.

    ReplyDelete
Next → ← Prev