macro errors

Reply
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);
%end;
%do i = 1 %to 50;
input rank city $ popul;
output;
keep i state;
%end;
%Mend create_datasets ;

run;


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_,
_NULL_.

run;

proc print data = state_data;

ERROR: FILE WORK.STATE_ DATA.DATA does not exist

title " Cities ";

run;


*%print create_datasets(numstates=50)

run;
quit;

Super User
Posts: 19,038

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: 5,353

Re: macro errors

Tracy,

 

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.

SAS Super FREQ
Posts: 8,815

Re: macro errors

Hi:
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:
http://www2.sas.com/proceedings/sugi28/056-28.pdf
and
https://support.sas.com/resources/papers/proceedings13/120-2013.pdf
cynthia
Ask a Question
Discussion stats
  • 3 replies
  • 325 views
  • 2 likes
  • 4 in conversation