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

I am trying to read in all CSV files within a directory (I am using this page as a guide for my code: SAS Help Center: Example 1: Import All CSV Files That Exist within a Directory). Below is the code I am running and the errors I am getting. Can someone help me resolve these errors?

 

 

%macro retrieve(dir,ext);
	%local cnt infile rc did memcnt name;
	%let cnt=0;

	%let infile=S:\Projects\Data\Annual_Submissions\2019\Raw\State\City\Original downloads;
	%let rc=%sysfunc(filename(infile,&dir));
	%let did=%sysfunc(dopen(&infile));
 	  %if &did ne 0 %then do;
	%let memcnt=%sysfunc(dnum(&did));

	  %do i=1 %to &memcnt;

	    %let name=%qscan(%qsysfunc(dread(&did,&i)),-1,.);

		  %if %qupcase(%qsysfunc(dread(&did,&i))) ne %qupcase(&name) %then %do;
      	    %if %superq(ext) = %superq(name) %then %do;      
				%let cnt=%eval(&cnt+1);
				%put %qsysfunc(dread(&did,&i));
				proc import datafile="&&infile\%qsysfunc(dread(&did,&i))" out=tx&cnt 
					dbms=csv replace;
					guessingrows=10000; getnames=yes;
				run;
			%end;
		   %end;

		  %end;
		    %end;
		%else %put &dir cannot be opened.;
  		%let rc=%sysfunc(dclose(&did)); 

%mend retrieve;

The errors I am getting:

ERROR: There is no matching %DO statement for the %END. This statement will be ignored.
612          %else %put &infile cannot be opened.;
ERROR: There is no matching %IF statement for the %ELSE.
ERROR: A dummy macro will be compiled.
613          %let rc=%sysfunc(dclose(&did));

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Are these files all of the same structure? If so you will be WAY better off writing a data step to read them so the results are consistent instead of Proc Import which make guesses for each file about length and type of variable.

One might suspect from the path you show the data contains text about cities. If each file represents a different "city" then it is extremely likely that the length of the city variable will differ a lot between files and if you intend to combine any of these you will be spending time "fixing" things that should be the same.

 

Best would be to have a document from the source that describes the column contents so you can set the length and type.

 

When you delve into macro programming you will want to learn to use the options Mprint and possiblly Mlogic and Symbolgen to display the code generated and so the ERRORS end up in better relation to your code.

 

Start with

%let infile=S:\Projects\Data\Annual_Submissions\2019\Raw\State\City\Original downloads;
	%let rc=%sysfunc(filename(infile,&dir));
	%let did=%sysfunc(dopen(&infile));
 	  %if &did ne 0 %then do;
	%let memcnt=%sysfunc(dnum(&did));

 That hightlighted "do" should be "%do".

View solution in original post

4 REPLIES 4
ballardw
Super User

Are these files all of the same structure? If so you will be WAY better off writing a data step to read them so the results are consistent instead of Proc Import which make guesses for each file about length and type of variable.

One might suspect from the path you show the data contains text about cities. If each file represents a different "city" then it is extremely likely that the length of the city variable will differ a lot between files and if you intend to combine any of these you will be spending time "fixing" things that should be the same.

 

Best would be to have a document from the source that describes the column contents so you can set the length and type.

 

When you delve into macro programming you will want to learn to use the options Mprint and possiblly Mlogic and Symbolgen to display the code generated and so the ERRORS end up in better relation to your code.

 

Start with

%let infile=S:\Projects\Data\Annual_Submissions\2019\Raw\State\City\Original downloads;
	%let rc=%sysfunc(filename(infile,&dir));
	%let did=%sysfunc(dopen(&infile));
 	  %if &did ne 0 %then do;
	%let memcnt=%sysfunc(dnum(&did));

 That hightlighted "do" should be "%do".

raivester
Quartz | Level 8

Thank you this seemed to have resolved those errors! However, when I run

%retrieve(&fileref.,csv);

I now get:

NOTE: In a call to the DOPEN routine, the fileref
S:\Projects\Data\Annual_Submissions\2019\Raw\State\City\Original downloads
exceeds 8 characters, and will be truncated.
S:\Projects\Data\Annual_Submissions\2019\Raw\State\City\Original downloads cannot
be opened.
raivester
Quartz | Level 8

Actually, I'm using 

%retrieve(&infile.,csv);

Apologies.

ballardw
Super User

Please examine these two lines of code:

	%let rc=%sysfunc(filename(infile,&dir));
	%let did=%sysfunc(dopen(&infile));

You created a fileref name INFILE then used the macro variable &INFILE, not the fileref.

 

 

SAS Innovate 2025: Call for Content

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 766 views
  • 0 likes
  • 2 in conversation