<?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 Statement is not valid or it is used out of proper order in macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Statement-is-not-valid-or-it-is-used-out-of-proper-order-in/m-p/861286#M340224</link>
    <description>&lt;P&gt;When I execute the following code, it's works for first iteration and it's throwing an error from second iteration. I believe the issue is in the index portion (proc contents and data _null_). It's not working after&amp;nbsp;first iteration. May be code placement is not right. Any help?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro simple_loop;
proc sql noprint;
  
    connect using &amp;amp;db_lib as database;

    %do type_id=1 %to %sysfunc(countw(&amp;amp;type_list,,s));
/*some macro code*/
 execute(
        create or replace tbl &amp;amp;schema_temp..&amp;amp;prompt_tbl_name as
          select distinct &amp;amp;current_distinct_var_list
          from &amp;amp;schema_temp..&amp;amp;source_tbl
        ;
      ) by database;
      
      
      create tbl &amp;amp;prompt_tbl_name as
        select * from connection to database (
          select *
          from &amp;amp;schema_temp..&amp;amp;prompt_tbl_name;
        )
      ;
proc contents data=&amp;amp;prompt_tbl_name noprint
out=index_list(keep=libname memname name);
run;

filename code temp;
data _null_;
set index_list;
by libname memname ;
file code;
if first.libname then put 'proc datasets nolist lib=' libname ';' ;
if first.memname then put 'modify ' memname ';' / '  create index ' @;
put name @;
if last.memname then put ';' / 'run;' ;
if last.libname then put 'quit;' ;
run;  
%include code / source2;     
%end;    
quit;

%mend simple_loop;
%simple_loop;
&lt;/PRE&gt;</description>
    <pubDate>Tue, 28 Feb 2023 07:33:47 GMT</pubDate>
    <dc:creator>David_Billa</dc:creator>
    <dc:date>2023-02-28T07:33:47Z</dc:date>
    <item>
      <title>Statement is not valid or it is used out of proper order in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Statement-is-not-valid-or-it-is-used-out-of-proper-order-in/m-p/861286#M340224</link>
      <description>&lt;P&gt;When I execute the following code, it's works for first iteration and it's throwing an error from second iteration. I believe the issue is in the index portion (proc contents and data _null_). It's not working after&amp;nbsp;first iteration. May be code placement is not right. Any help?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro simple_loop;
proc sql noprint;
  
    connect using &amp;amp;db_lib as database;

    %do type_id=1 %to %sysfunc(countw(&amp;amp;type_list,,s));
/*some macro code*/
 execute(
        create or replace tbl &amp;amp;schema_temp..&amp;amp;prompt_tbl_name as
          select distinct &amp;amp;current_distinct_var_list
          from &amp;amp;schema_temp..&amp;amp;source_tbl
        ;
      ) by database;
      
      
      create tbl &amp;amp;prompt_tbl_name as
        select * from connection to database (
          select *
          from &amp;amp;schema_temp..&amp;amp;prompt_tbl_name;
        )
      ;
proc contents data=&amp;amp;prompt_tbl_name noprint
out=index_list(keep=libname memname name);
run;

filename code temp;
data _null_;
set index_list;
by libname memname ;
file code;
if first.libname then put 'proc datasets nolist lib=' libname ';' ;
if first.memname then put 'modify ' memname ';' / '  create index ' @;
put name @;
if last.memname then put ';' / 'run;' ;
if last.libname then put 'quit;' ;
run;  
%include code / source2;     
%end;    
quit;

%mend simple_loop;
%simple_loop;
&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Feb 2023 07:33:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Statement-is-not-valid-or-it-is-used-out-of-proper-order-in/m-p/861286#M340224</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2023-02-28T07:33:47Z</dc:date>
    </item>
    <item>
      <title>Re: Statement is not valid or it is used out of proper order in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Statement-is-not-valid-or-it-is-used-out-of-proper-order-in/m-p/861287#M340225</link>
      <description>&lt;P&gt;Before running the code add:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint symbolgen;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to it and share log results.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 07:54:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Statement-is-not-valid-or-it-is-used-out-of-proper-order-in/m-p/861287#M340225</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-02-28T07:54:25Z</dc:date>
    </item>
    <item>
      <title>Re: Statement is not valid or it is used out of proper order in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Statement-is-not-valid-or-it-is-used-out-of-proper-order-in/m-p/861291#M340226</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;Log is shown below. It's big file hence I shared only the portion of error log. Here the catch is index is created for all the iterations and from second iteration I could see the error before the index steps. However if I remove the index part from my code, it is executing without any issues for all the iteration.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;SYMBOLGEN:  Macro variable CLASS_VAR_ID resolves to 2
