<?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: Parameterizing Proc SQL with Arrays and Iterating Using Nested Loops in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71184#M15383</link>
    <description>There is a large number of iterations, about 500K total.  I reformulated the macro as you describe to perform the proc sql once per iteration and it seems to work right now.  Its been running for about 10min which is longer than the normal 3min.  So I might have a solution.

more&lt;BR /&gt;
&lt;BR /&gt;
    &lt;BR /&gt;
Message was edited by: SteveONCSU</description>
    <pubDate>Wed, 16 Sep 2009 12:44:57 GMT</pubDate>
    <dc:creator>StephenOverton</dc:creator>
    <dc:date>2009-09-16T12:44:57Z</dc:date>
    <item>
      <title>Parameterizing Proc SQL with Arrays and Iterating Using Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71168#M15367</link>
      <description>I've got an interesting situation due to an interesting request.  I've googled for a while but can't find the simple solution I think I'm looking for.  I want to use nested "do over" loops to go through all possibilities of 2 arrays inside a proc sql statement.&lt;BR /&gt;
&lt;BR /&gt;
I've got a query that returns results exactly how I need them I just need to iterate through every possible value of two variables underlined and highlighted bold below.  I've tried using entirely SQL by using sub-selects but performance is horrible due to table size.  The query cannot change much as I need it to sort and limit by 1 to find the first occurance of an so_pk for each unit_pk and service_cat_desc.  I've highlighted in &lt;B&gt;bold&lt;/B&gt; what I need to iterate through.&lt;BR /&gt;
&lt;BR /&gt;
I also need to understand how to load an array using proc sql (so I can actually use the two arrays needed).  I somewhat understand how to load using a data step but I will only have 1 column of observations that needs to be loaded into the array.  I feel like I need to transpose the results of SQL so they can be loaded into an array.&lt;BR /&gt;
&lt;BR /&gt;
Here is the query that needs to be parameterized and looped through using nested array loops:&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
select&lt;BR /&gt;
  orig.unit_pk,&lt;BR /&gt;
  cb_cntr_service_category.service_cat_desc,&lt;BR /&gt;
  orig.so_pk,&lt;BR /&gt;
  min(sub_prod_pckg_pk) as pk&lt;BR /&gt;
&lt;BR /&gt;
from&lt;BR /&gt;
  cb_subs_prod_pckg as orig&lt;BR /&gt;
  left outer join cb_cntr_service_type on&lt;BR /&gt;
    orig.service_type_code = cb_cntr_service_type.service_type_code&lt;BR /&gt;
  left outer join cb_cntr_service_category on&lt;BR /&gt;
    cb_cntr_service_category.service_cat_code = cb_cntr_service_type.service_cat_code&lt;BR /&gt;
&lt;BR /&gt;
where&lt;BR /&gt;
  orig.so_pk is not null and&lt;BR /&gt;
  orig.unit_pk = &lt;B&gt;&lt;U&gt;54&lt;/U&gt; &lt;/B&gt;and&lt;BR /&gt;
  cb_cntr_service_category.service_cat_desc = &lt;B&gt;&lt;U&gt;'Video'&lt;/U&gt;&lt;/B&gt;&lt;BR /&gt;
&lt;BR /&gt;
group by&lt;BR /&gt;
  orig.unit_pk,&lt;BR /&gt;
  cb_cntr_service_category.service_cat_desc,&lt;BR /&gt;
  orig.so_pk&lt;BR /&gt;
&lt;BR /&gt;
order by&lt;BR /&gt;
  orig.unit_pk,&lt;BR /&gt;
  cb_cntr_service_category.service_cat_desc,&lt;BR /&gt;
  pk&lt;BR /&gt;
&lt;BR /&gt;
limit 1&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
To summarize I'm looking for the following:&lt;BR /&gt;
1) How to load an array using a single column of data returned by proc sql.&lt;BR /&gt;
2) How to loop through a proc sql statement with arrays and store the results in a table.&lt;BR /&gt;
&lt;BR /&gt;
Thanks!</description>
      <pubDate>Fri, 11 Sep 2009 20:36:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71168#M15367</guid>
      <dc:creator>StephenOverton</dc:creator>
      <dc:date>2009-09-11T20:36:42Z</dc:date>
    </item>
    <item>
      <title>Re: Parameterizing Proc SQL with Arrays and Iterating Using Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71169#M15368</link>
      <description>Not sure if I fully understand your problem. Hopefully the following helps anyway:&lt;BR /&gt;
