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

Hello:

 

I would like to write a macro to representing my files name because the program is needed to run many times.  However, the system showed syntax error.  Please help.  Thanks.

 

%let libname=RF;
%let filename=NG;
%let year=2013;
%let number=02;

% X1=wage;

% Y1=salary;

data &libname.&filename&year(&number+2);
set &libname.&filename&year&number;
if &Y1 in (1,2) and &X1 in (1,2,5,6);
run;

 

51 data &libname.&filename&year(&number+2);
-
200
ERROR 200-322: The symbol is not recognized and will be ignored.

 

52 set &libname.&filename&year&number;
- - -
22 200 200
200
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, ;, CUROBS, END,
INDSNAME, KEY, KEYRESET, KEYS, NOBS, OPEN, POINT, _DATA_, _LAST_, _NULL_.

ERROR 200-322: The symbol is not recognized and will be ignored.

 

WARNING: Apparent symbolic reference LIBNAME not resolved.
WARNING: Apparent symbolic reference FILENAME not resolved.
WARNING: Apparent symbolic reference YEAR not resolved.
WARNING: Apparent symbolic reference NUMBER not resolved.
ERROR: File WORK.FILENAME.DATA does not exist.
ERROR: File WORK.YEAR.DATA does not exist.
ERROR: File WORK.NUMBER.DATA does not exist.
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, (, -, :, ;,
CUROBS, END, INDSNAME, KEY, KEYRESET, KEYS, NOBS, OPEN, POINT, _DATA_, _LAST_, _NULL_.

 

1 ACCEPTED SOLUTION

Accepted Solutions
LaurieF
Barite | Level 11

There are few problems I can see here. Does this code work better?

%let libname=RF;
%let filename=NG;
%let year=2013;
%let number=02;
%let X1=wage;
%let Y1=salary;
data &libname..&filename&year%eval(&number+2);
set &libname..&filename&year&number;
if &Y1 in (1,2) and &X1 in (1,2,5,6);
run;

You missed out the %let commands for wage and salary.

When you have a macro variable that must be suffixed with a full-stop, you need two of them: &libname..

To make sure that number has 2 added to it, you need to use %eval.

View solution in original post

5 REPLIES 5
LaurieF
Barite | Level 11

There are few problems I can see here. Does this code work better?

%let libname=RF;
%let filename=NG;
%let year=2013;
%let number=02;
%let X1=wage;
%let Y1=salary;
data &libname..&filename&year%eval(&number+2);
set &libname..&filename&year&number;
if &Y1 in (1,2) and &X1 in (1,2,5,6);
run;

You missed out the %let commands for wage and salary.

When you have a macro variable that must be suffixed with a full-stop, you need two of them: &libname..

To make sure that number has 2 added to it, you need to use  %eval.

DanielSantos
Barite | Level 11

Hi.

 

If you are using frequently that kind of dataset naming, maybe you would benefit by creating a macro "function" like this:

 

%let libname=RF;
%let filename=NG;
%let year=2013;
%let number=02;
%let X1=wage;
%let Y1=salary;

%macro mFilename(mLIBNAME=&LIBNAME,mFILENAME=&FILENAME,mYEAR=&YEAR,mNUMBER=&NUMBER);
&mLIBNAME%str(.)&mFILENAME&mYEAR%sysfunc(putn(&mNUMBER,z2.))
%mend mFilename; * return my dataset name;

data %mFilename(mNUMBER=%eval(&NUMBER+2));
set %mFilename();;
if &Y1 in (1,2) and &X1 in (1,2,5,6);
run;

Macro mFilename will build and return the name for you.

 

For convenience It's using named parameters and defaulting to macro vars LIBNAME, FILENAME, YEAR and NUMBER.

 

Hope it helps.

 

Daniel Santos @ www.cgd.pt

LaurieF
Barite | Level 11

There are few problems I can see here. Does this code work better?

%let libname=RF;
%let filename=NG;
%let year=2013;
%let number=02;
%let X1=wage;
%let Y1=salary;
data &libname..&filename&year%eval(&number+2);
set &libname..&filename&year&number;
if &Y1 in (1,2) and &X1 in (1,2,5,6);
run;

You missed out the %let commands for wage and salary.

When you have a macro variable that must be suffixed with a full-stop, you need two of them: &libname..

To make sure that number has 2 added to it, you need to use %eval.

ybz12003
Rhodochrosite | Level 12

Actually, the following is my macro codes.  However, after I ran it, I found another syntax again under %macro.   Please see below.  How to fix it?

 

%let libname=RF;
%let filename=NG;
%let year=2013;
%let number=02;
%let X1=wage;
%let Y1=salary;
%macro chisqrun2 (&X1, &Y1);

ERROR: Invalid macro parameter name &. It should be a valid SAS identifier no longer than 32
characters.
ERROR: A dummy macro will be compiled.


proc freq data=&libname..&filename&year&number;
table &X1 * &Y1 / chisq;
run;
%mend;

 

data &libname..&filename&year%eval(&number+2);;
set &libname..&filename&year&number;
if &Y1 in (1,2) and &X1 in (1,2,5,6);
run;
%chisqrun2 (&X1,&Y1);
quit;

ballardw
Super User

%macro chisqrun2 (&X1, &Y1);

is the start of a macro definition. As such the processor really does not want to compile a macro that it does not know the name of the VARIABLES that it well be using.

the definitions should use

%macro chisqrun2 (X1, Y1);

so that the first position will be referenced inside the macro as &x1 and the second as &y1.

 

 

 

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 4195 views
  • 0 likes
  • 4 in conversation