Suppose you want to import multiple excel workbooks with the same variable names from a folder into a library and then merge data from all the data sets to a single data set (table).
%macro MultImp(dir=,out=);
%let rc=%str(%'dir %")&dir.%str(\%" /A-D/B/ON%');
filename myfiles pipe %unquote(&rc);
data list;
length fname $256.;
infile myfiles truncover;
input myfiles $100.;
fname=quote(upcase(cats("&dir",'\',myfiles)));
out="&out";
drop myfiles;
call execute('
proc import dbms=xlsx out= _test
datafile= '||fname||' replace ;
run;
proc append data=_test base='||out||' force; run;
proc delete data=_test; run;
');
run;
filename myfiles clear;
%mend;
%MultImp(dir=C:\Users\Deepanshu Bhalla\Documents\test,out=merged);
How to use :
1. Paste the above program into SAS program editor window
2. Change the path mentioned in the last line of program (highlighted below in red)
%MultImp(dir=C:\Users\Deepanshu Bhalla\Documents\test,out=merged);
3. Run the program
Followed as suggested above but did not get my files imported. getting below messages in log..
ReplyDeleteWARNING: 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.
Thank you for letting me know the issue. I have fixed the code. It is working fine now!
DeleteWhy these Errors are comming in SAS studio given below:
DeleteERROR: Insufficient authorization to access PIPE.
ERROR: Error in the FILENAME statement.
Great Work!!
ReplyDeleteHi, Can you update a article on basic introduction to sas macros?
ReplyDeletePlease start with introduction to macros.
ReplyDeleteHow can we use the code when some variables may be different (or some files have some variables that others don't have).
ReplyDeleteThanks
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?
ReplyDeleteAwesome!!!!
ReplyDeleteVery Nice! Awesome!
ReplyDeletedbms=xlsx should be dbms=excel in my case.
ReplyDeleteThanks 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.
ReplyDeleteits not working sir
ReplyDeleteerror coming
ReplyDeleteWhat error you are getting?
Deletenow its working sir
ReplyDeleteGreat!
DeleteIn one of the interview, i was faced the below question, plz help and give me the code:
ReplyDeleteexample :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.
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.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHow could this be done with 14 text delimited files instead of Excel files?
ReplyDeleteOTE: The infile MYFILES is:
ReplyDeleteUnnamed 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
OTE: The infile MYFILES is:
ReplyDeleteUnnamed 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
Hello, I followed above suggestion and get this error, could you please help check? Thanks.
ReplyDeleteERROR: Insufficient authorization to access PIPE.
ERROR: Error in the FILENAME statement.
Hello, I followed above suggestion and get this error, could you please help check? Thanks.
ReplyDeleteERROR: 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.
Trying to import 23 Excel files but no log errors or no output
ReplyDelete