MLOGIC(SIMPLE_LOOP):  %IF condition (&amp;amp;class_var_ID eq 1) is FALSE
MLOGIC(SIMPLE_LOOP):  %LET (variable name is PROMPT_TBL_NAME)
SYMBOLGEN:  Macro variable PROMPT_TBL_NAME resolves to dmm_tts_pt_div
SYMBOLGEN:  Macro variable CURRENT_CLASS_VAR resolves to upn
MLOGIC(SIMPLE_LOOP):  %DO loop index variable CLASS_VAR_ID is now 3; loop will not iterate again.
MLOGIC(SIMPLE_LOOP):  %PUT NOTE: &amp;amp;=prompt_TBL_name
SYMBOLGEN:  Macro variable PROMPT_TBL_NAME resolves to dmm_ttt_pt_div_upn
NOTE: PROMPT_TBL_NAME=dmm_ttt_pt_div_upn
NOTE: Line generated by the invoked macro "SIMPLE_LOOP".
235                  execute(         create or replace TBL &amp;amp;schema_temp..&amp;amp;prompt_TBL_name as           select distinct
                     -------
                     180
235  ! &amp;amp;current_distinct_var_list           from &amp;amp;schema_temp..&amp;amp;source_TBL         ;       ) by database;
SYMBOLGEN:  Macro variable SCHEMA_TEMP resolves to PMQ_TEMP_TBLS
SYMBOLGEN:  Macro variable PROMPT_TBL_NAME resolves to dmm_tts_pt_div_upn
SYMBOLGEN:  Macro variable CURRENT_DISTINCT_VAR_LIST resolves to bss_division,upn
SYMBOLGEN:  Macro variable SCHEMA_TEMP resolves to PMQ_TEMP_TBLS
SYMBOLGEN:  Macro variable SOURCE_TBL resolves to dmm_transfer_prompts
MPRINT(SIMPLE_LOOP):   execute( create or replace TBL PMQ_TEMP_TBLS.dmm_tts_pt_div_upn as select distinct bss_division,upn 
from PMQ_TEMP_TBLS.dmm_transfer_prompts ;
ERROR 180-322: Statement is not valid or it is used out of proper order.
NOTE: Line generated by the invoked macro "SIMPLE_LOOP".
235                  execute(         create or replace TBL &amp;amp;schema_temp..&amp;amp;prompt_TBL_name as           select distinct
235  ! &amp;amp;current_distinct_var_list           from &amp;amp;schema_temp..&amp;amp;source_TBL         ;       ) by database;
                                                                                             -
                                                                                             180
ERROR 180-322: Statement is not valid or it is used out of proper order.
MPRINT(SIMPLE_LOOP):   ) by database;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 08:16:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Statement-is-not-valid-or-it-is-used-out-of-proper-order-in/m-p/861291#M340226</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2023-02-28T08:16:21Z</dc:date>
    </item>
    <item>
      <title>Re: Statement is not valid or it is used out of proper order in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Statement-is-not-valid-or-it-is-used-out-of-proper-order-in/m-p/861293#M340228</link>
      <description>&lt;P&gt;There is this very old saying which states: &amp;gt;&amp;gt;&lt;EM&gt;Usually a code executed inside the Proc SQL works better if before it you write "Proc SQL;"...&lt;/EM&gt;&amp;lt;&amp;lt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Look at your code, where the %do-loop is located and what it contains, you have:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) "proc SQL"&lt;/P&gt;
