<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Macro not interpreting a full line of code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-not-interpreting-a-full-line-of-code/m-p/934463#M367465</link>
    <description>&lt;P&gt;The most likely culprit is %qscan.&amp;nbsp; When macro language quotes a string, it actually inserts a character into the text.&amp;nbsp; Usually (but evidently not in this case), SAS figures out to remove that character when generating SAS code.&amp;nbsp; The problem usually occurs when using SQL, but here is appears to be happening even in a DATA step.&amp;nbsp; Just switch from %QSCAN to %SCAN.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that fails, make sure the extra character is gone by removing it yourself:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.%unquote(year_&amp;amp;yearcounter);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 02 Jul 2024 20:07:36 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2024-07-02T20:07:36Z</dc:date>
    <item>
      <title>Macro not interpreting a full line of code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-not-interpreting-a-full-line-of-code/m-p/934458#M367462</link>
      <description>&lt;P&gt;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&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;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.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;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(&amp;amp;Yearlist,%str( )));
                         %let yearcounter = %qscan(&amp;amp;Yearlist,&amp;amp;i,%str( ));

           


            data work.year_&amp;amp;yearcounter;
                      set otherdataset;

                      [dataset operations]

            run;


             %end;


%mend;



%classes;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2024 19:36:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-not-interpreting-a-full-line-of-code/m-p/934458#M367462</guid>
      <dc:creator>sasuser1444</dc:creator>
      <dc:date>2024-07-02T19:36:20Z</dc:date>
    </item>
    <item>
      <title>Re: Macro not interpreting a full line of code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-not-interpreting-a-full-line-of-code/m-p/934463#M367465</link>
      <description>&lt;P&gt;The most likely culprit is %qscan.&amp;nbsp; When macro language quotes a string, it actually inserts a character into the text.&amp;nbsp; Usually (but evidently not in this case), SAS figures out to remove that character when generating SAS code.&amp;nbsp; The problem usually occurs when using SQL, but here is appears to be happening even in a DATA step.&amp;nbsp; Just switch from %QSCAN to %SCAN.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that fails, make sure the extra character is gone by removing it yourself:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.%unquote(year_&amp;amp;yearcounter);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2024 20:07:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-not-interpreting-a-full-line-of-code/m-p/934463#M367465</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2024-07-02T20:07:36Z</dc:date>
    </item>
    <item>
      <title>Re: Macro not interpreting a full line of code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-not-interpreting-a-full-line-of-code/m-p/934466#M367467</link>
      <description>&lt;P&gt;Do not use %QSCAN, use %SCAN instead.&lt;/P&gt;
&lt;HR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2024 20:03:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-not-interpreting-a-full-line-of-code/m-p/934466#M367467</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-07-02T20:03:47Z</dc:date>
    </item>
    <item>
      <title>Re: Macro not interpreting a full line of code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-not-interpreting-a-full-line-of-code/m-p/934470#M367468</link>
      <description>&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Fixing this and changing QSCAN to SCAN allows the code to work correctly.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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(&amp;amp;Yearlist, %str( )));
        %let yearcounter = %scan(&amp;amp;Yearlist, &amp;amp;i, %str( ));

        data work.year_&amp;amp;yearcounter;
            set sashelp.class;
        run;

    %end;
%mend;

%classes;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do yr=&amp;amp;startYear %to &amp;amp;endYear;

data year_&amp;amp;yr.;
....


run;

%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/466995"&gt;@sasuser1444&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;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&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;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.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;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(&amp;amp;Yearlist,%str( )));
                         %let yearcounter = %qscan(&amp;amp;Yearlist,&amp;amp;i,%str( ));

           


            data work.year_&amp;amp;yearcounter;
                      set otherdataset;

                      [dataset operations]

            run;


             %end;


%mend;



%classes;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2024 20:35:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-not-interpreting-a-full-line-of-code/m-p/934470#M367468</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2024-07-02T20:35:07Z</dc:date>
    </item>
  </channel>
</rss>

