<?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 execute sql-query within a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/execute-sql-query-within-a-macro/m-p/641583#M191235</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;/* The Macro */
%macro run_query(q,id);
  proc sql noprint;
      select count into: count
      from (&amp;amp;q.) a;
  quit;
%mend;

/* Some fake-data */
DATA queries;
INPUT id :$12. query :$3000.;
INFORMAT id $12.;
INFILE DATALINES DSD;
DATALINES;
01,SELECT COUNT(*) AS count FROM sashelp.bweight WHERE Married=1
0101,SELECT COUNT(*) AS count FROM sashelp.bweight WHERE Boy=1
0102,SELECT COUNT(*) AS count FROM sashelp.bweight WHERE Black=1
;
RUN;

/* Make a copy of the dataset */
DATA want;
SET queries;
RUN;

/* Insert the results */
data want;
set queries;
call execute(%nrstr(%run_query('||query||','||id||')));
run;&lt;/PRE&gt;&lt;P&gt;Hello everybody,&lt;/P&gt;&lt;P&gt;I have problems with the above code. In table queries there are two variables: a sql-query (`query`) plus an `id`. Now, I'd like to save the results of these queries as well as the corresponding `id` to another table `want`. What do I have to change?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Tue, 21 Apr 2020 12:04:16 GMT</pubDate>
    <dc:creator>dstuder</dc:creator>
    <dc:date>2020-04-21T12:04:16Z</dc:date>
    <item>
      <title>execute sql-query within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/execute-sql-query-within-a-macro/m-p/641583#M191235</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;/* The Macro */
%macro run_query(q,id);
  proc sql noprint;
      select count into: count
      from (&amp;amp;q.) a;
  quit;
%mend;

/* Some fake-data */
DATA queries;
INPUT id :$12. query :$3000.;
INFORMAT id $12.;
INFILE DATALINES DSD;
DATALINES;
01,SELECT COUNT(*) AS count FROM sashelp.bweight WHERE Married=1
0101,SELECT COUNT(*) AS count FROM sashelp.bweight WHERE Boy=1
0102,SELECT COUNT(*) AS count FROM sashelp.bweight WHERE Black=1
;
RUN;

/* Make a copy of the dataset */
DATA want;
SET queries;
RUN;

/* Insert the results */
data want;
set queries;
call execute(%nrstr(%run_query('||query||','||id||')));
run;&lt;/PRE&gt;&lt;P&gt;Hello everybody,&lt;/P&gt;&lt;P&gt;I have problems with the above code. In table queries there are two variables: a sql-query (`query`) plus an `id`. Now, I'd like to save the results of these queries as well as the corresponding `id` to another table `want`. What do I have to change?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2020 12:04:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/execute-sql-query-within-a-macro/m-p/641583#M191235</guid>
      <dc:creator>dstuder</dc:creator>
      <dc:date>2020-04-21T12:04:16Z</dc:date>
    </item>
    <item>
      <title>Re: execute sql-query within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/execute-sql-query-within-a-macro/m-p/641588#M191239</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* The Macro */
%macro run_query(q,id);
  %global _&amp;amp;id.; /* &amp;lt;- change of scope */
  proc sql noprint;
      select count into: _&amp;amp;id. /* &amp;lt;- identifier */
      from &amp;amp;q. a;
  quit;
%mend;

/* Some fake-data */
DATA queries;
INPUT id :$12. query :$3000.;
INFORMAT id $12.;
INFILE DATALINES DSD;
DATALINES;
01,SELECT COUNT(*) AS count FROM sashelp.bweight WHERE Married=1
0101,SELECT COUNT(*) AS count FROM sashelp.bweight WHERE Boy=1
0102,SELECT COUNT(*) AS count FROM sashelp.bweight WHERE Black=1
;
RUN;

/* Make a copy of the dataset */
DATA want;
SET queries;
RUN;

/* Insert the results */
data _null_;
set queries;
call execute('%nrstr(%run_query(('||strip(query)||'),'||id||'))'); /* &amp;lt;- "(" and ")" added over querry text */
run;

DATA want2;
  SET queries;
  count = input(symget(cats("_",id)), ?? best32. ); /* collect data */
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2020 12:25:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/execute-sql-query-within-a-macro/m-p/641588#M191239</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-04-21T12:25:21Z</dc:date>
    </item>
    <item>
      <title>Re: execute sql-query within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/execute-sql-query-within-a-macro/m-p/641593#M191241</link>
      <description>&lt;P&gt;So Bart (&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;) has clearly given the correct answer, but to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/324367"&gt;@dstuder&lt;/a&gt;&amp;nbsp;I point out that before you write macros, you first need to write code that works and &lt;EM&gt;does what you want&lt;/EM&gt; without macros and without macro variables, for one or two cases. That is an essential first step in getting macros to work — if you can't get the code working and &lt;EM&gt;doing what you want&lt;/EM&gt; without macro variables and without macros, then it will never work once you try to write a macro version of the code. The code in your macro %run_query doesn't do what you want, and so wrapping it in a macro will not remedy this.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2020 12:45:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/execute-sql-query-within-a-macro/m-p/641593#M191241</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-04-21T12:45:50Z</dc:date>
    </item>
    <item>
      <title>Re: execute sql-query within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/execute-sql-query-within-a-macro/m-p/641594#M191242</link>
      <description>&lt;P&gt;Paige (&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;) I couldn't agree more with your advice to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/324367"&gt;@dstuder&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;First working "simple" code and only(!) then "macro wrapping".&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;</description>
      <pubDate>Tue, 21 Apr 2020 12:50:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/execute-sql-query-within-a-macro/m-p/641594#M191242</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-04-21T12:50:30Z</dc:date>
    </item>
    <item>
      <title>Re: execute sql-query within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/execute-sql-query-within-a-macro/m-p/641598#M191244</link>
      <description>&lt;P&gt;Here is a slightly modified queries, with a in-data step Proc SQL execution&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Some fake-data */
