<?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 SAS Question: Loop through list of different criteria for one query in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-Question-Loop-through-list-of-different-criteria-for-one/m-p/754439#M237915</link>
    <description>&lt;P&gt;Hi everyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was researching a lot of forums and for some reason I can't seem to find what I'm looking for so I'm posting here. Apologies if I miss any details. I also won't be able to share too many details since I work with PHI data.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I'm ultimately trying to accomplish is creating a snippet of code that will loop through a list of accounts and start dates and pull information from one designated query where the account and start dates will change based on what is imported from the excel file. I will note here, the excel file is on a shared location and is updated each month to add or remove clients that need to be tracked&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As an example, here is what the excel (in it's simplest form) looks like:&lt;/P&gt;&lt;P&gt;A user can go in and either remove or add clients to this list. So the first "client" listed here, we would pull information starting from 202104 to current (&amp;amp;End_Date) which is already calculated but the second "Client" on here would need to have their information pulled starting in 201905&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Account_Name&lt;/TD&gt;&lt;TD&gt;Account_Number&lt;/TD&gt;&lt;TD&gt;Start_Date&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Alpha&lt;/TD&gt;&lt;TD&gt;1A1234B&lt;/TD&gt;&lt;TD&gt;202104&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Beta&lt;/TD&gt;&lt;TD&gt;1B5489H&lt;/TD&gt;&lt;TD&gt;201905&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Gamma&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;1C5421J&lt;/TD&gt;&lt;TD&gt;202101&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Delta&lt;/TD&gt;&lt;TD&gt;1W6811C&lt;/TD&gt;&lt;TD&gt;202012&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Epsilon&lt;/TD&gt;&lt;TD&gt;1G1234U&lt;/TD&gt;&lt;TD&gt;201911&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code pasted below is what I'm trying to update. Here the &amp;amp;Account_Number and &amp;amp;Start_Date would need change as the loop processes until it reaches the end of the list. Each client will have it's own output table&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;PROC SQL;
CREATE TABLE &amp;amp;Account_Name_&amp;amp;End_Date AS 
SELECT 
ACCOUNT, 
ACCOUNT_NAME, 
PERIOD_ID,
SUM(COUNT_MON_NUM) AS MEMBERS 
FROM 
DATABASE.SR_MBR_MON 
WHERE ACCOUNT = &lt;U&gt;&lt;STRONG&gt;&amp;amp;ACCOUNT_NUMBER&lt;/STRONG&gt; &lt;/U&gt;
AND PERIOD_ID BETWEEN &lt;U&gt;&lt;STRONG&gt;&amp;amp;START_DATE&lt;/STRONG&gt;&lt;/U&gt; AND &amp;amp;END_DATE
GROUP BY 1,2
;QUIT;&lt;/PRE&gt;&lt;P&gt;Any help would be greatly appreciated and please let me know if I need to be more specific with my ask - thank you in advanced!&amp;nbsp;&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>Thu, 15 Jul 2021 20:12:48 GMT</pubDate>
    <dc:creator>Pattyp33</dc:creator>
    <dc:date>2021-07-15T20:12:48Z</dc:date>
    <item>
      <title>SAS Question: Loop through list of different criteria for one query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Question-Loop-through-list-of-different-criteria-for-one/m-p/754439#M237915</link>
      <description>&lt;P&gt;Hi everyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was researching a lot of forums and for some reason I can't seem to find what I'm looking for so I'm posting here. Apologies if I miss any details. I also won't be able to share too many details since I work with PHI data.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I'm ultimately trying to accomplish is creating a snippet of code that will loop through a list of accounts and start dates and pull information from one designated query where the account and start dates will change based on what is imported from the excel file. I will note here, the excel file is on a shared location and is updated each month to add or remove clients that need to be tracked&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As an example, here is what the excel (in it's simplest form) looks like:&lt;/P&gt;&lt;P&gt;A user can go in and either remove or add clients to this list. So the first "client" listed here, we would pull information starting from 202104 to current (&amp;amp;End_Date) which is already calculated but the second "Client" on here would need to have their information pulled starting in 201905&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Account_Name&lt;/TD&gt;&lt;TD&gt;Account_Number&lt;/TD&gt;&lt;TD&gt;Start_Date&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Alpha&lt;/TD&gt;&lt;TD&gt;1A1234B&lt;/TD&gt;&lt;TD&gt;202104&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Beta&lt;/TD&gt;&lt;TD&gt;1B5489H&lt;/TD&gt;&lt;TD&gt;201905&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Gamma&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;1C5421J&lt;/TD&gt;&lt;TD&gt;202101&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Delta&lt;/TD&gt;&lt;TD&gt;1W6811C&lt;/TD&gt;&lt;TD&gt;202012&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Epsilon&lt;/TD&gt;&lt;TD&gt;1G1234U&lt;/TD&gt;&lt;TD&gt;201911&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code pasted below is what I'm trying to update. Here the &amp;amp;Account_Number and &amp;amp;Start_Date would need change as the loop processes until it reaches the end of the list. Each client will have it's own output table&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;PROC SQL;
