BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ronein
Meteorite | Level 14

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.*/

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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. 

View solution in original post

7 REPLIES 7
Patrick
Opal | Level 21

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. 

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Ronein
Meteorite | Level 14

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;
PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
Ronein
Meteorite | Level 14
I ask theortically if it will be fine after reading your inputs
PaigeMiller
Diamond | Level 26

Some things you can find out for yourself.

--
Paige Miller
Ronein
Meteorite | Level 14

I have no SAS now but i will have a look when I have SAS available,cheers

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!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 993 views
  • 3 likes
  • 3 in conversation