DATA Step, Macro, Functions and more

Adding value to Do Loop Variable in a Macro

Accepted Solution Solved
Reply
Contributor
Posts: 27
Accepted Solution

Adding value to Do Loop Variable in a Macro

I'm trying to add a value to do loop variable within a macro. However it keeps failing. Here is my code:

 

%macro base (yr);

 

%do i=10 %to &yr;

 

proc sort data=lib.den(&i+1).pzr out=ID_20(&i+1);by unique_id;run;
proc sort data=lib.den&i.pzr out=den_20&i;by unique_id;run; 

 

%end;

 

%mend;

 

%base (14); 

 

All the errors appear to be around the parenthesis

 

The error I get is this:

 

ERROR 22-322: Syntax error, expecting one of the following: ;, ASCII, BUFFNO, DANISH, DATA, DATECOPY, DETAILS, DIAG, DUPOUT,
EBCDIC, EQUALS, FINNISH, FORCE, IN, ISA, L, LEAVE, LIST, MESSAGE, MSG, NATIONAL, NODUP, NODUPKEY, NODUPKEYS,
NODUPLICATE, NODUPLICATES, NODUPREC, NODUPRECS, NODUPS, NOEQUALS, NORWEGIAN, NOTHREADS, NOUNIKEY, NOUNIKEYS,
NOUNIQUEKEY, NOUNIQUEKEYS, NOUNIQUEREC, NOUNIQUERECS, NOUNIREC, NOUNIRECS, OSA, OUT, OVERWRITE, PAGESIZE, PRESORTED,
PSIZE, REVERSE, SIZE, SORTSEQ, SORTSIZE, SORTWKNO, SWEDISH, T, TAGSORT, TECH, TECHNIQUE, TESTHSI, THREADS, UNIOUT,

 

I'm trying to grab the current year of data I'm intersted in and the next years of data. Thanks for any insight! 


Accepted Solutions
Solution
‎01-20-2017 04:42 PM
Super User
Posts: 19,792

Re: Adding value to Do Loop Variable in a Macro

Posted in reply to endofline

Your code:

lib.den(&i+1).pzr

 

Let's assume i=1, then this will resolve to:

 

lib.den(1+1).pzr 

 

%let i=1;

%put lib.den(&i+1).pzr;

RESULTS:

348759 %let i=1;
348760
348761 %put lib.den(&i+1).pzr;
SYMBOLGEN: Macro variable I resolves to 1
lib.den(1+1).pzr

 

This is not valid SAS code.

 

Try removing the parenthesis, use %eval with brackets or create a new macro variable outside and use that.

%let i=1;

%put lib.den%eval(&i+1).pzr;

Results:

 

348762  %let i=1;
348763
348764  %put lib.den%eval(&i+1).pzr;
SYMBOLGEN:  Macro variable I resolves to 1
lib.den2.pzr

 

Probably another period there that's causing issues, but I think you should have the idea now. 

View solution in original post


All Replies
Solution
‎01-20-2017 04:42 PM
Super User
Posts: 19,792

Re: Adding value to Do Loop Variable in a Macro

Posted in reply to endofline

Your code:

lib.den(&i+1).pzr

 

Let's assume i=1, then this will resolve to:

 

lib.den(1+1).pzr 

 

%let i=1;

%put lib.den(&i+1).pzr;

RESULTS:

348759 %let i=1;
348760
348761 %put lib.den(&i+1).pzr;
SYMBOLGEN: Macro variable I resolves to 1
lib.den(1+1).pzr

 

This is not valid SAS code.

 

Try removing the parenthesis, use %eval with brackets or create a new macro variable outside and use that.

%let i=1;

%put lib.den%eval(&i+1).pzr;

Results:

 

348762  %let i=1;
348763
348764  %put lib.den%eval(&i+1).pzr;
SYMBOLGEN:  Macro variable I resolves to 1
lib.den2.pzr

 

Probably another period there that's causing issues, but I think you should have the idea now. 

Contributor
Posts: 27

Re: Adding value to Do Loop Variable in a Macro

Thank you! I couldn't remember to put the %eval. Once I put that it worked. I kept the period originally since I wanted to keep the pzr as it is part of the database name as in this:

 

proc sort data=lib.den&i.pzr out=den_20&i;by unique_id;run; 

 

but with the %eval statement, I no longer need it so I removed it. Thanks again! 

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 691 views
  • 0 likes
  • 2 in conversation