<?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: Loop inside Proc SQL using macro variables in where clause in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loop-inside-Proc-SQL-using-macro-variables-in-where-clause/m-p/735065#M228975</link>
    <description>&lt;P&gt;If that is a concern, the account table should be uploaded to the DB and the query run in a pass-through.&lt;/P&gt;
&lt;P&gt;I mainly wanted to show that no macro loop is needed.&lt;/P&gt;</description>
    <pubDate>Sun, 18 Apr 2021 09:11:34 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-04-18T09:11:34Z</dc:date>
    <item>
      <title>Loop inside Proc SQL using macro variables in where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-inside-Proc-SQL-using-macro-variables-in-where-clause/m-p/734977#M228953</link>
      <description>&lt;P&gt;I have a dataset with 3 Columns and hundreds of observations as in the snapshot. Using these values, I have to pull data from a Database with the below where clause -&lt;/P&gt;&lt;P&gt;Account = 'ACCOUNT_NBR'&lt;BR /&gt;and Alert_Date &amp;gt;= START_DT&lt;BR /&gt;and Alert_Date &amp;lt;= END_DT&lt;/P&gt;&lt;P&gt;For each account number in the dataset the Start and End dates are different and I want to loop it using macro variables, I could manage one but its generating 1 table per 1 where clause.&amp;nbsp;I want to create a single table which satisfies the above where clause with macros.&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Sample.JPG" style="width: 354px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/58223i0073E43C1CD9ED6B/image-size/large?v=v2&amp;amp;px=999" role="button" title="Sample.JPG" alt="Sample.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Apr 2021 23:25:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-inside-Proc-SQL-using-macro-variables-in-where-clause/m-p/734977#M228953</guid>
      <dc:creator>pradeep_kadasi</dc:creator>
      <dc:date>2021-04-17T23:25:57Z</dc:date>
    </item>
    <item>
      <title>Re: Loop inside Proc SQL using macro variables in where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-inside-Proc-SQL-using-macro-variables-in-where-clause/m-p/734980#M228954</link>
      <description>Please show us the full code that you tried. What is Alert_Date, it is not in your data set.&lt;BR /&gt;&lt;BR /&gt;Where would the loop fit in? Explain that part? SQL ought to be able to select observations that meet a condition without a loop.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Sun, 18 Apr 2021 00:40:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-inside-Proc-SQL-using-macro-variables-in-where-clause/m-p/734980#M228954</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-04-18T00:40:18Z</dc:date>
    </item>
    <item>
      <title>Re: Loop inside Proc SQL using macro variables in where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-inside-Proc-SQL-using-macro-variables-in-where-clause/m-p/735000#M228958</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Alert_Date is in the Database from which I intend to pull data with a where clause which is not static. Below is how I want -&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1st Iteration-&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Account = X1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;and Alert_Date &amp;gt;= 14MAR2021&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;and Alert_Date &amp;lt;= 13APR2021&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2nd Iteration-&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Account = X2&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;and Alert_Date &amp;gt;= 20FEB2021&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;and Alert_Date &amp;lt;= 22MAR2021&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;and so on...all the values are in the data set I have using which I need to pull data from a DB.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Below is the code I tried (includes multiple blocks) -&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;*This code is for getting count of records, add index to each record.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Data Temp3;&lt;BR /&gt;Set mylib.Alerts_Distinct END=Last;&lt;BR /&gt;By DECISION;&lt;BR /&gt;Retain Counter;&lt;/P&gt;&lt;P&gt;If (First.DECISION) then Do;&lt;BR /&gt;Counter=1;&lt;BR /&gt;End;&lt;BR /&gt;Else Do;&lt;BR /&gt;Counter+1;&lt;BR /&gt;End;&lt;BR /&gt;Run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;*This code is for creating the macro variables for Account number, Start and End dates&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Options Symbolgen;&lt;/P&gt;&lt;P&gt;DATA Temp4;&lt;BR /&gt;SET Temp3 END=LAST;&lt;/P&gt;&lt;P&gt;If LAST then&lt;BR /&gt;Call SYMPUT('Count', PUT(Counter, 10.));&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;Data Temp5;&lt;BR /&gt;Set Temp4;&lt;BR /&gt;call symputx('ACCOUNT_NUMBER'||Left(put(_n_,5.)), CURRENT_ACCOUNT_NBR);&lt;BR /&gt;call symputx('START_DATE'||Left(put(_n_,5.)), LOSS_START_DT);&lt;BR /&gt;call symputx('END_DATE'||Left(put(_n_,5.)), LOSS_DT);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;The above code generates&amp;nbsp;&lt;/P&gt;&lt;P&gt;ACCOUNT_NUMBER1,&amp;nbsp;ACCOUNT_NUMBER2 etc. holding values of Account numbers&lt;/P&gt;&lt;P&gt;START_DATE1,&amp;nbsp;START_DATE2 etc. holding the values of start dates of each records&lt;/P&gt;&lt;P&gt;END_DATE1,&amp;nbsp;END_DATE2 etc.&amp;nbsp;holding the values of end dates of each records&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro sqlloop(start,end);&lt;/P&gt;&lt;P&gt;Proc Sql;&lt;BR /&gt;%DO i=&amp;amp;start. %TO &amp;amp;end.;&lt;BR /&gt;create table Alerts as&lt;BR /&gt;select&amp;nbsp; * from Database&lt;BR /&gt;where&lt;BR /&gt;CONTACT_VALUE = '&amp;amp;ACCOUNT_NUMBER&amp;amp;i'&lt;BR /&gt;and Alert_Date &amp;gt;= %Format('&amp;amp;START_DATE&amp;amp;i',Date9.)&lt;BR /&gt;and Alert_Date &amp;lt;= %Format('&amp;amp;END_DATE&amp;amp;i',Date9.)&lt;BR /&gt;;&lt;BR /&gt;%END;&lt;BR /&gt;Quit;&lt;/P&gt;&lt;P&gt;%Mend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%sqlloop(start=1, end=&amp;amp;Count.);&lt;/P&gt;</description>
      <pubDate>Sun, 18 Apr 2021 01:45:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-inside-Proc-SQL-using-macro-variables-in-where-clause/m-p/735000#M228958</guid>
      <dc:creator>pradeep_kadasi</dc:creator>
      <dc:date>2021-04-18T01:45:23Z</dc:date>
    </item>
    <item>
      <title>Re: Loop inside Proc SQL using macro variables in where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-inside-Proc-SQL-using-macro-variables-in-where-clause/m-p/735037#M228965</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/325434"&gt;@pradeep_kadasi&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The way how this is done normally:&lt;/P&gt;
