<?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: Macro for dataset with multiple years in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-dataset-with-multiple-years/m-p/617240#M180830</link>
    <description>&lt;P&gt;&lt;STRONG&gt;UCLA introductory tutorial on macro variables and macros&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/" target="_blank" rel="noopener"&gt;https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;Tutorial on converting a working program to a macro&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_self"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;Examples of common macro usage&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're almost there, just wrap it in a macro now with the parameter. The tutorial above gives you step by step examples.&lt;STRONG&gt; I would recommend combining the data sets if they're reasonably sized though (less than a few million per) and use BY group processing instead. Both options are illustrated below. I fixed your SQL assuming I understand what you want correctly.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro count_data(year=);
PROC SQL;

CREATE TABLE COUNT_&amp;amp;YEAR as

select STUDENT_ZIP,

COUNT(DISTINCT STUDENT_ID) AS TOTAL_&amp;amp;YEAR

FROM

STUDENT_&amp;amp;YEAR

QUIT;

%mend count_data;

%count_data(year=2014);
%count_data(year=2015);
...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or instead use this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data combined / view= combined;
set student_2014-student_2017 indsname=source;
dsn=source;
run;

proc sql;
create table summary as
select dsn, student_zip, count(distinct student_id) as total
from combined
&lt;FONT size="4" color="#FF0000"&gt;&lt;STRONG&gt;group by dsn; /*edited in*/&lt;/STRONG&gt;&lt;/FONT&gt;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 14 Jan 2020 18:31:45 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2020-01-14T18:31:45Z</dc:date>
    <item>
      <title>Macro for dataset with multiple years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-dataset-with-multiple-years/m-p/617237#M180828</link>
      <description>&lt;P&gt;I have a set of 4 tables that are named the same as below and I need to pull the same data from each table&amp;nbsp;for years:&amp;nbsp;2014-2017. How can I do this&amp;nbsp;using a macro&amp;nbsp;within a data step or with PROC SQL so that I am not re-writing the same code for each year? Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Student_2014&lt;/P&gt;&lt;P&gt;Student_2015&lt;/P&gt;&lt;P&gt;Student_2016&lt;/P&gt;&lt;P&gt;Student_2017&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;PROC&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;SQL&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;CREATE&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;TABLE&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; COUNT_&amp;amp;YEAR&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;STUDENT_ZIP,&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;COUNT(&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;DISTINCT&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; STUDENT_ID) &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;AS&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; TOTAL_&amp;amp;YEAR&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;FROM&lt;/P&gt;&lt;P&gt;STUDENT_&amp;amp;YEAR&lt;/P&gt;&lt;P&gt;QUIT;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2020 16:24:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-dataset-with-multiple-years/m-p/617237#M180828</guid>
      <dc:creator>AP718</dc:creator>
      <dc:date>2020-01-14T16:24:53Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for dataset with multiple years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-dataset-with-multiple-years/m-p/617240#M180830</link>
      <description>&lt;P&gt;&lt;STRONG&gt;UCLA introductory tutorial on macro variables and macros&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/" target="_blank" rel="noopener"&gt;https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;Tutorial on converting a working program to a macro&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_self"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;Examples of common macro usage&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're almost there, just wrap it in a macro now with the parameter. The tutorial above gives you step by step examples.&lt;STRONG&gt; I would recommend combining the data sets if they're reasonably sized though (less than a few million per) and use BY group processing instead. Both options are illustrated below. I fixed your SQL assuming I understand what you want correctly.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro count_data(year=);
PROC SQL;

CREATE TABLE COUNT_&amp;amp;YEAR as

select STUDENT_ZIP,

COUNT(DISTINCT STUDENT_ID) AS TOTAL_&amp;amp;YEAR

FROM

STUDENT_&amp;amp;YEAR

QUIT;

%mend count_data;

%count_data(year=2014);
%count_data(year=2015);
...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or instead use this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data combined / view= combined;
set student_2014-student_2017 indsname=source;
dsn=source;
run;

