Hello
I want to import files into sas if they exists.
These files that I want to import has name structure RevenueYYYYMMDD.
It means that I enter dates in a data set and I want to import files with these names.
What is wrong with my code that I get an error
data Source&FileImport. ;
set datestbl;
filename = "/path/Revenue&FileImport..txt";
if fileexist(filename) then do;
proc import datafile ="/path/Revenue&FileImport..txt"
out=wanted&FileDate.
dbms = dlm
replace
;
delimiter = ';';
end;
run;
/*47 proc import datafile ="/path/Revenue&FileImport..txt"*/
/* _*/
/* 117*/
/**/
/*ERROR 117-185: There was 1 unclosed DO block.*/
You can't mix Data Steps and Proc Steps.
You could wrap your proc import into a SAS macro defined before the data step and then use call execute() in the data step to call the macro.
Call Execute() will stack up the macro calls and then execute them once the data step completed.
If your monthly data should always have the same structure and result in SAS tables with the same variable names AND variable attributes like lengths then I'd strongly recommend that you don't use Proc Import but an explicit INPUT statement.
You can't mix Data Steps and Proc Steps.
You could wrap your proc import into a SAS macro defined before the data step and then use call execute() in the data step to call the macro.
Call Execute() will stack up the macro calls and then execute them once the data step completed.
If your monthly data should always have the same structure and result in SAS tables with the same variable names AND variable attributes like lengths then I'd strongly recommend that you don't use Proc Import but an explicit INPUT statement.
You can use the same solution as in your earlier thread at
https://communities.sas.com/t5/SAS-Programming/proc-delete-data-set-if-exists/m-p/755250
modified appropriately to use PROC IMPORT instead of PROC DELETE.
May you please let me know if this code will work well?
%macro import_one(date);
%if%sysfunc(fileexist(path/revenue&date..csv))
%then%do;
HERE write PROC IMPORT
%end;
%mend;
Data dates_tbl;
input dates $;
cards;
20210603
20210601
20210528
20210514
;
Run;
Data _Null_;
SET dates_tbl;
callexecute('%nrstr(%import_one('!!date!!'))');
Run;
@Ronein wrote:
May you please let me know if this code will work well?
You can run it yourself and find out if it works.
Some things you can find out for yourself.
I have no SAS now but i will have a look when I have SAS available,cheers
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.