&lt;P&gt;1. you upload your small table into the database (could be a temporary table)&lt;/P&gt;
&lt;P&gt;2. you inner join your small table in the database with the big table in the database&lt;/P&gt;
&lt;P&gt;3. you download the result set (that's just happening via the create table statement in the SAS SQL portion).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.lexjansen.com/sesug/2018/SESUG2018_Paper-154_Final_PDF.pdf" target="_self"&gt;Here&lt;/A&gt; a whitepaper describing the approach for joining a SAS with a Teradata table. It's in principle the same for any DB.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could of course define a SAS macro which generates code like....&lt;/P&gt;
&lt;PRE&gt;...
from db_table
where account_id='a' and from_date=.. and to_date=...
or  account_id='b' and from_date=.. and to_date=...
or  account_id='c' and from_date=.. and to_date=...
.....

&lt;/PRE&gt;
&lt;P&gt;...but this would result in a rather inefficient query plus at least some DB's have also an upper limit for how much code a single SQL can contain (it used to be 32KB for Oracle; may be different for recent versions).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't recommend to use this approach but below just to demonstrate how generation of a where clause could work. I'm using a data step and not a macro to generate the code as this allows me to print the generated code in a way that's easy to read and without executing it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;  
  infile datalines truncover dsd dlm=' ';
  input account $ (start_dt end_dt) (:$9.);
datalines;
a 01jan2019 15jan2020
b 10feb2020 15feb2020
c 15mar2020 10mar2021
;

filename codegen temp;
data _null_;
/*  file codegen;*/
  file print;
  set have end=last;
  if _n_=1 then
    do;
      put 
        'proc sql;' /
        '  create table want as ' /
        '    select * ' /
        '    from db.table ' /
        '    where ' 
        ;
    end;
    put
      "      "
      "account='" account +(-1) "' and "
      "start_dt='" start_dt +(-1) "'d and "
      "end_dt='" end_dt +(-1) "'d"
      @ ;
    if not last then put " or";
    else put;
    if last then
      do;
        put
          '    ;' /
          'quit;'
          ;
      end;
run;

data _null_;
  file codegen mod;
  stop;
run;
%include codegen /source2;
filename codegen clear;
 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1618724440063.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/58271i7209941A52B05416/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1618724440063.png" alt="Patrick_0-1618724440063.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;...or even simpler but also not really suitable for hundreds of where conditions....&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql noprint;
  select 
    cats("account='",account,"'")
    ||' and '||
    cats("start_dt='",start_dt,"d'")
    ||' and '||
    cats("end_dt='",end_dt,"d'")
    into :where_cond separated by ' or '
  from have
  ;
quit;
%put %nrbquote(&amp;amp;where_cond);

proc sql noprint; 
  create table want as  
    select *  
    from db.table  
    where  
      %nrbquote(&amp;amp;where_cond)
    ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Apr 2021 05:47:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-inside-Proc-SQL-using-macro-variables-in-where-clause/m-p/735037#M228965</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-04-18T05:47:05Z</dc:date>
    </item>
    <item>
      <title>Re: Loop inside Proc SQL using macro variables in where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-inside-Proc-SQL-using-macro-variables-in-where-clause/m-p/735063#M228973</link>
      <description>&lt;P&gt;One join is all that's needed:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select b.*
  from account a left join mylib.Alerts_Distinct b
  on a.account_nbr = b.account and b.alert_date between a.start_dt and a.end_dt
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 18 Apr 2021 09:06:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-inside-Proc-SQL-using-macro-variables-in-where-clause/m-p/735063#M228973</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-04-18T09:06:49Z</dc:date>
    </item>
    <item>
      <title>Re: Loop inside Proc SQL using macro variables in where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-inside-Proc-SQL-using-macro-variables-in-where-clause/m-p/735064#M228974</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;Isn't that going to download the full big DB table into SAS prior to joining?&lt;/P&gt;</description>
      <pubDate>Sun, 18 Apr 2021 09:07:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-inside-Proc-SQL-using-macro-variables-in-where-clause/m-p/735064#M228974</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-04-18T09:07:51Z</dc:date>
    </item>
    <item>
      <title>Re: Loop inside Proc SQL using macro variables in where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-inside-Proc-SQL-using-macro-variables-in-where-clause/m-p/735065#M228975</link>
      <description>&lt;P&gt;If that is a concern, the account table should be uploaded to the DB and the query run in a pass-through.&lt;/P&gt;
&lt;P&gt;I mainly wanted to show that no macro loop is needed.&lt;/P&gt;</description>
      <pubDate>Sun, 18 Apr 2021 09:11:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-inside-Proc-SQL-using-macro-variables-in-where-clause/m-p/735065#M228975</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-04-18T09:11:34Z</dc:date>
    </item>
    <item>
      <title>Re: Loop inside Proc SQL using macro variables in where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-inside-Proc-SQL-using-macro-variables-in-where-clause/m-p/735108#M228992</link>
      <description>&lt;P&gt;You have design problems with your macro.&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The loop is overwriting the target dataset so only the results of the last one will exist.&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;To use a macro variable, like I, as a suffix on macro variable name you need to double the initial &amp;amp; so it will re-process the resulting name with the suffix added.&lt;/LI&gt;
&lt;LI&gt;Macro code inside strings bounded by single quotes is ignored. So either use double quotes. Or just generate the macro variables in the format needed and don't add in extra quotes.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example you could create separate datasets.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro sqlloop(start,end);
%local i;
proc sql;
%do i=&amp;amp;start. %TO &amp;amp;end.;
create table Alerts&amp;amp;i as
select  * from mylib.mydataset
where CONTACT_VALUE = "&amp;amp;&amp;amp;ACCOUNT_NUMBER&amp;amp;i"
  and Alert_Date between &amp;amp;&amp;amp;start_date&amp;amp;i and &amp;amp;&amp;amp;end_date&amp;amp;i
;
%end;
quit;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also the code that is generating the series of macro variables could be a lot simpler&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set temp3 end=last;
  call symputx(cats('account_number',_n_), current_account_nbr);
  call symputx(cats('start_date',_n_), loss_start_dt);
  call symputx(cats('end_date',_n_), loss_dt);
  if last then call symputx('count',_n_);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you are going to use pass thru SQL (unlike the example code you showed) then format the dates in the style that the SQL of that remote database needs.&amp;nbsp; For example many want dates to look like &lt;FONT face="courier new,courier"&gt;'2021-04-18'&lt;/FONT&gt;.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;...
  call symputx(cats('start_date',_n_), quote(put(loss_start_dt,yymmdd10.),"'"));
  call symputx(cats('end_date',_n_), quote(put(loss_dt,yymmdd10.),"'"));
...

%macro sqlloop(start,end);
%local i;
proc sql;
connect using mylib;
%do i=&amp;amp;start. %TO &amp;amp;end.;
create table Alerts&amp;amp;i as
select  * from connection to mylib
(select * from mydataset
where CONTACT_VALUE = "&amp;amp;&amp;amp;ACCOUNT_NUMBER&amp;amp;i"
  and Alert_Date between &amp;amp;&amp;amp;start_date&amp;amp;i and &amp;amp;&amp;amp;end_date&amp;amp;i
)
;
%end;
quit;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 18 Apr 2021 15:51:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-inside-Proc-SQL-using-macro-variables-in-where-clause/m-p/735108#M228992</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-04-18T15:51:05Z</dc:date>
    </item>
  </channel>
</rss>

