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.
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
You're missing semicolons at the end of the %DO/%END loop.
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.
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.