<?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: Looping inside proc sql with scan function error in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looping-inside-proc-sql-with-scan-function-error/m-p/505283#M135294</link>
    <description>&lt;P&gt;This was helpful, thank you!&amp;nbsp; I was able to identify that the issue was with the macro variable and not the sql code.&amp;nbsp; Thank you very much!&lt;/P&gt;</description>
    <pubDate>Wed, 17 Oct 2018 18:48:08 GMT</pubDate>
    <dc:creator>boyandhisrobot</dc:creator>
    <dc:date>2018-10-17T18:48:08Z</dc:date>
    <item>
      <title>Looping inside proc sql with scan function error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-inside-proc-sql-with-scan-function-error/m-p/505230#M135275</link>
      <description>&lt;P&gt;Hi, All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm looking to loop through a list of variables and create a table for each variable.&amp;nbsp; I'm getting some errors due to the scan function (I think?) and I was hoping somebody could point out where I'm going wrong. Here's my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro table_creator;
	proc sql;
		%do i=1 %to 33;

		create table 
			%scan(&amp;amp;variables.,&amp;amp;i.)_tab
		as select distinct
			%scan(&amp;amp;variables.,&amp;amp;i.),
			sum(paid_amount) as paid_amount,
                        paid_date
		from 
			add_descriptions5
		group by 
			%scan(&amp;amp;variables.,&amp;amp;i.),paid_date;
	%end;
	quit;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm receiving a series of notes stating that the tables were created, but then there's the following error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;___22&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant,&lt;BR /&gt;a missing value, *, BTRIM, INPUT, PUT, SUBSTRING, USER.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;after the %scan in my select statement.&amp;nbsp; and again after the %scan in my group by clause.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be greatly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 17 Oct 2018 17:32:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-inside-proc-sql-with-scan-function-error/m-p/505230#M135275</guid>
      <dc:creator>boyandhisrobot</dc:creator>
      <dc:date>2018-10-17T17:32:42Z</dc:date>
    </item>
    <item>
      <title>Re: Looping inside proc sql with scan function error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-inside-proc-sql-with-scan-function-error/m-p/505243#M135279</link>
      <description>&lt;P&gt;This query seems to run fine if I run it outside of a loop for just one variable.&amp;nbsp; There's something about the loop that is affecting my scan function.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql; 
	create table %scan(&amp;amp;variables.,1)
	as select distinct 
			%scan(&amp;amp;variables.,1), 		
			sum(paid_amount) as paid_amount,
			paid_date
		from 
			add_descriptions5
		group by 
			%scan(&amp;amp;variables.,1),paid_date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This seems to work fine.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Oct 2018 17:45:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-inside-proc-sql-with-scan-function-error/m-p/505243#M135279</guid>
      <dc:creator>boyandhisrobot</dc:creator>
      <dc:date>2018-10-17T17:45:16Z</dc:date>
    </item>
    <item>
      <title>Re: Looping inside proc sql with scan function error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-inside-proc-sql-with-scan-function-error/m-p/505245#M135281</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%macro table_creator;
	proc sql;
		%do i=1 %to 33;

		create table 
			%scan(%superq(variables),&amp;amp;i.)_tab
		as select distinct
			%scan(%superq(variables),&amp;amp;i.),
			sum(paid_amount) as paid_amount,
                        paid_date
		from 
			add_descriptions5
		group by 
			%scan(%superq(variables),&amp;amp;i.),paid_date;
	%end;
	quit;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Oct 2018 17:46:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-inside-proc-sql-with-scan-function-error/m-p/505245#M135281</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-10-17T17:46:45Z</dc:date>
    </item>
    <item>
      <title>Re: Looping inside proc sql with scan function error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-inside-proc-sql-with-scan-function-error/m-p/505248#M135284</link>
      <description>PROC MEANS would also do this in a single procedure, probably a bit more straightforward than a macro.</description>
      <pubDate>Wed, 17 Oct 2018 17:51:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-inside-proc-sql-with-scan-function-error/m-p/505248#M135284</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-10-17T17:51:37Z</dc:date>
    </item>
    <item>
      <title>Re: Looping inside proc sql with scan function error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-inside-proc-sql-with-scan-function-error/m-p/505267#M135288</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;SPAN class="login-bold"&gt;novinosrin,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;This didn't seem to work either.&amp;nbsp; This results in the same exact error.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt; create table     %scan(&amp;amp;variables.,&amp;amp;i.)_tb   as select distinct    %scan(%superq(variables),&amp;amp;i.),
                                                                                                               _
                                                                                                               22
45       ! sum(paid_amount) as paid_amount,     paid_date   from
45       !    add_descriptions5   group
3                                                          The SAS System                          11:41 Wednesday, October 17, 2018

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant, 
              a missing value, *, BTRIM, INPUT, PUT, SUBSTRING, USER.  

NOTE 137-205: Line generated by the invoked macro "table_creator".
45          by     %scan(%superq(variables),&amp;amp;i.),paid_date;
                                                _
                                                22
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant, 
              a missing value, BTRIM, INPUT, PUT, SUBSTRING, USER.  

NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
NOTE 137-205: Line generated by the invoked macro "table_creator".
45             create table     %scan(&amp;amp;variables.,&amp;amp;i.)_tb   as select distinct    %scan(%superq(variables),&amp;amp;i.),
                                                                                                               _
                                                                                                               22
