<?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: Dynamic Proc SQL select into using do loop? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Proc-SQL-select-into-using-do-loop/m-p/295002#M61596</link>
    <description>&lt;P&gt;One would ask about why using lists at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* after making connections to oracle*/

proc sql;
   create table want as
   select OracleStudentData.*  /*or appropriate variables*/ f
   from YourDataSetOfStudentIds left join OracleStudentData
      on   YourDataSetOfStudentIds.IdVariable =  OracleStudentData.IdVariable;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hopefully your variable VariableToGroup is an Id variable similar to what I called IdVariable that you can match to the Oracle variable containing id values. No reason to use Macro variable and long where statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 29 Aug 2016 23:21:38 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2016-08-29T23:21:38Z</dc:date>
    <item>
      <title>Dynamic Proc SQL select into using do loop?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Proc-SQL-select-into-using-do-loop/m-p/294996#M61594</link>
      <description>&lt;P&gt;Our Oracle system doesn't allow lists of greater than 1,000 items. When using a cohort of students, and wanting to retrieve more about them from Oracle, we use Proc SQL to select into list groups of 1,000 items each as such:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql noprint;
	select VariableToGroup into :group_1 separated by ','
    from SASdataset (where=(count &amp;gt; 0 and COUNT &amp;lt;= 1000)) ;

	select VariableToGroup into :group_2 separated by ','
    from SASdataset (where=(count &amp;gt; 1000 and COUNT &amp;lt;= 2000)) ;

    select VariableToGroup into :group_3 separated by ','
    from SASdataset (where=(count &amp;gt; 2000 and COUNT &amp;lt;= 3000)) ;
			
    select VariableToGroup into :group_4 separated by ','
    from SASdataset (where=(count &amp;gt; 3000 and COUNT &amp;lt;= 4000)) ;

	select VariableToGroup into :group_5 separated by ','
    from SASdataset (where=(count &amp;gt; 4000 and COUNT &amp;lt;= 5000)) ;

	select VariableToGroup into :group_6 separated by ','
    from SASdataset (where=(count &amp;gt; 5000 and COUNT &amp;lt;= 6000)) ;              
quit;&lt;/PRE&gt;
&lt;P&gt;We then create a corresponding where statement:&lt;/P&gt;
&lt;PRE&gt;			WHERE	(
					ID in (&amp;amp;GROUP_1)
					OR ID in (&amp;amp;GROUP_2)
					OR ID in (&amp;amp;GROUP_3)
					OR ID in (&amp;amp;GROUP_4)
					OR ID in (&amp;amp;GROUP_5)
					OR ID in (&amp;amp;GROUP_6)
					OR ID in (&amp;amp;GROUP_7)
					OR ID in (&amp;amp;GROUP_8)
					OR ID in (&amp;amp;GROUP_9)
					OR ID in (&amp;amp;GROUP_10)
					)&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;How do I do this dynamically?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I may not have the best way, but I&amp;nbsp;&lt;EM&gt;can&lt;/EM&gt; get the number of observations in the VariableToGroup, divide that by 1,000 and then use a ceiling to get the number of groups to create. But I can't figure out how to use this to my advantage to dynamically create groups.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is my first failed attempt:&lt;/P&gt;
&lt;PRE&gt;%macro sqlloop(last=);
proc sql noprint;
	%do i=1 %to &amp;amp;last;
   	select variableToGroup into :group_&amp;amp;i separated by ','
        from SASdataset (where=(count &amp;gt; ((&amp;amp;i - 1)*1000) and COUNT &amp;lt;= (&amp;amp;i*1000))) ;
		    /*first time through, ^this^ should be 0*/
	%end;
quit;
%mend sqlloop;
%sqlloop(last=1012);&lt;/PRE&gt;
&lt;P&gt;There is probably quite a bit wrong with it, but it isn't working. It gives no error when calling it, but it doesn't create group_1, group_2, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Aug 2016 23:07:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Proc-SQL-select-into-using-do-loop/m-p/294996#M61594</guid>
      <dc:creator>GregG</dc:creator>
      <dc:date>2016-08-29T23:07:43Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Proc SQL select into using do loop?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Proc-SQL-select-into-using-do-loop/m-p/295002#M61596</link>
      <description>&lt;P&gt;One would ask about why using lists at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* after making connections to oracle*/

