BookmarkSubscribeRSS Feed
PVSCBS
Calcite | Level 5

Hi all,

I writing a code to import several .dta files available in a same folder named INDyyyy, with yyyy corresponding to the year. However I am running into problems because SAS does not recognize the PROC IMPORT command inside a macro.

 

I can successfully import each of them individually using the following code:

 

PROC IMPORT DATAFILE = "X:\Rawdata\IND1998.dta"
OUT = WORK.IND1998
DMS=STATA replace;
RUN;

The natural step forward is to write a macro and loop over the years:

 

%Macro prep;

     %do i=1998 %to 2015

     PROC IMPORT DATAFILE = "X:\Rawdata\IND&i.dta"
          OUT = WORK.IND&i
          DBMS=STATA replace;
          RUN;
     %end
%mend; 
%prep;

The problem is that the PROC IMPORT command is not recognized inside of the macro. As you can seen in the screenshot below it is written in black, but when outside it is totally fine.

PVSCBS_1-1661867837801.png

 

I don't understand why this is happening as it seems something similar has been used by several users before, see for example this post. I also know that the problem is not related to the %do loop because I have removed it from the code and nothing changes.

 

Any idea as to why this is happening? Would you have any other suggestions as far as procedures to import such files?

 

Thanks in advance for the help

 

 

3 REPLIES 3
Reeza
Super User

You're missing semicolons at the end of the %DO/%END loop.

ballardw
Super User

If you read your log when you run that I will bet you see at least one "File not found" type error message.

The file name you provide

"X:\Rawdata\IND&i.dta"

uses the period to delimit the end of the macro variable &i so is generating names like Ind88dta without the period.

You need to add another period so there is one for the file name

"X:\Rawdata\IND&i..dta"

You do read your logs don't you?

 

Note: many problems with macro coding errors can be seen by setting the system options MPRINT , to show actual generated code, and SYMBOLGEN , to trace construction/use of macro variables.

So try running:

 

options mprint;

before executing the  macro and then reading the log to see error messages in better proximity to the generated code.

Tom
Super User Tom
Super User

You have two mistakes. 

 

The main one is that you are not including the period in the generated filename.   The period you have is being used by the macro processor so that it knows that the macro variable is named I and IDTA.  So you need another one to become part of the filename.

 

The other are missing semi-colons at the end of two of your macro statements.  Macro statements end with semi-colons just like regular SAS statements.

 

%macro prep;
%local i;
%do i=1998 %to 2015;
PROC IMPORT DATAFILE = "X:\Rawdata\IND&i..dta"
          OUT = WORK.IND&i
          DBMS=STATA replace
;
RUN;
%end;
%mend; 
%prep;

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!

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
  • 3 replies
  • 445 views
  • 0 likes
  • 4 in conversation