2
votes

I am trying to pass numeric variables into this macro. I am able to pass it through the KEEP and SET statements, but when it gets to the RENAME statement I get these errors:

ERROR: Variable '2013'n is not on file WORK.'2013'n.

ERROR: Invalid DROP, KEEP, or RENAME option on file WORK.'2013'n.

%macro step2(year,cwyear);
    TITLE; FOOTNOTE;
    DATA WORK._EG_CFMT;
        LENGTH label $ 9;
        SET WORK."&year."n (KEEP="&year."n "&cwyear."n RENAME=("&year."n =start "&year."n =label)) END=__last;
        RETAIN fmtname "cw&year."n type "C";

        end=start;

              RUN;
    %mend step2;

When I change the double quotes around &year. to single quotes, like this:

SET WORK.'&year.'n (KEEP="&year."n "&cwyear."n RENAME=("&year."n =start "&year."n =label))

I get this error:

ERROR: File WORK.'&YEAR.'n.DATA does not exist.

When I change back to all double quotes and remove the n's, I get the following error:

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.

How do I make this work?

Thanks for the help!

1
Why do you remove the n when you change to double quotes? - Joe
because the original code has double quotes and n's and is throwing an error. I was trying to test something different than what I already know isn't working. - theponcer
The original error message is just saying that the variable doesn't exist. Are you sure you are not trying to reference the variable by its label instead of its name? - Tom

1 Answers

5
votes

You need to have double quotes AND the n. Name literals can use single or double quotes just like any other string. Single quotes won't resolve macro variables inside of them, double will.

Example:

options validvarname=any;
options validmemname=extend;

data '2015'n;
  '2015'n = 5;
run;

%macro do_something(year=);
  data work.want;
    set "&year."n(rename="&year."n = start);
  run;
%mend do_something;

%do_something(year=2015);