proc sql;
   create table want as
   select OracleStudentData.*  /*or appropriate variables*/ f
   from YourDataSetOfStudentIds left join OracleStudentData
      on   YourDataSetOfStudentIds.IdVariable =  OracleStudentData.IdVariable;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hopefully your variable VariableToGroup is an Id variable similar to what I called IdVariable that you can match to the Oracle variable containing id values. No reason to use Macro variable and long where statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Aug 2016 23:21:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Proc-SQL-select-into-using-do-loop/m-p/295002#M61596</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-08-29T23:21:38Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Proc SQL select into using do loop?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Proc-SQL-select-into-using-do-loop/m-p/295017#M61601</link>
      <description>&lt;P&gt;As of right now, we have to do quite a bit with Oracle to bring data to us. Because of this, we have switched to using passthrough (instead of libname), which has increased our query runtimes significantly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The studentIds often originate from previous queries involving the Oracle database, then using SAS to indentify cohorts (rather than some rather involved inline views or nested subqueries within SQL) - but the studentIds also come from other sources, most notably imported Excel sheets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have tried but was unable to join a sasdataset with an Oracle table within Proc SQL passthrough - is this possible?&lt;/P&gt;</description>
      <pubDate>Tue, 30 Aug 2016 00:22:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Proc-SQL-select-into-using-do-loop/m-p/295017#M61601</guid>
      <dc:creator>GregG</dc:creator>
      <dc:date>2016-08-30T00:22:22Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Proc SQL select into using do loop?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Proc-SQL-select-into-using-do-loop/m-p/295019#M61602</link>
      <description>&lt;P&gt;No, you can't use a SAS dataset with a pass through query.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you create temp tables on your Oracle DB? If so, use a libname method to create the table on your DB and then join on it there.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Aug 2016 00:24:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Proc-SQL-select-into-using-do-loop/m-p/295019#M61602</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-08-30T00:24:56Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Proc SQL select into using do loop?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Proc-SQL-select-into-using-do-loop/m-p/295022#M61603</link>
      <description>&lt;P&gt;I would use the dataset options FIRSTOBS and OBS to control gettng the groups. I would use the automatic macro variables SQLOBS to control stopping the loop.&lt;/P&gt;
&lt;P&gt;First lets make a sample dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ids;
  do id=1 to 25; output; end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's make a simple macro to build a series of macro variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro loop(size) ;
%global i ;
%let i=1;
%let sqlobs=0;
proc sql noprint;
%do %until(&amp;amp;sqlobs=0);
  %global group&amp;amp;i ;
  %let group&amp;amp;i=;
  select id into :group&amp;amp;i separated by ','
  from ids (firstobs=%eval(&amp;amp;size*(&amp;amp;i-1)+1) obs=%eval(&amp;amp;size*&amp;amp;i) )
  ;
  %if (&amp;amp;sqlobs) %then %do; 
    %put &amp;amp;=i (&amp;amp;&amp;amp;group&amp;amp;i) ;
    %let i=%eval(&amp;amp;i + 1);
  %end;
  %else %let i=%eval(&amp;amp;i - 1);
%end;
%mend loop;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now try calling it with different values for SIZE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%loop(10);
%put &amp;amp;=i ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then when you want to generate your query you can use another %DO loop to reference your macro variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do j=1 to &amp;amp;i ;
 or ID in (&amp;amp;&amp;amp;group&amp;amp;j)