45       ! sum(paid_amount) as paid_amount,    paid_date   from
45       !    add_descriptions5   group
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant, 
              a missing value, *, BTRIM, INPUT, PUT, SUBSTRING, USER.  

NOTE 137-205: Line generated by the invoked macro "table_creator".
45          by     %scan(%superq(variables),&amp;amp;i.),paid_date;
                                                _
                                                22
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant, 
              a missing value, BTRIM, INPUT, PUT, SUBSTRING, USER.  

NOTE: The SAS System stopped processing this step because of errors.&lt;/PRE&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Oct 2018 18:23:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-inside-proc-sql-with-scan-function-error/m-p/505267#M135288</guid>
      <dc:creator>boyandhisrobot</dc:creator>
      <dc:date>2018-10-17T18:23:56Z</dc:date>
    </item>
    <item>
      <title>Re: Looping inside proc sql with scan function error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-inside-proc-sql-with-scan-function-error/m-p/505269#M135290</link>
      <description>&lt;P&gt;did you make the change here?&lt;/P&gt;
&lt;PRE&gt;create table     %scan(&amp;amp;variables.,&amp;amp;i.)_tb &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;and try running&lt;/P&gt;
&lt;P&gt;options mlogic mprint symbolgen;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Oct 2018 18:25:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-inside-proc-sql-with-scan-function-error/m-p/505269#M135290</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-10-17T18:25:55Z</dc:date>
    </item>
    <item>
      <title>Re: Looping inside proc sql with scan function error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-inside-proc-sql-with-scan-function-error/m-p/505274#M135291</link>
      <description>&lt;P&gt;Set options mprint.&amp;nbsp; Do the loop from 1 to 2, which will give you a good chance to see what is not working in the log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also use the mfile option (and an mfile filename statement)&amp;nbsp;to write the generated sas program text to a file.&amp;nbsp; Read that back and you'll find the unadulterated code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hesitate to endorse the %superq idea, unless there is something very pathological about the VARIABLE macrovar.&amp;nbsp; What does that macrovar look like?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I made a synthetic dataset and used your macro for I=1 to 2, and got no errors:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data add_descriptions5;
  set sashelp.class (rename=(height=paid_date weight=paid_amount));
run;

%let variables=age name sex;
options mprint;
%macro table_creator;
	proc sql;
		%do i=1 %to 2;

		create table 
			%scan(&amp;amp;variables.,&amp;amp;i.)_tab
		as select distinct
			%scan(&amp;amp;variables.,&amp;amp;i.),
			sum(paid_amount) as paid_amount,
                        paid_date
		from 
			add_descriptions5
		group by 
			%scan(&amp;amp;variables.,&amp;amp;i.),paid_date;
	%end;
	quit;
%mend;
%table_creator;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Oct 2018 18:30:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-inside-proc-sql-with-scan-function-error/m-p/505274#M135291</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-10-17T18:30:26Z</dc:date>
    </item>
    <item>
      <title>Re: Looping inside proc sql with scan function error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-inside-proc-sql-with-scan-function-error/m-p/505277#M135292</link>
      <description>&lt;P&gt;I did, yes.&amp;nbsp; Sorry.&amp;nbsp; The log seems to state that it is now creating 33 tables titled "_tb" prior to the same error occurring in the select statement.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Oct 2018 18:37:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-inside-proc-sql-with-scan-function-error/m-p/505277#M135292</guid>
      <dc:creator>boyandhisrobot</dc:creator>
      <dc:date>2018-10-17T18:37:25Z</dc:date>
    </item>
    <item>
      <title>Re: Looping inside proc sql with scan function error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-inside-proc-sql-with-scan-function-error/m-p/505283#M135294</link>
      <description>&lt;P&gt;This was helpful, thank you!&amp;nbsp; I was able to identify that the issue was with the macro variable and not the sql code.&amp;nbsp; Thank you very much!&lt;/P&gt;</description>
      <pubDate>Wed, 17 Oct 2018 18:48:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-inside-proc-sql-with-scan-function-error/m-p/505283#M135294</guid>
      <dc:creator>boyandhisrobot</dc:creator>
      <dc:date>2018-10-17T18:48:08Z</dc:date>
    </item>
    <item>
      <title>Re: Looping inside proc sql with scan function error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-inside-proc-sql-with-scan-function-error/m-p/505291#M135295</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/126243"&gt;@boyandhisrobot&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;This was helpful, thank you!&amp;nbsp; I was able to identify that the issue was with the macro variable and not the sql code.&amp;nbsp; Thank you very much!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Fixed loop index, I= 1 to 33 may not actually match you created macro variable list of "words" that are found with %scan.&lt;/P&gt;
&lt;P&gt;Did you find that &amp;amp;variables had fewer than 33 "words"?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To make code like that a bit more flexible %do I = 1 %to %sysfunc(countw(&amp;amp;variables.));&lt;/P&gt;</description>
      <pubDate>Wed, 17 Oct 2018 19:09:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-inside-proc-sql-with-scan-function-error/m-p/505291#M135295</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-10-17T19:09:26Z</dc:date>
    </item>
  </channel>
</rss>

