<?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: PROC SQL and MACROS in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-and-MACROS/m-p/292658#M60795</link>
    <description>&lt;P&gt;hi&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you are using SQL Pass-Through, you can not use the double quotes to resolve macro trigger like &amp;amp;name and %name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However the is a simple solution to this, use the %TSLIT autocall macro to resolve macro triggers and put them into single quotes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a simple example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%MACRO YEARS(START,STOP);
	%DO YEAR = &amp;amp;START %TO &amp;amp;STOP;
    %put NOTE: TEST.USP_PATIENT %tslit(&amp;amp;year.-01-01) , %tslit(&amp;amp;year.-01-01);
	%END;

%MEND YEARS;

%YEARS(2010,2015)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bruno&lt;/P&gt;</description>
    <pubDate>Fri, 19 Aug 2016 06:43:16 GMT</pubDate>
    <dc:creator>BrunoMueller</dc:creator>
    <dc:date>2016-08-19T06:43:16Z</dc:date>
    <item>
      <title>PROC SQL and MACROS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-and-MACROS/m-p/292529#M60733</link>
      <description>&lt;P&gt;I'm sure this is something super easy to do, but I am just blanking on how to do this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have writen several proc sql and data statements that do different things throughout my SAS program. I was running the code on one year to test things out. What I need to do now is run my code for multiple years (2010-2015). So in my code below, wherever you see 2012, that needs to update based on the years of interest. Since it is a lot of code, I wanted to be able to have a macro fill that information in for me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;&lt;BR /&gt;CONNECT TO ODBC (DATAsrc='O Prod');
CREATE TABLE O_TEST2012 AS
SELECT *&lt;BR /&gt;FROM CONNECTION TO ODBC&lt;BR /&gt;(EXEC TEST.USP_PATIENT '2012-01-01', '2012-12-31') AS ODBC;
DISCONNECT FROM ODBC;
QUIT;&lt;BR /&gt;&lt;BR /&gt;DATA M1_2012; SET O_TEST2012;&lt;BR /&gt;DO DTHOSPSTAY = DTADMIT TO DTDISCH;&lt;BR /&gt;OUTPUT;&lt;BR /&gt;END;&lt;BR /&gt;FORMAT DTHOSPSTAY MMDDYY10.;&lt;BR /&gt;RUN;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am used to using macros in data steps, but have never done so in proc sql.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO YEARS(START,STOP);
	%DO YEAR = &amp;amp;START %TO &amp;amp;STOP;
	%END;

{........CODE...........}

%MEND YEARS;

%YEARS(2010,2015);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From what I can see online, macros don't work the same way in proc sql as they do in a data step. Any help would be appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2016 19:11:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-and-MACROS/m-p/292529#M60733</guid>
      <dc:creator>gdaymte</dc:creator>
      <dc:date>2016-08-18T19:11:51Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL and MACROS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-and-MACROS/m-p/292538#M60736</link>
      <description>&lt;P&gt;Macro generates SAS code, it doesn't care if it is a data step, SQL or another PROC. SO go ahead and try.&lt;/P&gt;
&lt;P&gt;But from your snippet, I would suspect that the dynamic code you wish to generate should be inside the %DO loop, not after.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2016 19:34:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-and-MACROS/m-p/292538#M60736</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-08-18T19:34:34Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL and MACROS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-and-MACROS/m-p/292565#M60753</link>
      <description>&lt;P&gt;I've tried it both ways, but neither seem work. I keep getting a message stating "WARNING: Apparent symbolic reference&amp;nbsp;YEARS not resolved." I went through and checked to maked sure everywhere I called upon &amp;amp;YEARS, that it matched up to the original macro name. It also throws a Syntax Error in my EXEC statement when it calls upon the stored procedure.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2016 20:54:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-and-MACROS/m-p/292565#M60753</guid>
      <dc:creator>gdaymte</dc:creator>
      <dc:date>2016-08-18T20:54:48Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL and MACROS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-and-MACROS/m-p/292585#M60763</link>
      <description>&lt;P&gt;Please show the log entry after running the code with OPTIONS MPRINT;&lt;/P&gt;
&lt;P&gt;And it may not hurt to show the actual macro code. Debugging code is very difficult without the code.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2016 21:44:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-and-MACROS/m-p/292585#M60763</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-08-18T21:44:16Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL and MACROS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-and-MACROS/m-p/292612#M60770</link>
      <description>&lt;P&gt;As mentionned, macros just generate text (code in this case).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example of a&amp;nbsp;macro used in&amp;nbsp;SQL:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table CLASS as
  select * 
  from SASHELP.CLASS
  where  %macro loop;
           %do i=10 %to 18 %by 2;
           %if &amp;amp;i&amp;gt;10 %then or ;
           AGE=&amp;amp;i
           %end;
         %mend;
         %loop
   order by NAME;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Aug 2016 23:17:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-and-MACROS/m-p/292612#M60770</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2016-08-18T23:17:56Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL and MACROS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-and-MACROS/m-p/292658#M60795</link>
      <description>&lt;P&gt;hi&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you are using SQL Pass-Through, you can not use the double quotes to resolve macro trigger like &amp;amp;name and %name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However the is a simple solution to this, use the %TSLIT autocall macro to resolve macro triggers and put them into single quotes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a simple example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%MACRO YEARS(START,STOP);
	%DO YEAR = &amp;amp;START %TO &amp;amp;STOP;
    %put NOTE: TEST.USP_PATIENT %tslit(&amp;amp;year.-01-01) , %tslit(&amp;amp;year.-01-01);
	%END;

%MEND YEARS;

%YEARS(2010,2015)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bruno&lt;/P&gt;</description>
      <pubDate>Fri, 19 Aug 2016 06:43:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-and-MACROS/m-p/292658#M60795</guid>
      <dc:creator>BrunoMueller</dc:creator>
      <dc:date>2016-08-19T06:43:16Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL and MACROS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-and-MACROS/m-p/292666#M60801</link>
      <description>&lt;P&gt;As an alternative, why not use the Actual Programming language itself:&lt;/P&gt;
&lt;PRE&gt;data years;
  do year=&amp;amp;start. to &amp;amp;stop.;
    output;
  end;
run;

proc sql;
  create table WANT as
  select    ...
  from      ...
  where  YEAR in (select YEAR from YEARS);
quit;&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 Aug 2016 08:02:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-and-MACROS/m-p/292666#M60801</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-08-19T08:02:08Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL and MACROS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-and-MACROS/m-p/294852#M61551</link>
      <description>&lt;P&gt;This worked for the SQL pass-through part, but I also used dates elsewhere in my code. To account for the dates further down in&amp;nbsp;my where statement, I had to make sure to use double qoutes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	CREATE TABLE P_ONE_&amp;amp;YEAR AS
		SELECT *
		FROM O_TEST&amp;amp;YEAR
		WHERE O_EXCLUDE=0 AND V_EXCLUDE=0 AND ("01JAN&amp;amp;YEAR."D &amp;lt;= RX_DTFILLED2 &amp;lt;= "31DEC&amp;amp;YEAR."D)
		GROUP BY PERSONID, RX_QTRFILLED
		ORDER BY PERSONID, RX_DTFILLED2
		;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Aug 2016 14:53:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-and-MACROS/m-p/294852#M61551</guid>
      <dc:creator>gdaymte</dc:creator>
      <dc:date>2016-08-29T14:53:22Z</dc:date>
    </item>
  </channel>
</rss>