&lt;P&gt;2) "%do-loop"&lt;/P&gt;
&lt;P&gt;3) "sql code"&lt;/P&gt;
&lt;P&gt;4) "proc contents"&lt;/P&gt;
&lt;P&gt;5) "data step"&lt;/P&gt;
&lt;P&gt;6) "%include with proc datasets"&lt;/P&gt;
&lt;P&gt;7) "%end" for do-loop&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":smiling_face_with_sunglasses:"&gt;😎&lt;/span&gt; "quit;"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically point 4) turns off proc SQL and in the second iteration of %do-loop tries to execute "sql code" without Proc SQL...&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 08:25:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Statement-is-not-valid-or-it-is-used-out-of-proper-order-in/m-p/861293#M340228</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-02-28T08:25:42Z</dc:date>
    </item>
    <item>
      <title>Re: Statement is not valid or it is used out of proper order in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Statement-is-not-valid-or-it-is-used-out-of-proper-order-in/m-p/861295#M340230</link>
      <description>&lt;P&gt;Thanks for the insights. Were you able to help me fix the issue? I'm not certain how to replace proc contents&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 08:28:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Statement-is-not-valid-or-it-is-used-out-of-proper-order-in/m-p/861295#M340230</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2023-02-28T08:28:32Z</dc:date>
    </item>
    <item>
      <title>Re: Statement is not valid or it is used out of proper order in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Statement-is-not-valid-or-it-is-used-out-of-proper-order-in/m-p/861296#M340231</link>
      <description>&lt;P&gt;Your first statement created by the %DO loop is a PROC SQL statement (EXECUTE), but your loop also contains a PROC CONTENTS, which implicitly terminates your PROC SQL right there.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 08:29:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Statement-is-not-valid-or-it-is-used-out-of-proper-order-in/m-p/861296#M340231</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-02-28T08:29:07Z</dc:date>
    </item>
    <item>
      <title>Re: Statement is not valid or it is used out of proper order in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Statement-is-not-valid-or-it-is-used-out-of-proper-order-in/m-p/861299#M340234</link>
      <description>&lt;P&gt;Review your process. Start with simple, non-macro code. Then write the first iteration, again without macro language. From this, deduce how the loop should be built.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 08:31:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Statement-is-not-valid-or-it-is-used-out-of-proper-order-in/m-p/861299#M340234</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-02-28T08:31:13Z</dc:date>
    </item>
    <item>
      <title>Re: Statement is not valid or it is used out of proper order in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Statement-is-not-valid-or-it-is-used-out-of-proper-order-in/m-p/861300#M340235</link>
      <description>&lt;P&gt;Yes I did this with one iteration before placing my code inside macro. I didn't realise that it will fail in macro looping. Now I'm not sure to correct it inside macro&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 08:34:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Statement-is-not-valid-or-it-is-used-out-of-proper-order-in/m-p/861300#M340235</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2023-02-28T08:34:16Z</dc:date>
    </item>
    <item>
      <title>Re: Statement is not valid or it is used out of proper order in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Statement-is-not-valid-or-it-is-used-out-of-proper-order-in/m-p/861301#M340236</link>
      <description>&lt;P&gt;try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro simple_loop;

%do type_id=1 %to %sysfunc(countw(&amp;amp;type_list,,s));

  proc sql noprint;
    
  connect using &amp;amp;db_lib as database;

  /*some macro code*/
   execute(
          create or replace tbl &amp;amp;schema_temp..&amp;amp;prompt_tbl_name as
            select distinct &amp;amp;current_distinct_var_list
            from &amp;amp;schema_temp..&amp;amp;source_tbl
          ;
        ) by database;
        
        
        create tbl &amp;amp;prompt_tbl_name as
          select * from connection to database (
            select *
            from &amp;amp;schema_temp..&amp;amp;prompt_tbl_name;
          )
        ;
  quit;

  proc contents data=&amp;amp;prompt_tbl_name noprint
  out=index_list(keep=libname memname name);
  run;

  filename code temp;
  data _null_;
  set index_list;
  by libname memname ;
  file code;
  if first.libname then put 'proc datasets nolist lib=' libname ';' ;
  if first.memname then put 'modify ' memname ';' / '  create index ' @;
  put name @;
  if last.memname then put ';' / 'run;' ;
  if last.libname then put 'quit;' ;
  run;  
  %include code / source2;     
%end;    

%mend simple_loop;
%simple_loop;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 08:34:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Statement-is-not-valid-or-it-is-used-out-of-proper-order-in/m-p/861301#M340236</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-02-28T08:34:36Z</dc:date>
    </item>
    <item>
      <title>Re: Statement is not valid or it is used out of proper order in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Statement-is-not-valid-or-it-is-used-out-of-proper-order-in/m-p/861303#M340238</link>
      <description>&lt;P&gt;That's why you need to first get a &lt;U&gt;complete second iteration&lt;/U&gt; of the intended loop to run before coding the macro. Only then will you see which components MUST be inside the loop.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 08:42:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Statement-is-not-valid-or-it-is-used-out-of-proper-order-in/m-p/861303#M340238</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-02-28T08:42:48Z</dc:date>
    </item>
  </channel>
</rss>