proc sql;
create table summary as
select dsn, student_zip, count(distinct student_id) as total
from combined
&lt;FONT size="4" color="#FF0000"&gt;&lt;STRONG&gt;group by dsn; /*edited in*/&lt;/STRONG&gt;&lt;/FONT&gt;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2020 18:31:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-dataset-with-multiple-years/m-p/617240#M180830</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-01-14T18:31:45Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for dataset with multiple years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-dataset-with-multiple-years/m-p/617262#M180840</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/140206"&gt;@AP718&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a set of 4 tables that are named the same as below and I need to pull the same data from each table&amp;nbsp;for years:&amp;nbsp;2014-2017. How can I do this&amp;nbsp;using a macro&amp;nbsp;within a data step or with PROC SQL so that I am not re-writing the same code for each year? Thank you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Student_2014&lt;/P&gt;
&lt;P&gt;Student_2015&lt;/P&gt;
&lt;P&gt;Student_2016&lt;/P&gt;
&lt;P&gt;Student_2017&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#000080"&gt;&lt;STRONG&gt;PROC&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT face="Courier New" size="3" color="#000080"&gt;SQL&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;CREATE&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;TABLE&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; COUNT_&amp;amp;YEAR&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;STUDENT_ZIP,&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;COUNT(&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;DISTINCT&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; STUDENT_ID) &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;AS&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; TOTAL_&amp;amp;YEAR&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;FROM&lt;/P&gt;
&lt;P&gt;STUDENT_&amp;amp;YEAR&lt;/P&gt;
&lt;P&gt;QUIT;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I wouldn't use a macro at all. I would append all the data sets, then do your analysis with GROUP BY year. That seems much simpler than trying to create a working macro here.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2020 17:40:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-dataset-with-multiple-years/m-p/617262#M180840</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-01-14T17:40:12Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for dataset with multiple years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-dataset-with-multiple-years/m-p/617263#M180841</link>
      <description>&lt;P&gt;Thank you! do I have to assign the years as it's not running in the Proc SQL version&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%LET&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; YEAR = &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%PUT&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; &amp;amp;YEAR; &lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2020 17:42:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-dataset-with-multiple-years/m-p/617263#M180841</guid>
      <dc:creator>AP718</dc:creator>
      <dc:date>2020-01-14T17:42:17Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for dataset with multiple years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-dataset-with-multiple-years/m-p/617264#M180842</link>
      <description>&lt;P&gt;If you do a GROUP BY in PROC SQL, you won't need macros at all.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2020 17:44:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-dataset-with-multiple-years/m-p/617264#M180842</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-01-14T17:44:05Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for dataset with multiple years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-dataset-with-multiple-years/m-p/617268#M180844</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/140206"&gt;@AP718&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you! do I have to assign the years as it's not running in the Proc SQL version&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;%LET&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; YEAR = &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;%PUT&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; &amp;amp;YEAR; &lt;/FONT&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;No, assuming you're using the code I posted. If you wrote your own or changed it then I don't know, you'd have to post the full code.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general, make sure it works for one case, you have working code without macros, and then follow the tutorial and you should be fine.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2020 18:07:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-dataset-with-multiple-years/m-p/617268#M180844</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-01-14T18:07:24Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for dataset with multiple years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-dataset-with-multiple-years/m-p/617269#M180845</link>
      <description>&lt;P&gt;the goal is to not have to write the same query multiple times to pull the same data from the 4 tables&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2020 18:07:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-dataset-with-multiple-years/m-p/617269#M180845</guid>
      <dc:creator>AP718</dc:creator>
      <dc:date>2020-01-14T18:07:57Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for dataset with multiple years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-dataset-with-multiple-years/m-p/617271#M180847</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/140206"&gt;@AP718&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;the goal is to not have to write the same query multiple times to pull the same data from the 4 tables&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, you use the PROC SQL code from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;but you add a GROUP BY ... and you get a solution with one query and no macros.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2020 18:17:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-dataset-with-multiple-years/m-p/617271#M180847</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-01-14T18:17:22Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for dataset with multiple years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-dataset-with-multiple-years/m-p/617272#M180848</link>
      <description>I posted two, different, full working solutions based on what you posted. Did you run them exactly as is? If so, did they not work? If not, specify how exactly.</description>
      <pubDate>Tue, 14 Jan 2020 18:20:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-dataset-with-multiple-years/m-p/617272#M180848</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-01-14T18:20:41Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for dataset with multiple years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-dataset-with-multiple-years/m-p/617277#M180849</link>
      <description>&lt;P&gt;I had to fix a string and it worked. Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2020 18:42:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-dataset-with-multiple-years/m-p/617277#M180849</guid>
      <dc:creator>AP718</dc:creator>
      <dc:date>2020-01-14T18:42:25Z</dc:date>
    </item>
  </channel>
</rss>