CREATE TABLE &amp;amp;Account_Name_&amp;amp;End_Date AS 
SELECT 
ACCOUNT, 
ACCOUNT_NAME, 
PERIOD_ID,
SUM(COUNT_MON_NUM) AS MEMBERS 
FROM 
DATABASE.SR_MBR_MON 
WHERE ACCOUNT = &lt;U&gt;&lt;STRONG&gt;&amp;amp;ACCOUNT_NUMBER&lt;/STRONG&gt; &lt;/U&gt;
AND PERIOD_ID BETWEEN &lt;U&gt;&lt;STRONG&gt;&amp;amp;START_DATE&lt;/STRONG&gt;&lt;/U&gt; AND &amp;amp;END_DATE
GROUP BY 1,2
;QUIT;&lt;/PRE&gt;&lt;P&gt;Any help would be greatly appreciated and please let me know if I need to be more specific with my ask - thank you in advanced!&amp;nbsp;&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>Thu, 15 Jul 2021 20:12:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Question-Loop-through-list-of-different-criteria-for-one/m-p/754439#M237915</guid>
      <dc:creator>Pattyp33</dc:creator>
      <dc:date>2021-07-15T20:12:48Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Question: Loop through list of different criteria for one query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Question-Loop-through-list-of-different-criteria-for-one/m-p/754441#M237917</link>
      <description>&lt;P&gt;1. Import your Excel file and join the files&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See example query below, you didn't illustrate where End date comes from though so I just assumed that's a different macro variable and you can fill that in.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
CREATE TABLE FULL_FILE AS 
SELECT 
ACCOUNT, 
ACCOUNT_NAME, 
PERIOD_ID,
SUM(COUNT_MON_NUM) AS MEMBERS 
FROM 
DATABASE.SR_MBR_MON AS A
LEFT JOIN EXCELFILE as B
WHERE A.ACCOUNT = B.ACCOUNT_NUMBER
AND A.PERIOD_ID BETWEEN B.START_DATE AND &amp;amp;END_DATE
GROUP BY 1,2
;QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;2. Write a macro and use CALL EXECUTE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*assumes you have already imported the excel file;

%macro export_data(account_name = , start_date= , end_date=);

PROC SQL;
CREATE TABLE &amp;amp;Account_Name_&amp;amp;End_Date AS 
SELECT 
ACCOUNT, 
ACCOUNT_NAME, 
PERIOD_ID,
SUM(COUNT_MON_NUM) AS MEMBERS 
FROM 
DATABASE.SR_MBR_MON 
WHERE ACCOUNT = &amp;amp;ACCOUNT_NUMBER 
AND PERIOD_ID BETWEEN &amp;amp;START_DATE AND &amp;amp;END_DATE
GROUP BY 1,2
;QUIT;

%mend;

data make_reports;
set excelFile;
str = catt('%make_report(account_number = ',
                account_number, 
                ' , start_date = ',
                start_date, 
                ' , end_date = ',
                end_date,
               ');');
call execute(str);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;UCLA introductory tutorial on macro variables and macros&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Tutorial on converting a working program to a macro&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;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank"&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;Examples of common macro usage&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"&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;</description>
      <pubDate>Thu, 15 Jul 2021 20:25:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Question-Loop-through-list-of-different-criteria-for-one/m-p/754441#M237917</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-07-15T20:25:31Z</dc:date>
    </item>
  </channel>
</rss>

