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

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! 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

2 REPLIES 2
Reeza
Super User

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. 

endofline
Obsidian | Level 7

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! 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 2 replies
  • 5189 views
  • 0 likes
  • 2 in conversation