DATA queries;
INPUT id :$12. query :$3000.;
INFORMAT id $12.;
INFILE DATALINES DSD;
DATALINES;
01,SELECT COUNT(*)&lt;STRONG&gt; into :count&lt;/STRONG&gt; FROM sashelp.bweight WHERE Married=1
0101,SELECT COUNT(*)&lt;STRONG&gt; into :count&lt;/STRONG&gt; FROM sashelp.bweight WHERE Boy=1
0102,SELECT COUNT(*)&lt;STRONG&gt; into :count&lt;/STRONG&gt; FROM sashelp.bweight WHERE Black=1
;
RUN;

data want(drop=query);
set queries;
rc= &lt;STRONG&gt;dosubl&lt;/STRONG&gt;('proc sql noprint;'||query||'; quit;');
&lt;STRONG&gt;count = symget('count');&lt;/STRONG&gt;
put count=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;Ahmed&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2020 12:53:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/execute-sql-query-within-a-macro/m-p/641598#M191244</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2020-04-21T12:53:50Z</dc:date>
    </item>
    <item>
      <title>Re: execute sql-query within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/execute-sql-query-within-a-macro/m-p/641687#M191303</link>
      <description>&lt;P&gt;I like the DOSUBL, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13868"&gt;@AhmedAl_Attar&lt;/a&gt;&amp;nbsp;!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The only bit that bothers me is the creation of a global macro variable, which I try to avoid.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One alternative is to wrap the DOSUBL bit inside a function-style macro, and then to pass it data, you need to call it via RESOLVE, e.g.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA queries;
INPUT id :$12. query :$3000.;
INFORMAT id $12.;
INFILE DATALINES DSD;
DATALINES;
01,SELECT COUNT(*) into :count trimmed FROM sashelp.bweight WHERE Married=1
0101,SELECT COUNT(*) into :count trimmed FROM sashelp.bweight WHERE Boy=1
0102,SELECT COUNT(*) into :count trimmed FROM sashelp.bweight WHERE Black=1
;
RUN;


%macro runquery(query) ;
  %local rc count ;
  %let rc=%sysfunc(dosubl(%nrstr(
    proc sql noprint;
      &amp;amp;query; 
    quit;
    )));
  &amp;amp;count
%mend ;

data want ;
  set queries ;
  count=resolve(cats('%runquery(',query,')')) ;

  put (id query count)(=) ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2020 17:24:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/execute-sql-query-within-a-macro/m-p/641687#M191303</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2020-04-21T17:24:50Z</dc:date>
    </item>
    <item>
      <title>Re: execute sql-query within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/execute-sql-query-within-a-macro/m-p/641709#M191313</link>
      <description>&lt;P&gt;Just for fun, clear datastep no macros, one single proc sql:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA queries;
INPUT id :$12. query :$3000.;
INFORMAT id $12.;
INFILE DATALINES DSD;
DATALINES;
01  ,SELECT COUNT(*) AS count FROM sashelp.bweight WHERE Married=1
0101,SELECT COUNT(*) AS count FROM sashelp.bweight WHERE Boy=1
0102,SELECT COUNT(*) AS count FROM sashelp.bweight WHERE Black=1
;
RUN;

DATA _null_;
  call execute('proc sql noprint;');
  do _N_ = 1 by 1 until(EOF);
    SET queries end= EOF;
    call execute(
    'create table work._' || put(_N_,z30.) || /* to keep observations order */
    ' as select a.count
      from (' || strip(query) || ') a;'
    );
  end;
  call execute('quit;');
  stop;
RUN;

data counts;
  set work._0: OPEN=DEFER;
run;

data want;
 merge queries counts;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2020 18:38:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/execute-sql-query-within-a-macro/m-p/641709#M191313</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-04-21T18:38:35Z</dc:date>
    </item>
    <item>
      <title>Re: execute sql-query within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/execute-sql-query-within-a-macro/m-p/641728#M191322</link>
      <description>&lt;P&gt;Nice&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;. If we're just having, fun, how about one just one data step to generate one SQL step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not sure if below actually works, and I wouldn't want to maintain it.&amp;nbsp; : )&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA _null_;
  call execute(
    'proc sql noprint;'
    || 'create table work.want as '
    || ' select a.id, a.query, b.count from ' 
    || '  (select id, query from queries) as a'
    || ','
    || '('
  );
  
  do _N_ = 1 by 1 until(EOF);
    SET queries end= EOF;
    call execute(
      '(SELECT "'||strip(id)||'" as id, '||transtrn(strip(query),'SELECT','')||')'
    );
    if NOT EOF then do ;
      call execute('union corr all') ;
    end ;
  end;
  call execute(
    ') as b ' 
   ||'where a.id=b.id'
   ||';quit ;'
  );
  stop;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Apr 2020 19:44:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/execute-sql-query-within-a-macro/m-p/641728#M191322</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2020-04-21T19:44:29Z</dc:date>
    </item>
    <item>
      <title>Re: execute sql-query within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/execute-sql-query-within-a-macro/m-p/641739#M191323</link>
      <description>&lt;P&gt;But it will handle only up to 255 subqueries.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;B-)&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2020 20:03:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/execute-sql-query-within-a-macro/m-p/641739#M191323</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-04-21T20:03:23Z</dc:date>
    </item>
  </channel>
</rss>

