I've got a pretty simple macro to take values from a list of strings and use them to name datasets. I use the variable "yearcounter" represent the actual value of where we are in the list. When I try to create a dataset name with it, I get the error
NOTE: Line generated by the macro variable "YEARCOUNTER".
work.year_1998
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, (, /, ;, _DATA_, _LAST_, _NULL_.
ERROR 200-322: The symbol is not recognized and will be ignored.
So it's placing the year in the right spot, but it's not interpreting the whole line of code. If I replace the [yearcounter] variable with [i], it runs fine with no problems, but then I get datasets with names like year_1, year_2, and so forth. Any idea what's going on?
data yearlist;
input term;
datalines;
1998
1999
2000
2001
2002
2003
2004
2005
2006;
run;
proc sql;
select year into :Yearlist separated by ' '
from yearlist;
quit;
%macro classes;
%do i = 1 %to %sysfunc(countw(&Yearlist,%str( )));
%let yearcounter = %qscan(&Yearlist,&i,%str( ));
data work.year_&yearcounter;
set otherdataset;
[dataset operations]
run;
%end;
%mend;
%classes;
The most likely culprit is %qscan. When macro language quotes a string, it actually inserts a character into the text. Usually (but evidently not in this case), SAS figures out to remove that character when generating SAS code. The problem usually occurs when using SQL, but here is appears to be happening even in a DATA step. Just switch from %QSCAN to %SCAN.
If that fails, make sure the extra character is gone by removing it yourself:
data work.%unquote(year_&yearcounter);
Suspect this is just a typo for the example, but in your proc sql step you select a variable called YEAR but in the prior dataset the variable is called TERM.
Fixing this and changing QSCAN to SCAN allows the code to work correctly.
data yearlist;
input term;
datalines;
1998
1999
2000
2001
2002
2003
2004
2005
2006
;
run;
proc sql;
select term into :Yearlist separated by ' ' from yearlist;
quit;
%macro classes;
%do i=1 %to %sysfunc(countw(&Yearlist, %str( )));
%let yearcounter = %scan(&Yearlist, &i, %str( ));
data work.year_&yearcounter;
set sashelp.class;
run;
%end;
%mend;
%classes;
Note that if your years are consecutive and you know the start/end you could just use those in the loop instead of creating a macro variable list.
%do yr=&startYear %to &endYear;
data year_&yr.;
....
run;
%end;
@sasuser1444 wrote:
I've got a pretty simple macro to take values from a list of strings and use them to name datasets. I use the variable "yearcounter" represent the actual value of where we are in the list. When I try to create a dataset name with it, I get the error
NOTE: Line generated by the macro variable "YEARCOUNTER". work.year_1998 ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, (, /, ;, _DATA_, _LAST_, _NULL_. ERROR 200-322: The symbol is not recognized and will be ignored.
So it's placing the year in the right spot, but it's not interpreting the whole line of code. If I replace the [yearcounter] variable with [i], it runs fine with no problems, but then I get datasets with names like year_1, year_2, and so forth. Any idea what's going on?
data yearlist; input term; datalines; 1998 1999 2000 2001 2002 2003 2004 2005 2006; run; proc sql; select year into :Yearlist separated by ' ' from yearlist; quit; %macro classes; %do i = 1 %to %sysfunc(countw(&Yearlist,%str( ))); %let yearcounter = %qscan(&Yearlist,&i,%str( )); data work.year_&yearcounter; set otherdataset; [dataset operations] run; %end; %mend; %classes;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.