<?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 proc sql union and macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-union-and-macro/m-p/163117#M31614</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I have sas data sets distributed by months -example sales201401 sales 201402....&lt;/P&gt;&lt;P&gt;i want to retriever the data using proc sql&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;example&lt;/P&gt;&lt;P&gt;i want to the records for a particular product by month for 3 months-201401, 201402, 201403&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;select *&lt;/P&gt;&lt;P&gt;from sales201401&lt;/P&gt;&lt;P&gt;where product_id=1&lt;/P&gt;&lt;P&gt;union all&lt;/P&gt;&lt;P&gt;select *&lt;/P&gt;&lt;P&gt;from sales201402&lt;/P&gt;&lt;P&gt;where product_id=1&lt;/P&gt;&lt;P&gt;union all&lt;/P&gt;&lt;P&gt;select *&lt;/P&gt;&lt;P&gt;from sales201403&lt;/P&gt;&lt;P&gt;where product_id=1;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;is there any macro technique i can use?&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;vminc&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 26 Dec 2014 18:49:47 GMT</pubDate>
    <dc:creator>vminc</dc:creator>
    <dc:date>2014-12-26T18:49:47Z</dc:date>
    <item>
      <title>proc sql union and macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-union-and-macro/m-p/163117#M31614</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I have sas data sets distributed by months -example sales201401 sales 201402....&lt;/P&gt;&lt;P&gt;i want to retriever the data using proc sql&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;example&lt;/P&gt;&lt;P&gt;i want to the records for a particular product by month for 3 months-201401, 201402, 201403&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;select *&lt;/P&gt;&lt;P&gt;from sales201401&lt;/P&gt;&lt;P&gt;where product_id=1&lt;/P&gt;&lt;P&gt;union all&lt;/P&gt;&lt;P&gt;select *&lt;/P&gt;&lt;P&gt;from sales201402&lt;/P&gt;&lt;P&gt;where product_id=1&lt;/P&gt;&lt;P&gt;union all&lt;/P&gt;&lt;P&gt;select *&lt;/P&gt;&lt;P&gt;from sales201403&lt;/P&gt;&lt;P&gt;where product_id=1;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;is there any macro technique i can use?&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;vminc&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 26 Dec 2014 18:49:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-union-and-macro/m-p/163117#M31614</guid>
      <dc:creator>vminc</dc:creator>
      <dc:date>2014-12-26T18:49:47Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql union and macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-union-and-macro/m-p/163118#M31615</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You could save yourself a lot of trouble with a simple datastep:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data allSales;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;set sales201:;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;if product_id=1;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The SET statement expands dataset lists such as &lt;STRONG&gt;sales201:&lt;/STRONG&gt; or &lt;STRONG&gt;sales201401-sales201408&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PG&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 26 Dec 2014 19:16:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-union-and-macro/m-p/163118#M31615</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2014-12-26T19:16:13Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql union and macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-union-and-macro/m-p/163119#M31616</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P style="font-size: 10px; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #0433ff;"&gt;&lt;SPAN style="font-size: 12pt; color: #000000; font-family: calibri, verdana, arial, sans-serif;"&gt;You can use macro variables to get the date intervals you need and then use a set statement in a data step.&lt;/SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 10px; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #0433ff;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 10px; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #0433ff;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 10px; font-family: 'Courier New';"&gt;&lt;SPAN style="font-size: 12pt;"&gt;&lt;SPAN style="color: #0433ff;"&gt;%let&lt;/SPAN&gt; start_month=201408;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 10px; font-family: 'Courier New';"&gt;&lt;SPAN style="font-size: 12pt;"&gt;&lt;SPAN style="color: #0433ff;"&gt;%let&lt;/SPAN&gt; end_month=&lt;SPAN style="color: #0433ff;"&gt;%sysfunc&lt;/SPAN&gt;(intnx(month, &lt;SPAN style="color: #0433ff;"&gt;%sysfunc&lt;/SPAN&gt;(inputn(&amp;amp;start_month, yymmn6.)), 3), yymmn6.);&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 10px; font-family: 'Courier New';"&gt;&lt;/P&gt;&lt;P style="font-size: 10px; font-family: 'Courier New';"&gt;&lt;SPAN style="font-size: 12pt;"&gt;&lt;SPAN style="color: #0433ff;"&gt;%put&lt;/SPAN&gt; &amp;amp;start_month;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 10px; font-family: 'Courier New';"&gt;&lt;SPAN style="font-size: 12pt;"&gt;&lt;SPAN style="color: #0433ff;"&gt;%put&lt;/SPAN&gt; &amp;amp;end_month;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 10px; font-family: 'Courier New';"&gt;&lt;/P&gt;&lt;P style="font-size: 10px; font-family: 'Courier New';"&gt;&lt;SPAN style="font-size: 12pt;"&gt;&lt;SPAN style="color: #011993;"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/SPAN&gt; want;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 10px; font-family: 'Courier New';"&gt;&lt;SPAN style="font-size: 12pt;"&gt;&lt;SPAN style="color: #0433ff;"&gt;set&lt;/SPAN&gt; sales&amp;amp;start_month - sales&amp;amp;end_month;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 10px; font-family: 'Courier New'; color: #011993;"&gt;&lt;SPAN style="font-size: 12pt;"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;SPAN style="color: #000000;"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 26 Dec 2014 22:43:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-union-and-macro/m-p/163119#M31616</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2014-12-26T22:43:03Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql union and macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-union-and-macro/m-p/163120#M31617</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Why (not) generate code with a macro?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But first, make sure the target code is right. Why the ALLs in the UNIONs?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 27 Dec 2014 02:55:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-union-and-macro/m-p/163120#M31617</guid>
      <dc:creator>Howles</dc:creator>
      <dc:date>2014-12-27T02:55:29Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql union and macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-union-and-macro/m-p/163121#M31618</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;UNION ALL appends the tables without checking for duplicate records while UNION creates a set of unique records. UNION ALL is thus faster because it requires less processing. - PG&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 27 Dec 2014 03:43:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-union-and-macro/m-p/163121#M31618</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2014-12-27T03:43:24Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql union and macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-union-and-macro/m-p/302523#M64180</link>
      <description>&lt;P&gt;Thanks and how to unite tables during 201412 - 201501?&lt;BR /&gt;In the specified example tries to unite 201413, 201414, etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;or&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let end_month=%sysfunc(intnx(month, %sysfunc(inputn(&amp;amp;start_month, yymmn6.)), &lt;FONT color="#ff0000"&gt;-24&lt;/FONT&gt;), yymmn6.);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Oct 2016 07:23:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-union-and-macro/m-p/302523#M64180</guid>
      <dc:creator>trem</dc:creator>
      <dc:date>2016-10-05T07:23:07Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql union and macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-union-and-macro/m-p/302535#M64183</link>
      <description>&lt;P&gt;If you are the one who is creating these monthly data sets then be nice to your data consumers and create views for them for the most often required windows like "current month", "current quarter", "last quarter" and so on - and update (re-create) these views whenever you create a new monthly data set.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are one of the data consumers then I'd request such views as that's really not hard to code and implement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below some code which should give you what you have been asking for.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sales201512 sales201601 sales201602;
  product_id=1; output;
  product_id=2; output;
run;

%macro create_view
  (
    view_name=, 
    base_name=,
    start_yyyymm=, 
    n_months=
  );

  proc sql;
    create view &amp;amp;view_name as
    %do i=0 %to %eval(&amp;amp;n_months-1);
      select *
      from &amp;amp;base_name%sysfunc(intnx(month, %sysfunc(inputn(&amp;amp;start_yyyymm, yymmn6.)), &amp;amp;i), yymmn6.)
      %if &amp;amp;i ne %eval(&amp;amp;n_months-1) %then
        %do;
          union all
        %end;
    %end;
    ;
  quit;

%mend;

%create_view
  (
    view_name=v_3months, 
    base_name=sales,
    start_yyyymm=201312, 
    n_months=3
  );

data test;
  set v_3months;
  where product_id=1;
run;

proc print data=v_3months;
  where product_id=2;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Oct 2016 08:29:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-union-and-macro/m-p/302535#M64183</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2016-10-05T08:29:36Z</dc:date>
    </item>
  </channel>
</rss>

