BookmarkSubscribeRSS Feed
sasuser1444
New User | Level 1

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;

 

3 REPLIES 3
Astounding
PROC Star

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);

 

Reeza
Super User

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;

 


 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 115 views
  • 3 likes
  • 4 in conversation