<?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: Execute proc sql based on value of global macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Execute-proc-sql-based-on-value-of-global-macro-variable/m-p/685107#M207736</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The message about quoted string too long is just a warning.&amp;nbsp; You can turn it off with system option noquotelenmax.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That said, if your goal is to execute a PROC SQL step (or any step) conditionally based on the value of a global macro variable, typically you would use a macro %IF statement for that, e.g.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;pen_ts=TS %then %do ;  %*I am assuming your macro variable does not have quotes in the value;
  proc sql;
       create table risk_assess as 
       select distinct compound_id, 
                       code, 
                       penalty_id,
                       track, 
                       min(seq) format=4. as min_of_seq 
                     
       from ps.tent_buy
       where code = '02603026'
       group by compound_id, code, penalty_id, track
       having seq = min(seq);
     quit;
%end ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you're not on a recent version of SAS, you will get an error about %if not valid in open code.&amp;nbsp; If that happens, you can create a macro, which is probably a good idea anyway, as you can trade the global macro variable for a local macro parameter:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro runquery(pen_ts=);
%if &amp;amp;pen_ts=TS %then %do ;  %*I am assuming your macro variable does not have quotes in the value;
  proc sql;
       create table risk_assess as 
       select distinct compound_id, 
                       code, 
                       penalty_id,
                       track, 
                       min(seq) format=4. as min_of_seq 
                     
       from ps.tent_buy
       where code = '02603026'
       group by compound_id, code, penalty_id, track
       having seq = min(seq);
     quit;
%end ;
%mend runquery;

%runquery(pen_ts=no) /*will not execute SQL*/
%runquery(pen_ts=TS) /*will execute SQL*/
%runquery(pen_ts=&amp;amp;pen_ts) /*will pass the value of global macro var to the parameter*/
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;Once you have written that macro, you'll start seeing additional parameters that might be useful (name of table queried, name of table to create, code ...)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 18 Sep 2020 20:46:34 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2020-09-18T20:46:34Z</dc:date>
    <item>
      <title>Execute proc sql based on value of global macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Execute-proc-sql-based-on-value-of-global-macro-variable/m-p/685094#M207726</link>
      <description>&lt;P&gt;I'm trying to execute some Proc SQL conditionally, based on the value of a global macro variable (&amp;amp;pen_ts). I attempted to do it with call execute () but it tells me the quoted string is too long. This is what I'm trying to do:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  length longstr $ 200;
  longstr = "proc sql;
     create table risk_assess as 
     select distinct compound_id, 
                     code, 
                     penalty_id,
                     track, 
                     min(seq) format=4. as min_of_seq 
                     
     from ps.tent_buy
     where code = '02603026'
     group by compound_id, code, penalty_id, track
     having seq = min(seq);
   quit;";
  if &amp;amp;pen_ts = 'TS' then call execute (longstr);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any suggestions? Thank you...&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2020 20:25:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Execute-proc-sql-based-on-value-of-global-macro-variable/m-p/685094#M207726</guid>
      <dc:creator>PegaZeus</dc:creator>
      <dc:date>2020-09-18T20:25:09Z</dc:date>
    </item>
    <item>
      <title>Re: Execute proc sql based on value of global macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Execute-proc-sql-based-on-value-of-global-macro-variable/m-p/685106#M207735</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) why not to use macro, it seems to be easier to maintain:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro doSQL()
%if &amp;amp;pen_ts = 'TS' %then 
  %do;
    proc sql;
     create table risk_assess as 
     select distinct compound_id, 
                     code, 
                     penalty_id,
                     track, 
                     min(seq) format=4. as min_of_seq 
                     
     from ps.tent_buy
     where code = '02603026'
     group by compound_id, code, penalty_id, track
     having seq = min(seq);
    quit;
  %end;
%mend doSQL;

%doSQL()&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;2) Does macrovariable pen_ts contains `'TS'` (with single quotes) as the value or `TS` (without quotes) as the value? This is important to check the %if condition&amp;nbsp;&lt;/P&gt;
&lt;P&gt;since for macrolanguage text `'TS'` not equals to text `TS`&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2020 20:46:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Execute-proc-sql-based-on-value-of-global-macro-variable/m-p/685106#M207735</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-09-18T20:46:02Z</dc:date>
    </item>
    <item>
      <title>Re: Execute proc sql based on value of global macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Execute-proc-sql-based-on-value-of-global-macro-variable/m-p/685107#M207736</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The message about quoted string too long is just a warning.&amp;nbsp; You can turn it off with system option noquotelenmax.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That said, if your goal is to execute a PROC SQL step (or any step) conditionally based on the value of a global macro variable, typically you would use a macro %IF statement for that, e.g.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;pen_ts=TS %then %do ;  %*I am assuming your macro variable does not have quotes in the value;
  proc sql;
       create table risk_assess as 
       select distinct compound_id, 
                       code, 
                       penalty_id,
                       track, 
                       min(seq) format=4. as min_of_seq 
                     
       from ps.tent_buy
       where code = '02603026'
       group by compound_id, code, penalty_id, track
       having seq = min(seq);
     quit;
%end ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you're not on a recent version of SAS, you will get an error about %if not valid in open code.&amp;nbsp; If that happens, you can create a macro, which is probably a good idea anyway, as you can trade the global macro variable for a local macro parameter:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro runquery(pen_ts=);
%if &amp;amp;pen_ts=TS %then %do ;  %*I am assuming your macro variable does not have quotes in the value;
  proc sql;
       create table risk_assess as 
       select distinct compound_id, 
                       code, 
                       penalty_id,
                       track, 
                       min(seq) format=4. as min_of_seq 
                     
       from ps.tent_buy
       where code = '02603026'
       group by compound_id, code, penalty_id, track
       having seq = min(seq);
     quit;
%end ;
%mend runquery;

%runquery(pen_ts=no) /*will not execute SQL*/
%runquery(pen_ts=TS) /*will execute SQL*/
%runquery(pen_ts=&amp;amp;pen_ts) /*will pass the value of global macro var to the parameter*/
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;Once you have written that macro, you'll start seeing additional parameters that might be useful (name of table queried, name of table to create, code ...)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2020 20:46:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Execute-proc-sql-based-on-value-of-global-macro-variable/m-p/685107#M207736</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2020-09-18T20:46:34Z</dc:date>
    </item>
  </channel>
</rss>