%end;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Aug 2016 00:50:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Proc-SQL-select-into-using-do-loop/m-p/295022#M61603</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-08-30T00:50:38Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Proc SQL select into using do loop?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Proc-SQL-select-into-using-do-loop/m-p/295025#M61604</link>
      <description>&lt;P&gt;Tom, that works great with the sample dataset you provided (even increasing the id count from 25 to 3105).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But it causes an issue when I try it on my actual dataset (with 3105 actual ids, which are 18 characters long). It continually gives me the "must clear log" pop-up and will not continue until I choose an option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I should mention that there is a separate variable for the count, but you are using sqlobs so I don't know how that plays with this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The ids are unique, but 18 characters long. They already have the requisite single quotes ( '00000000ID00000000' ) around them, since they are character.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the full popup I am receiving:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Log WINDOW FULL&lt;/P&gt;
&lt;P&gt;Window is full and must be cleared. Select&lt;/P&gt;
&lt;P&gt;F to File,&lt;/P&gt;
&lt;P&gt;P to Print,&lt;/P&gt;
&lt;P&gt;S to Save or&amp;nbsp;&lt;/P&gt;
&lt;P&gt;C to Clear window without saving&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I must hand select one of the above before it continues for a few seconds, and then I have to select it again. I did this 10 times before I canceled the submitted statements.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Editted to add: I am processing the same exact list of ids in the first set of code without these popups.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Aug 2016 01:27:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Proc-SQL-select-into-using-do-loop/m-p/295025#M61604</guid>
      <dc:creator>GregG</dc:creator>
      <dc:date>2016-08-30T01:27:55Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Proc SQL select into using do loop?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Proc-SQL-select-into-using-do-loop/m-p/295026#M61605</link>
      <description>&lt;P&gt;You probably do NOT want the %PUT statement. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Aug 2016 01:42:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Proc-SQL-select-into-using-do-loop/m-p/295026#M61605</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-08-30T01:42:52Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Proc SQL select into using do loop?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Proc-SQL-select-into-using-do-loop/m-p/295028#M61606</link>
      <description>&lt;P&gt;It is even easier to do with a DATA step, mainly because the DO statement is much more flexible than the %DO statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  if eof then call symputx('ngroups',i);
  i+1;
  length group $32767 ;
  do j=1 to 10 while (not eof) ;
    set ids end=eof;
    group = catx(',',group,id);
  end;
  call symputx(cats('group',i),group);
run;
%put &amp;amp;=ngroups (&amp;amp;group2);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Aug 2016 01:51:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Proc-SQL-select-into-using-do-loop/m-p/295028#M61606</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-08-30T01:51:25Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Proc SQL select into using do loop?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Proc-SQL-select-into-using-do-loop/m-p/295044#M61615</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/39048"&gt;@GregG&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Tom, that works great with the sample dataset you provided (even increasing the id count from 25 to 3105).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But it causes an issue when I try it on my actual dataset (with 3105 actual ids, which are 18 characters long). It continually gives me the "must clear log" pop-up and will not continue until I choose an option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I should mention that there is a separate variable for the count, but you are using sqlobs so I don't know how that plays with this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The ids are unique, but 18 characters long. They already have the requisite single quotes ( '00000000ID00000000' ) around them, since they are character.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the full popup I am receiving:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Log WINDOW FULL&lt;/P&gt;
&lt;P&gt;Window is full and must be cleared. Select&lt;/P&gt;
&lt;P&gt;F to File,&lt;/P&gt;
&lt;P&gt;P to Print,&lt;/P&gt;
&lt;P&gt;S to Save or&amp;nbsp;&lt;/P&gt;
&lt;P&gt;C to Clear window without saving&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I must hand select one of the above before it continues for a few seconds, and then I have to select it again. I did this 10 times before I canceled the submitted statements.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Editted to add: I am processing the same exact list of ids in the first set of code without these popups.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;When you run into this issue check what's printing to your log. If it's unavoidable, use PROC PRINTTO to redirect your log. You can also consider option nonotes though I rarely find that to be a good idea.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Aug 2016 04:42:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Proc-SQL-select-into-using-do-loop/m-p/295044#M61615</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-08-30T04:42:38Z</dc:date>
    </item>
  </channel>
</rss>