&lt;BR /&gt;
As you use "group by" you will get one observation for each distinct combination of orig.unit_pk,cb_cntr_service_category.service_cat_desc,orig.so_pk. &lt;BR /&gt;
The value for pk will be the minimum value per distinct combination of these 3 columns.&lt;BR /&gt;
&lt;BR /&gt;
As you select in your where close orig.unit_pk = 54 and cb_cntr_service_category.service_cat_desc = 'Video'   only rows with these two values are selected and joined.&lt;BR /&gt;
(actually: I'm not sure whether the joins happen first and then the where clause subsets the result or the other way round. You might want to try and have the selections already applied during the joins in the "on.." part to improve performance).&lt;BR /&gt;
&lt;BR /&gt;
The "order by" is not necessary, "group by" sorts already.&lt;BR /&gt;
&lt;BR /&gt;
select&lt;BR /&gt;
orig.unit_pk,&lt;BR /&gt;
cb_cntr_service_category.service_cat_desc,&lt;BR /&gt;
orig.so_pk,&lt;BR /&gt;
min(sub_prod_pckg_pk) as pk&lt;BR /&gt;
&lt;BR /&gt;
from&lt;BR /&gt;
cb_subs_prod_pckg as orig&lt;BR /&gt;
left outer join cb_cntr_service_type on&lt;BR /&gt;
orig.service_type_code = cb_cntr_service_type.service_type_code&lt;BR /&gt;
left outer join cb_cntr_service_category on&lt;BR /&gt;
cb_cntr_service_category.service_cat_code = cb_cntr_service_type.service_cat_code&lt;BR /&gt;
&lt;BR /&gt;
where&lt;BR /&gt;
orig.so_pk is not null and&lt;BR /&gt;
orig.unit_pk = 54 and&lt;BR /&gt;
cb_cntr_service_category.service_cat_desc = 'Video'&lt;BR /&gt;
&lt;BR /&gt;
group by&lt;BR /&gt;
orig.unit_pk,&lt;BR /&gt;
cb_cntr_service_category.service_cat_desc,&lt;BR /&gt;
orig.so_pk&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
"....find the first occurance of an so_pk ..." &lt;BR /&gt;
As you use "group by" you will only get one row per combination. As the only other variable is a minimum it doesn't matter whether this is "the first one" or not (the "first one" wouldn't be possible).&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick</description>
      <pubDate>Sat, 12 Sep 2009 11:14:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71169#M15368</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2009-09-12T11:14:04Z</dc:date>
    </item>
    <item>
      <title>Re: Parameterizing Proc SQL with Arrays and Iterating Using Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71170#M15369</link>
      <description>orig.unit_pk = 54 and cb_cntr_service_category.service_cat_desc = 'Video'&lt;BR /&gt;
&lt;BR /&gt;
These are the two values I need to iterate through.  There are unit_pk's that range from 1 to a big number and service_cat_desc that are about 10 different text values.&lt;BR /&gt;
&lt;BR /&gt;
As you suggest, I could move these parameters to the ON clause to improve performance but this doesn't solve my problem of needing to cycle through different values.  I'm thinking use nested loops to iterate through every combination of unit_pk and service_cat_desc.</description>
      <pubDate>Mon, 14 Sep 2009 13:02:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71170#M15369</guid>
      <dc:creator>StephenOverton</dc:creator>
      <dc:date>2009-09-14T13:02:18Z</dc:date>
    </item>
    <item>
      <title>Re: Parameterizing Proc SQL with Arrays and Iterating Using Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71171#M15370</link>
      <description>Hi:&lt;BR /&gt;
  You requirement to cycle through a process multiple times and only vary a few pieces of the process points to using SAS macro coding techniques. To me. As far as I know, you can't use arrays, per se, with SQL, but you can create numbered macro variables, and within a macro program, you could iteratively cycle through your SQL code and only change your unit_pk value and your service_cat_desc value.&lt;BR /&gt;
 &lt;BR /&gt;
  I would start where you are at: you already have a working SAS program. Now you need to learn more about SAS macro processing. The SAS Macro facility is a method that you can use to actually -generate- code -- think of the SAS Macro facility as a big, behind-the-scenes, typewriter, which can type pieces of code or whole programs for you and then send those programs to the regular SAS compile/execute phases. So the Macro facility isn't doing any execution of code -- just resolving macro program invocations and macro variable references in order to generate code.&lt;BR /&gt;
