macro errors

New User
Posts: 1

macro errors

I am getting some errors in log report. I am using SAS 9.4. Any suggestions on how I can fix  them.


options ls=64 nodate pageno=1;

%MACRO create_datasets(num_states=);
data state_data;
libname states C:/Users/Documents/states;
%DO k = 1 %TO &num_states;

infile 'C:/Users/Documents/states' dlm=',';
length state $ 20;
length city $ 20;
input state $;
dsname = tranwrd(trim(state), ' ', '_');
call symput("name", state);
call symput("dsname", dsname);
%do i = 1 %to 50;
input rank city $ popul;
keep i state;
%Mend create_datasets ;


set state_data;


ERROR 180-322: Statement is not valid or it is used out of
proper order.

data states.&name;
ERROR 22-322: Syntax error, expecting one of the following:
a name, a quoted string, /, ;, _DATA_, _LAST_,


proc print data = state_data;


title " Cities ";


*%print create_datasets(numstates=50)


Super User
Posts: 23,675

Re: macro errors

Use MPRINT to see th values of STATE


options mprint;

Other issues I see:


1.Your libname should be outside your data step. 

2. Not sure what the first loop should be doing, doesn't seem required?

3. Second SET statement with no data step start?


The key to writing a good macro is to start off with working code and expand it from there. 

It looks like you're trying to read multiple files or datasets from a library?


Clarify what your objective is and others can make suggestions. Sometimes you don't even need a macro. 

Super User
Posts: 6,753

Re: macro errors



There are so many errors here, that the real fix is to stop writing macros.  You need more work with base SAS before you will be ready to write macros.  I will pick out just a few here.


Syntax for a libname statement requires quotes around the folder.


Libname and Infile referring to the same path, when one should be a folder and one should be a file.


Reading in POPUL when you never use it.


Repeating an INPUT statement 50 times, instead of executing the same INPUT statement 50 times.


Trying to keep "i" when it is not a variable in your data set.


Defining a macro, never executing it, and expecting the results to be available.


Get a program working without macros first.  You will have enough work to do before you start writing macros.

Posts: 9,365

Re: macro errors

Posted in reply to Astounding
I agree with all the suggestions that you start with a working SAS program before you try to "macro-ize" it. Somehow, it doesn't make sense to me that you are really trying to generate multiple INFILE and INPUT statements with your first %DO loop and 50 INPUT statements with your second %DO loop. When you put a SAS DATA step statement inside a macro %DO, you are telling the macro processor to TYPE that statement for you over, and over, and over again.

And, it's not clear what your CALL SYMPUT is supposed to be doing, or why you have the poor SET statement hanging out in open code.

I recommend getting a program working and then adding macro elements. Otherwise, this will be a nightmare to debug. And, Astounding is correct, the %MACRO/%MENT only DEFINE a macro -- they store the macro definition in the default macro catalog, you won't really see anything useful until you INVOKE the macro.

Here's some reading to get you started:
Ask a Question
Discussion stats
  • 3 replies
  • 4 in conversation