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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

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