&lt;BR /&gt;
  So, briefly, if you had:&lt;BR /&gt;
[pre]&lt;BR /&gt;
** above SQL step;&lt;BR /&gt;
%let wantpk = 54;&lt;BR /&gt;
%let wantsvc=Video;&lt;BR /&gt;
                                                                                             &lt;BR /&gt;
then in the SQL step had:&lt;BR /&gt;
orig.unit_pk = &amp;amp;wantpk and cb_cntr_service_category.service_cat_desc = "&amp;amp;wantsvc"&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                                                                  &lt;BR /&gt;
what would happen is that the macro facility word scanner would detect the &amp;amp;wantpk reference and would substitute or type the number 54 into the resolved code. Next, the macro word scanner would encounter the macro reference &amp;amp;wantsvc and would type or substitute the value Video into the quoted string (note the need for double quotes in the SQL statement in order for the macro variable reference to resolve).&lt;BR /&gt;
 &lt;BR /&gt;
Once you get a macro variable reference like this working for your program, you are now ready to look into the use of a macro program and a macro %DO loop to iterate through a numbered list of macro variables.&lt;BR /&gt;
 &lt;BR /&gt;
  If you have never used the SAS macro facility, then, buckle your seat belt because this is a new SAS ride for you in the SAS park. A good place to start is this paper -- that covers the basics of the Macro facility in 9 steps:&lt;BR /&gt;
