Help using Base SAS procedures

Macro naming?

Accepted Solution Solved
Reply
Super Contributor
Posts: 297
Accepted Solution

Macro naming?

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

 


Accepted Solutions
Solution
‎02-14-2017 01:54 PM
Super Contributor
Posts: 251

Re: Macro naming?

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


All Replies
Super Contributor
Posts: 251

Re: Macro naming?

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.

Super Contributor
Posts: 474

Re: Macro naming?

[ Edited ]

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

Solution
‎02-14-2017 01:54 PM
Super Contributor
Posts: 251

Re: Macro naming?

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.

Super Contributor
Posts: 297

Re: Macro naming?

[ Edited ]

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;

Super User
Posts: 10,460

Re: Macro naming?

%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.

 

 

 

 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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