&lt;A href="http://www2.sas.com/proceedings/sugi28/056-28.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi28/056-28.pdf&lt;/A&gt;&lt;BR /&gt;
and this one: &lt;A href="http://www2.sas.com/proceedings/sugi29/243-29.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi29/243-29.pdf&lt;/A&gt;&lt;BR /&gt;
and this is a nice blog about using %DO loops&lt;BR /&gt;
&lt;A href="http://scott.sherrillmix.com/blog/programmer/sas-macros-letting-sas-do-the-typing/" target="_blank"&gt;http://scott.sherrillmix.com/blog/programmer/sas-macros-letting-sas-do-the-typing/&lt;/A&gt;&lt;BR /&gt;
you'll have to search for more papers by SAS users on macro processing. Here's one that explicitly talks about using PROC SQL to create your macro variables (such as the numbered list you'll need for your task)&lt;BR /&gt;
&lt;A href="http://support.sas.com/resources/papers/proceedings09/200-2009.pdf" target="_blank"&gt;http://support.sas.com/resources/papers/proceedings09/200-2009.pdf&lt;/A&gt;&lt;BR /&gt;
             &lt;BR /&gt;
Hopefully, this will get you started.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Mon, 14 Sep 2009 13:23:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71171#M15370</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2009-09-14T13:23:56Z</dc:date>
    </item>
    <item>
      <title>Re: Parameterizing Proc SQL with Arrays and Iterating Using Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71172#M15371</link>
      <description>Thanks Cynthia, you've been very helpful as always!&lt;BR /&gt;
&lt;BR /&gt;
Using the select into clause is exactly what I was looking for.  I was not aware of macro lists either.&lt;BR /&gt;
&lt;BR /&gt;
I think I've got two lists loaded for my two parameters.  Now I've just got to figure out how to iterate through them.&lt;BR /&gt;
&lt;BR /&gt;
One question in the meantime: You mentioned a "numbered list of macro variables".  Should I be loading the two parameters in any special way??&lt;BR /&gt;
&lt;BR /&gt;
Here is what I've done to load the lists so far:&lt;BR /&gt;
&lt;BR /&gt;
/**Load service categories into macro variable*/&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
  select servicecat into :Services separated by ' '&lt;BR /&gt;
  from ServiceCategories;&lt;BR /&gt;
quit;&lt;BR /&gt;
/**Load units into macro variable*/&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
  select unit_pk into :Units separated by ' '&lt;BR /&gt;
  from PossibleUnits;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
ServiceCategories and PossibleUnits are tables with distinct values of what I want to iterate through.</description>
      <pubDate>Mon, 14 Sep 2009 14:16:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71172#M15371</guid>
      <dc:creator>StephenOverton</dc:creator>
      <dc:date>2009-09-14T14:16:45Z</dc:date>
    </item>
    <item>
      <title>Re: Parameterizing Proc SQL with Arrays and Iterating Using Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71173#M15372</link>
      <description>Also, in your SELECT, you may want to use DISTINCT(&lt;VARNAME&gt;) so that there is only one macro variable for each unique value. And, depending on the number of "separated" values generated, you may exceed the size limit of a SAS macro variable -- not sure about your unique value count though.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;/VARNAME&gt;</description>
      <pubDate>Mon, 14 Sep 2009 14:33:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71173#M15372</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-09-14T14:33:44Z</dc:date>
    </item>
    <item>
      <title>Re: Parameterizing Proc SQL with Arrays and Iterating Using Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71174#M15373</link>
      <description>Thanks, I've got the distinct covered in the SQL that loads the temp tables.&lt;BR /&gt;
&lt;BR /&gt;
I think I am exceeding the size of macro variables.  My list of unit_pks can have about 70000 integers starting from 1 to 70000.</description>
      <pubDate>Mon, 14 Sep 2009 14:39:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71174#M15373</guid>
      <dc:creator>StephenOverton</dc:creator>
      <dc:date>2009-09-14T14:39:10Z</dc:date>
    </item>
    <item>
      <title>Re: Parameterizing Proc SQL with Arrays and Iterating Using Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71175#M15374</link>
      <description>As a general-rule, consider always working with a SAS-generated macro variable list, if you must use PROC SQL, rather than using a DATA step approach.  Here's a Google advanced search argument which will reveal some useful SAS-hosted DOC and technical/conference reference material on the topic:&lt;BR /&gt;
&lt;BR /&gt;
proc sql select into macro variable iterate site:sas.com&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Mon, 14 Sep 2009 14:42:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71175#M15374</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-09-14T14:42:39Z</dc:date>
    </item>
    <item>
      <title>Re: Parameterizing Proc SQL with Arrays and Iterating Using Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71176#M15375</link>
      <description>So instead of a single list, I could use something like this, right?&lt;BR /&gt;
&lt;BR /&gt;
SELECT &lt;COLUMN name="" in="" a="" table=""&gt;&lt;BR /&gt;
INTO :&lt;MACRO variable="" name1=""&gt; -&lt;BR /&gt;
:&lt;MACRO variable="" name999=""&gt;&lt;BR /&gt;
FROM &amp;lt; table&amp;gt;&lt;BR /&gt;
&lt;BR /&gt;
I think this is what Cynthia was talking about when she mentions "numbered list".&lt;/MACRO&gt;&lt;/MACRO&gt;&lt;/COLUMN&gt;</description>
      <pubDate>Mon, 14 Sep 2009 14:46:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71176#M15375</guid>
      <dc:creator>StephenOverton</dc:creator>
      <dc:date>2009-09-14T14:46:10Z</dc:date>
    </item>
    <item>
      <title>Re: Parameterizing Proc SQL with Arrays and Iterating Using Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71177#M15376</link>
      <description>Yes, but the high-value (macro variable with count of unique values) would be derived from an initial SELECT, followed by the SELECT that populates your declared range of values.  Then, you would iterate through the list of macro variables in a %DO / %END code paragraph, generating your SAS code as needed.  You will find several examples on the SAS.COM support site with example coding techniques.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Mon, 14 Sep 2009 14:59:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71177#M15376</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-09-14T14:59:40Z</dc:date>
    </item>
    <item>
      <title>Re: Parameterizing Proc SQL with Arrays and Iterating Using Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71178#M15377</link>
      <description>Sounds good, I found a good example with your search suggestion.  I'll post the code when I'm done.&lt;BR /&gt;
&lt;BR /&gt;
In the meantime, this PDF covers a lot of what I'm doing: &lt;A href="http://support.sas.com/resources/papers/proceedings09/061-2009.pdf" target="_blank"&gt;http://support.sas.com/resources/papers/proceedings09/061-2009.pdf&lt;/A&gt;</description>
      <pubDate>Mon, 14 Sep 2009 15:16:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71178#M15377</guid>
      <dc:creator>StephenOverton</dc:creator>
      <dc:date>2009-09-14T15:16:38Z</dc:date>
    </item>
    <item>
      <title>Re: Parameterizing Proc SQL with Arrays and Iterating Using Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71179#M15378</link>
      <description>Now I'm stuck with this error:&lt;BR /&gt;
&lt;BR /&gt;
ERROR: The %DO statement is not valid in open code.&lt;BR /&gt;
&lt;BR /&gt;
This is the full code of what I have right now:&lt;BR /&gt;
&lt;BR /&gt;
LIBNAME CONX BASE "/sas_perm_data/Enlighten_Landing" ;&lt;BR /&gt;
%global s;&lt;BR /&gt;
&lt;BR /&gt;
/** Passthrough SQL to load distinct service category types  ***/&lt;BR /&gt;
proc sql;    &lt;BR /&gt;
  connect to odbc as myconn (dsn=Enlighten);&lt;BR /&gt;
    create table ServiceCategories  as&lt;BR /&gt;
      select * &lt;BR /&gt;
         from connection to myconn (&lt;BR /&gt;
           select distinct&lt;BR /&gt;
             cb_cntr_service_category.service_cat_desc as ServiceCat&lt;BR /&gt;
           from&lt;BR /&gt;
             cb_subs_prod_pckg as orig&lt;BR /&gt;
             left outer join cb_cntr_service_type on&lt;BR /&gt;
               orig.service_type_code = cb_cntr_service_type.service_type_code&lt;BR /&gt;
             left outer join cb_cntr_service_category on&lt;BR /&gt;
               cb_cntr_service_category.service_cat_code = cb_cntr_service_type.service_cat_code&lt;BR /&gt;
         );&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
/** Passthrough SQL to load possible units ***/&lt;BR /&gt;
proc sql;    &lt;BR /&gt;
  connect to odbc as myconn (dsn=Enlighten);&lt;BR /&gt;
    create table PossibleUnits  as&lt;BR /&gt;
      select * &lt;BR /&gt;
         from connection to myconn (&lt;BR /&gt;
           select distinct unit_pk from cb_so_unit&lt;BR /&gt;
         );&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
/**Load service categories into numbered macro variables*/&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
  select servicecat into :Services1 - :Services20&lt;BR /&gt;
  from ServiceCategories;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
%let cntservices=&amp;amp;sqlobs; /*count of services */&lt;BR /&gt;
&lt;BR /&gt;
/**Load units into numbered macro variables*/&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
  select unit_pk into :Units1 - :Units70000 &lt;BR /&gt;
  from PossibleUnits;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
%let cntunits=&amp;amp;sqlobs; /* Count of units */&lt;BR /&gt;
&lt;BR /&gt;
/*Create table to hold returned values*/&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
  create table test (unit_pk num, service_cat_desc char(30), so_pk num, pk num);&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
/**Loop through all possible units and service categories and insert into table*/&lt;BR /&gt;
proc sql;&lt;BR /&gt;
  %do s=1 %to &amp;amp;cntservices; /* just through services for now for testing*/&lt;BR /&gt;
    insert into test&lt;BR /&gt;
	  select&lt;BR /&gt;
        orig.unit_pk,&lt;BR /&gt;
        orig.service_cat_desc,&lt;BR /&gt;
        orig.so_pk,&lt;BR /&gt;
        min(orig.sub_prod_pckg_pk) as pk&lt;BR /&gt;
      from&lt;BR /&gt;
        conx.subscriber_services as orig&lt;BR /&gt;
      where&lt;BR /&gt;
        orig.so_pk is not null and&lt;BR /&gt;
        orig.unit_pk = 54 and&lt;BR /&gt;
        orig.service_cat_desc = &amp;amp;&amp;amp;Services&amp;amp;s&lt;BR /&gt;
      group by&lt;BR /&gt;
        orig.unit_pk,&lt;BR /&gt;
        orig.service_cat_desc,&lt;BR /&gt;
        orig.so_pk&lt;BR /&gt;
      order by&lt;BR /&gt;
        orig.unit_pk,&lt;BR /&gt;
        orig.service_cat_desc,&lt;BR /&gt;
        pk&lt;BR /&gt;
      ;&lt;BR /&gt;
  %end;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
I think I'm just a few lines away from getting this problem solved.

more&lt;BR /&gt;
    &lt;BR /&gt;
Message was edited by: SteveONCSU</description>
      <pubDate>Mon, 14 Sep 2009 18:00:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71179#M15378</guid>
      <dc:creator>StephenOverton</dc:creator>
      <dc:date>2009-09-14T18:00:29Z</dc:date>
    </item>
    <item>
      <title>Re: Parameterizing Proc SQL with Arrays and Iterating Using Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71180#M15379</link>
      <description>Hi:&lt;BR /&gt;
  Sounds like you're on the right track. However, I highly recommend that you read the "Nine Steps" paper -- it will explain why you cannot use %DO or %IF in open code and why you have to put create a macro program using %MACRO and %MEND.&lt;BR /&gt;
 &lt;BR /&gt;
  Once you read the "Nine Steps" paper, I think you'll have a much better idea of what to do next.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Mon, 14 Sep 2009 18:40:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71180#M15379</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2009-09-14T18:40:40Z</dc:date>
    </item>
    <item>
      <title>Re: Parameterizing Proc SQL with Arrays and Iterating Using Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71181#M15380</link>
      <description>So I've got the macro written now, thanks to a little old fashioned reading.  But I keep getting a segmentation error when running it:&lt;BR /&gt;
&lt;BR /&gt;
Segmentation Violation In Task [ SQL (2)  ]&lt;BR /&gt;
Fault and traceback information not available&lt;BR /&gt;
Task Traceback&lt;BR /&gt;
&lt;BR /&gt;
The macro runs and inserts values into the table correctly, it just doesn't finish which does not give me the entire dataset I need.&lt;BR /&gt;
&lt;BR /&gt;
Here is the macro, any suggestions would be appreciated:&lt;BR /&gt;
&lt;BR /&gt;
%macro iterate;&lt;BR /&gt;
  proc sql noprint;&lt;BR /&gt;
  %do u=1 %to &amp;amp;cntunits;&lt;BR /&gt;
  %do s=1 %to &amp;amp;cntservices;&lt;BR /&gt;
    insert into InitialTickets&lt;BR /&gt;
	  select&lt;BR /&gt;
        orig.unit_pk,&lt;BR /&gt;
        orig.service_cat_desc,&lt;BR /&gt;
        orig.so_pk,&lt;BR /&gt;
        min(orig.sub_prod_pckg_pk) as pk&lt;BR /&gt;
      from&lt;BR /&gt;
        conx.subscriber_services as orig&lt;BR /&gt;
      where&lt;BR /&gt;
        orig.so_pk is not null and&lt;BR /&gt;
        orig.unit_pk = &amp;amp;&amp;amp;Units&amp;amp;u and&lt;BR /&gt;
        orig.service_cat_desc = &amp;amp;&amp;amp;Services&amp;amp;s&lt;BR /&gt;
      group by&lt;BR /&gt;
        orig.unit_pk,&lt;BR /&gt;
        orig.service_cat_desc,&lt;BR /&gt;
        orig.so_pk&lt;BR /&gt;
      ;&lt;BR /&gt;
  %end;&lt;BR /&gt;
  %end;&lt;BR /&gt;
  quit;&lt;BR /&gt;
%mend iterate;</description>
      <pubDate>Tue, 15 Sep 2009 21:08:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71181#M15380</guid>
      <dc:creator>StephenOverton</dc:creator>
      <dc:date>2009-09-15T21:08:56Z</dc:date>
    </item>
    <item>
      <title>Re: Parameterizing Proc SQL with Arrays and Iterating Using Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71182#M15381</link>
      <description>Hi:&lt;BR /&gt;
  I don't know....segmentation violation sounds pretty serious. I wonder if you are somehow reaching a liimit on the INSERTs that you can generate or have exceeded some other limit. How high can your %DO loops go???&lt;BR /&gt;
  You might want to work with Tech Support on this one.&lt;BR /&gt;
cynthia</description>
      <pubDate>Wed, 16 Sep 2009 02:09:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71182#M15381</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2009-09-16T02:09:56Z</dc:date>
    </item>
    <item>
      <title>Re: Parameterizing Proc SQL with Arrays and Iterating Using Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71183#M15382</link>
      <description>Yes.&lt;BR /&gt;
&lt;BR /&gt;
If the number of iterations are pretty high you may have reached some limit.&lt;BR /&gt;
&lt;BR /&gt;
Try to reformulate your macro in the following way:&lt;BR /&gt;
&lt;BR /&gt;
%macro iterate;&lt;BR /&gt;
%do u=1 %to &amp;amp;cntunits;&lt;BR /&gt;
%do s=1 %to &amp;amp;cntservices;&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
insert into InitialTickets&lt;BR /&gt;
select&lt;BR /&gt;
orig.unit_pk,&lt;BR /&gt;
orig.service_cat_desc,&lt;BR /&gt;
orig.so_pk,&lt;BR /&gt;
min(orig.sub_prod_pckg_pk) as pk&lt;BR /&gt;
from&lt;BR /&gt;
conx.subscriber_services as orig&lt;BR /&gt;
where&lt;BR /&gt;
orig.so_pk is not null and&lt;BR /&gt;
orig.unit_pk = &amp;amp;&amp;amp;Units&amp;amp;u and&lt;BR /&gt;
orig.service_cat_desc = &amp;amp;&amp;amp;Services&amp;amp;s&lt;BR /&gt;
group by&lt;BR /&gt;
orig.unit_pk,&lt;BR /&gt;
orig.service_cat_desc,&lt;BR /&gt;
orig.so_pk&lt;BR /&gt;
;&lt;BR /&gt;
quit;&lt;BR /&gt;
%end;&lt;BR /&gt;
%end;&lt;BR /&gt;
%mend iterate; &lt;BR /&gt;
&lt;BR /&gt;
It will do the same but issue and commit one proc sql statement per iteration.&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal.&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;</description>
      <pubDate>Wed, 16 Sep 2009 11:29:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71183#M15382</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2009-09-16T11:29:38Z</dc:date>
    </item>
    <item>
      <title>Re: Parameterizing Proc SQL with Arrays and Iterating Using Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71184#M15383</link>
      <description>There is a large number of iterations, about 500K total.  I reformulated the macro as you describe to perform the proc sql once per iteration and it seems to work right now.  Its been running for about 10min which is longer than the normal 3min.  So I might have a solution.

more&lt;BR /&gt;
&lt;BR /&gt;
    &lt;BR /&gt;
Message was edited by: SteveONCSU</description>
      <pubDate>Wed, 16 Sep 2009 12:44:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71184#M15383</guid>
      <dc:creator>StephenOverton</dc:creator>
      <dc:date>2009-09-16T12:44:57Z</dc:date>
    </item>
    <item>
      <title>Re: Parameterizing Proc SQL with Arrays and Iterating Using Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71185#M15384</link>
      <description>It must be working now, still running after 45min.</description>
      <pubDate>Wed, 16 Sep 2009 13:18:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71185#M15384</guid>
      <dc:creator>StephenOverton</dc:creator>
      <dc:date>2009-09-16T13:18:46Z</dc:date>
    </item>
    <item>
      <title>Re: Parameterizing Proc SQL with Arrays and Iterating Using Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71186#M15385</link>
      <description>Success! Everything works now thanks to Daniel's suggestion.  Took about an hour to complete.</description>
      <pubDate>Wed, 16 Sep 2009 13:50:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71186#M15385</guid>
      <dc:creator>StephenOverton</dc:creator>
      <dc:date>2009-09-16T13:50:17Z</dc:date>
    </item>
    <item>
      <title>Re: Parameterizing Proc SQL with Arrays and Iterating Using Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71187#M15386</link>
      <description>One last thing, the log file is HUGE now.  Anybody know how to stop proc sql from writing to the log??</description>
      <pubDate>Thu, 17 Sep 2009 13:19:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parameterizing-Proc-SQL-with-Arrays-and-Iterating-Using-Nested/m-p/71187#M15386</guid>
      <dc:creator>StephenOverton</dc:creator>
      <dc:date>2009-09-17T13:19:07Z</dc:date>
    </item>
  </channel>
</rss>

