<?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: Create a loop in a SAS datatable / dataset and execute a store procedure each time in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/843304#M20615</link>
    <description>&lt;P&gt;Loops are fundamental to programming because they enable you to repeat a computation for various values of parameters. Different languages use different keywords to define the iteration statement. The most well-known statement is the "for loop," which is used by C/C++, MATLAB, R, and other languages. Older languages, such as FORTRAN and SAS, call the iteration statement a "do loop," but it is exactly the same concept.&lt;BR /&gt;DO loops in the DATA step&lt;BR /&gt;The basic iterative DO statement in SAS has the syntax DO value = start TO stop. An END statement marks the end of the loop, as shown in the following example:&lt;BR /&gt;data A;&lt;BR /&gt;do i = 1 to 5;&lt;BR /&gt;y = i**2; /* values are 1, 4, 9, 16, 25 */&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;By default, each iteration of a DO statement increments the value of the counter by 1, but you can use the BY option to increment the counter by other amounts, including non-integer amounts. For example, each iteration of the following DATA step increments the value i by 0.5:&lt;BR /&gt;data A;&lt;BR /&gt;do i = 1 to 5 by 0.5;&lt;BR /&gt;y = i**2; /* values are 1, 2.25, 4, ..., 16, 20.25, 25 */&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;You can also iterate "backwards" by using a negative value for the BY option: do i=5 to 1 by -0.5.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Rachel Gomez&lt;/P&gt;</description>
    <pubDate>Wed, 09 Nov 2022 09:32:25 GMT</pubDate>
    <dc:creator>RacheLGomez123</dc:creator>
    <dc:date>2022-11-09T09:32:25Z</dc:date>
    <item>
      <title>Create a loop in a SAS datatable / dataset and execute a store procedure each time</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/832837#M20557</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm relatively new to SAS and I'd really appreciate your input into this. Any guidance would be deeply appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a few tables which I fill with table loaders. All of my data are from my SQL DB.&lt;/P&gt;&lt;P&gt;I also have a store procedure being executed at the back.&lt;/P&gt;&lt;P&gt;Now, the datatable I have created, leaves column 1 as empty since I don't get a value for that (yet).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm looking for a way looping into my datatable and each time have my store procedure being executed, in order to get a value and update my datatable with that value, at a specific part of my datatable (let's say 0, 1 field)(column 1). At the next loop my datatable should be updated at the field (1, 1), the one next at the (2, 1) and so on and so forth. How can I achieve that?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please forgive my ignorance but I come from a VS environment and I thought this could be done easily but I can't seem to find a way out of this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance for your assistance,&lt;/P&gt;&lt;P&gt;tassnh&lt;/P&gt;</description>
      <pubDate>Mon, 12 Sep 2022 08:26:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/832837#M20557</guid>
      <dc:creator>tassnh</dc:creator>
      <dc:date>2022-09-12T08:26:42Z</dc:date>
    </item>
    <item>
      <title>Re: Create a loop in a SAS datatable / dataset and execute a store procedure each time</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/832857#M20558</link>
      <description>&lt;P&gt;Your question needs a few clarifications.&lt;/P&gt;
&lt;P&gt;- Is that SAS9.4 or SAS Viya?&lt;/P&gt;
&lt;P&gt;-"&lt;SPAN&gt;&lt;EM&gt;I have a few tables which I fill with table loaders&lt;/EM&gt;": Are you using SAS DI Studio? Or what's this table loader?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;"I'm looking for a way looping into my datatable and each time have my store procedure being executed, in order to get a value and update my datatable with that value, at a specific part of my datatable (let's say 0, 1 field)(column 1). At the next loop my datatable should be updated at the field (1, 1), the one next at the (2, 1) and so on and so forth. How can I achieve that?"&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;You're talking about a SQL Stored Procedure - right? Are you already able to call this procedure out of SAS?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I also don't understand what this stored procedure should be doing. You are loading some data from SAS into a SQL DB (is that MySQL or SQL Server?) and one of the columns in source is missing/empty and then you want to do "something"? What exactly should happen on the DB side. What value would you like to insert in case of the source column being empty?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Is your SQL Stored Procedure as such working? Can you share your code?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Sep 2022 10:52:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/832857#M20558</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-09-12T10:52:57Z</dc:date>
    </item>
    <item>
      <title>Re: Create a loop in a SAS datatable / dataset and execute a store procedure each time</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/832863#M20559</link>
      <description>&lt;P&gt;Hi Patrick and thank you for your reply!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As per your questions, please find my replies with red:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your question needs a few clarifications.&lt;/P&gt;&lt;P&gt;- Is that SAS9.4 or SAS Viya? &lt;FONT color="#FF0000"&gt;We are using SAS v9.4&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;-"&lt;SPAN&gt;&lt;EM&gt;I have a few tables which I fill with table loaders&lt;/EM&gt;": Are you using SAS DI Studio? &lt;FONT color="#FF0000"&gt;Yes &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Or what's this table loader? &lt;FONT color="#FF0000"&gt;Would you like to elaborate? I'm using table loader from Transformations&amp;nbsp; --&amp;gt; Access --&amp;gt; Table Loader&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;"I'm looking for a way looping into my datatable and each time have my store procedure being executed, in order to get a value and update my datatable with that value, at a specific part of my datatable (let's say 0, 1 field)(column 1). At the next loop my datatable should be updated at the field (1, 1), the one next at the (2, 1) and so on and so forth. How can I achieve that?"&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;You're talking about a SQL Stored Procedure - right? &lt;FONT color="#FF0000"&gt;Yes &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Are you already able to call this procedure out of SAS? &lt;FONT color="#FF0000"&gt;Yes&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I also don't understand what this stored procedure should be doing. &lt;FONT color="#FF0000"&gt;This store procedure gives me the next system_id from another application in which I will be loading all the gathered information, once they are gathered from various other DBs. In other words, I NEED to have (in addition to the rest of the info gathered) the system_id that will be given by that app / DB and load the entire table into that DB.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;You are loading some data from SAS into a SQL DB (is that MySQL or SQL Server?) &lt;FONT color="#FF0000"&gt;(SQL Server)&lt;/FONT&gt; and one of the columns in source is missing/empty and then you want to do "something"? What exactly should happen on the DB side. &lt;FONT color="#FF0000"&gt;As said before, I'm appending data into existing SQL table that also gets a system_id. That system_id has to be apprehended via that SP.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;What value would you like to insert in case of the source column being empty? &lt;FONT color="#FF0000"&gt;Integer&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Is your SQL Stored Procedure as such working? &lt;FONT color="#FF0000"&gt;Yes and already calling it both in SAS and in my other apps&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Can you share your code? &lt;FONT color="#FF0000"&gt;No, unfortunately can't do that. I'm sorry for the inconvenience&amp;nbsp;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;So, to sum up; I need to loop through my datatable and at each loop apprehend the system_id (which I will get from the SP) and append it into column 1. I hope I answered your questions. I'm at your disposal for further information.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks again,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;tassnh&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Sep 2022 11:19:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/832863#M20559</guid>
      <dc:creator>tassnh</dc:creator>
      <dc:date>2022-09-12T11:19:06Z</dc:date>
    </item>
    <item>
      <title>Re: Create a loop in a SAS datatable / dataset and execute a store procedure each time</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/832869#M20560</link>
      <description>&lt;P&gt;Hmmm... I don't get 100% of your problem so eventually my answer not what you're asking for.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this is about some auto-increment/identity column then what worked for me in the past:&lt;/P&gt;
&lt;P&gt;- Exclude the column from the Proc Append&lt;/P&gt;
&lt;P&gt;- Define the column as auto-increment/indentity column in the database&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not sure that I fully understand what your stored procedure is doing but just on a high-level I feel it's eventually easier to have the DB deal with SAS trying to insert blanks than SAS.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've lately done similar things with Postgres where one can define as part of table DDL if an identity column can be populated from source if there is a value - or if not.&lt;/P&gt;
&lt;P&gt;Another option is to use a trigger so whenever there is a missing from source it triggers a function that creates a value for insertion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "general" message is: Implement such specialties on the DB side and not on the SAS side. It's just easier.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another reason to implement this on the DB side (if I understand right): You ensure that the column gets populated as it should without a dependency on the client side.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Sep 2022 11:58:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/832869#M20560</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-09-12T11:58:16Z</dc:date>
    </item>
    <item>
      <title>Re: Create a loop in a SAS datatable / dataset and execute a store procedure each time</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/832883#M20561</link>
      <description>&lt;P&gt;It is not hard to generate code from data using a data step.&lt;/P&gt;
&lt;P&gt;But you need to have an idea of&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) What code you need to generate.&amp;nbsp; Show what SAS you need to run to execute the stored procedure and retrieve the value it generates.&lt;/P&gt;
&lt;P&gt;2) How the code varies as the data changes.&amp;nbsp; What part of the code changes?&amp;nbsp; What is the name of the variable in the source data that can be used to generate the value?&lt;/P&gt;</description>
      <pubDate>Mon, 12 Sep 2022 13:11:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/832883#M20561</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-09-12T13:11:10Z</dc:date>
    </item>
    <item>
      <title>Re: Create a loop in a SAS datatable / dataset and execute a store procedure each time</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833045#M20562</link>
      <description>&lt;P&gt;Good morning Patrick,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please find my replies in red:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hmmm... I don't get 100% of your problem so eventually my answer not what you're asking for.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;I'll do my best to explain my problem to the fullest&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If this is about some auto-increment/identity column then what worked for me in the past:&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;Yes it is. The problem is that, this auto-generated, auto-increment number, is being used in other tables as well. This system_id is used in all tables that this app has and is also the key of the table. In addition to that, in most cases it is the foreign key of another table, in order to link / combine data&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;- Exclude the column from the Proc Append&lt;/P&gt;&lt;P&gt;- Define the column as auto-increment/indentity column in the database&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;Based on my previous statement (link between data) this is not feasible. I need to have that system_id prior to appending the retrieved data, in order to use it in another table and link the two table between them.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm not sure that I fully understand what your stored procedure is doing but just on a high-level I feel it's eventually easier to have the DB deal with SAS trying to insert blanks than SAS. &lt;FONT color="#FF0000"&gt;My SP is basically auto-increamenting a number which gives me the next system_id. It's the easiest and most basic straight forward thing. It then updates another table which holds that system_id. So, in order to apprehend this system_id I need to loop through all my lines in my dt and assign that system_id to each line (on the first column). Then, I will be using that system_id of that line to link the specific value with another value on another table.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;I've lately done similar things with Postgres where one can define as part of table DDL if an identity column can be populated from source if there is a value - or if not.&lt;/P&gt;&lt;P&gt;Another option is to use a trigger so whenever there is a missing from source it triggers a function that creates a value for insertion.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;Using a demon&amp;nbsp;is again not an option since, as described above, I need to have that system_id at hand before appending the data to my table, in order to link my data with my 2 tables&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The "general" message is: Implement such specialties on the DB side and not on the SAS side. It's just easier.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;In general, I have to be sure that, that system_id won't be given in another table at another value by the time I will be using it. I need to have that SP ran and assure that it won't be used by another call of that SP. In addition to that, I need to have that link between my 2 tables done, again before appending any data into my tables.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another reason to implement this on the DB side (if I understand right): You ensure that the column gets populated as it should without a dependency on the client side.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;I hope I have addressed all your questions. Please feel free to ask whatever else you might think you need to clarify.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;Regards,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;tassnh&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Sep 2022 05:32:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833045#M20562</guid>
      <dc:creator>tassnh</dc:creator>
      <dc:date>2022-09-13T05:32:12Z</dc:date>
    </item>
    <item>
      <title>Re: Create a loop in a SAS datatable / dataset and execute a store procedure each time</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833047#M20563</link>
      <description>&lt;P&gt;Dear Tom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your reply.&lt;/P&gt;&lt;P&gt;Could you please elaborate on your answer? What is it exactly that you need me to post?&lt;/P&gt;&lt;P&gt;What I can do right now is better explain my problem, so please find my answers in red below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It is not hard to generate code from data using a data step.&lt;/P&gt;&lt;P&gt;But you need to have an idea of&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) What code you need to generate.&amp;nbsp; Show what SAS you need to run to execute the stored procedure and retrieve the value it generates.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;I already have a dt that have apprehended from the various dbs that SAS connects to.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;In that dt, I need to loop through all my lines and at each loop, execute a SP from my SQL, apprehend the result I get from that SP and assign it on the first column of each line on each loop. I can already execute the SP from outside SQL from within SAS. That is not my problem. My problem is that I don't know how to handle "loops" in SAS and within dts as is way different that the VS that I've been using so far.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;2) How the code varies as the data changes.&amp;nbsp; What part of the code changes?&amp;nbsp; What is the name of the variable in the source data that can be used to generate the value?&amp;nbsp;&lt;FONT color="#FF0000"&gt;The code does not vary in any way as the data changes. I already have my dt with all data collected and, as stated above, I need to loop through all lines in that dt. So from line 0 of that dt until line "count(dt) - 1", I need to execute the SP from within SAS, retrieve my data and assign it to a specific field in my dt.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;I don't know if what I described above makes sense but feel free to ask anything you think might help.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;Thank you again for your reply,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;Regards,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;tassnh&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Sep 2022 05:42:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833047#M20563</guid>
      <dc:creator>tassnh</dc:creator>
      <dc:date>2022-09-13T05:42:14Z</dc:date>
    </item>
    <item>
      <title>Re: Create a loop in a SAS datatable / dataset and execute a store procedure each time</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833117#M20564</link>
      <description>&lt;P&gt;What does the SP actually DO?&amp;nbsp; How do you CALL it?&amp;nbsp; How do you get back the value you actually need to insert into your table? How would you return that value to SAS so you can then return it to the database.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You cannot start building the "loop" as you call it until you know what you are looping over.&amp;nbsp; Right now you have not shown any working SAS code (or even any working SQL code in the dialect of whatever foreign database you are connecting to).&lt;/P&gt;</description>
      <pubDate>Tue, 13 Sep 2022 13:00:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833117#M20564</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-09-13T13:00:02Z</dc:date>
    </item>
    <item>
      <title>Re: Create a loop in a SAS datatable / dataset and execute a store procedure each time</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833275#M20566</link>
      <description>&lt;P&gt;Good morning Tom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please find answers in red:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What does the SP actually DO?&amp;nbsp; How do you CALL it?&amp;nbsp; How do you get back the value you actually need to insert into your table? How would you return that value to SAS so you can then return it to the database.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;As described before, my SP gives an incremental number. I call it through Precode in Table Loader. I then select the new value from a table which holds that value in the SQL DB. I then need to insert it to my dt.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You cannot start building the "loop" as you call it until you know what you are looping over.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;I think this is obvious. Why would you assume otherwise?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Right now you have not shown any working SAS code (or even any working SQL code in the dialect of whatever foreign database you are connecting to).&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;I am working for the IT dpt. of a bank corporation. We have a strict policy of sharing coding outside our organization and/or in forums. In a similar case a few years ago, a colleague was reprehended for sharing his code online. Sorry for the inconvenience but there is no alternative for that.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again,&lt;/P&gt;&lt;P&gt;tassnh&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Sep 2022 05:33:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833275#M20566</guid>
      <dc:creator>tassnh</dc:creator>
      <dc:date>2022-09-14T05:33:59Z</dc:date>
    </item>
    <item>
      <title>Re: Create a loop in a SAS datatable / dataset and execute a store procedure each time</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833281#M20567</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/433569"&gt;@tassnh&lt;/a&gt;&amp;nbsp;The moment you share some code statements like below become much clearer&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1663138654078.png" style="width: 1000px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/75221i0677876A6FE9913A/image-dimensions/1000x20?v=v2" width="1000" height="20" role="button" title="Patrick_0-1663138654078.png" alt="Patrick_0-1663138654078.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;You don't need to share your actual code and also not all of it. Just take the relevant portion and "anonymize" it by changing things like schema name, table name and procedure name.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've worked for many customers and as long as you don't share internal information none of them had ever a problem to share code snippets in a support forum.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this ETL is for loading into a SAS Solution then please share the name of the solution. Some of us might have experience with it and already know how to load into such tables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you by any chance loading into a dimensional model with a fact and dimension tables?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As I understand it you call the stored procedure once per row in the source table in SAS? For how many rows is that? I've seen something like that done in the past for daily high data volumes and then people were astonished that it performed badly...&lt;/P&gt;</description>
      <pubDate>Wed, 14 Sep 2022 07:02:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833281#M20567</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-09-14T07:02:32Z</dc:date>
    </item>
    <item>
      <title>Re: Create a loop in a SAS datatable / dataset and execute a store procedure each time</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833283#M20568</link>
      <description>&lt;P&gt;Dear Patrick,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'll try and do my best to give you an example that can be comprehended easily as well my task;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dt with system_id, name, surname, tel, e-mail, dob, address etc.&lt;/P&gt;&lt;P&gt;All data are drawn from various tables from our dwh and another SQL DB of a Document Management System.&lt;/P&gt;&lt;P&gt;All data will be appended to that Document Management System's DB, in a table that handles customers. Let's call this &lt;FONT color="#FF0000"&gt;customer_table&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Customer_table gives a system_id for each entry. A system_id is also given for all the other tables that this app has, so, I need to get the system_id from the SP that handles the system_id. That system_id is kept in a different table which (after I run my SP) I will select it's value from that table (single table, single column, single row value). Let's call this &lt;FONT color="#FF0000"&gt;system_id_table&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Coming back to my dt; all data are available to me except the system_id (1st column).&amp;nbsp;&lt;/P&gt;&lt;P&gt;All lines are new entries of customers inserted into the customer_table&lt;/P&gt;&lt;P&gt;In order to append this dt into my &lt;FONT color="#FF0000"&gt;customer_table&lt;/FONT&gt;, I first need to assure that the table is complete and includes the system_id. So, in order to get the system_id, I need to execute the SP once for each line, select the value from the &lt;FONT color="#FF0000"&gt;system_id_table&lt;/FONT&gt; and update my dt with that value. This has to loop for all the entries in my dt. This procedure will not loop for more than ~30 times. To my knowledge their won't be more than 30 customers on daily basis so, it will have to loop from line 0 till line max -1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does that make sense? I hope it helps you make a better understanding of my situation.&lt;/P&gt;&lt;P&gt;Please fell free to ask me anything you might think will help you clear things better out.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again,&lt;/P&gt;&lt;P&gt;tassnh&lt;/P&gt;</description>
      <pubDate>Wed, 14 Sep 2022 07:34:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833283#M20568</guid>
      <dc:creator>tassnh</dc:creator>
      <dc:date>2022-09-14T07:34:18Z</dc:date>
    </item>
    <item>
      <title>Re: Create a loop in a SAS datatable / dataset and execute a store procedure each time</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833287#M20569</link>
      <description>&lt;P&gt;From what you describe I assume your "system_id_table" is actually a sequence object as documented here&lt;BR /&gt;&lt;A href="https://docs.microsoft.com/en-us/sql/t-sql/statements/create-sequence-transact-sql?view=sql-server-ver16" target="_blank" rel="noopener"&gt;https://docs.microsoft.com/en-us/sql/t-sql/statements/create-sequence-transact-sql?view=sql-server-ver16&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You then got a stored procedure which calls this sequence object and returns the next increment.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What's missing and really important for us to understand is how you call this stored procedure out of SAS and especially how you get the value back into SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Normally it's a FUNCTION that returns a single value where else a Procedure is a whole "program" and can return a whole result set. So not sure if you're using the right terminology.&lt;/P&gt;
&lt;P&gt;SQL Stored Procedure: &lt;A href="https://www.w3schools.com/sql/sql_stored_procedures.asp" target="_blank" rel="noopener"&gt;https://www.w3schools.com/sql/sql_stored_procedures.asp&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;As soon as we would see the code that's already working (just change the names) and which loads a single new id into SAS, a lot of ambiguity in your description could be resolved.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In regards of "looping" in SAS: &lt;BR /&gt;The SAS data step is in a way nothing else than a loop which processes one row at a time. The data step will iterate as many times as there are rows in your table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You really need to share some snippet of your code as else there is just way too much guesswork.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The one problem that needs resolution is creation of a set of new ID's on the SQL server side and retrieve this set in SAS. Ideally your stored procedure (if it's one) allows for parameter passing where you can define how many new IDs you need - and then it's a single call.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once you've got a set of IDs on the SAS side things will be simple.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And last but not least:&lt;/P&gt;
&lt;P&gt;If you don't find a way to share the relevant code snippets then I'm not sure that "we" can really help you.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Sep 2022 09:57:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833287#M20569</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-09-14T09:57:07Z</dc:date>
    </item>
    <item>
      <title>Re: Create a loop in a SAS datatable / dataset and execute a store procedure each time</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833301#M20571</link>
      <description>&lt;P&gt;Patrick, please find my answers in red:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;From what you describe I assume your "system_id_table" is actually a sequence object as documented here&lt;BR /&gt;&lt;A href="https://docs.microsoft.com/en-us/sql/t-sql/statements/create-sequence-transact-sql?view=sql-server-ver16" target="_blank" rel="noopener nofollow noreferrer"&gt;https://docs.microsoft.com/en-us/sql/t-sql/statements/create-sequence-transact-sql?view=sql-server-v...&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;Precisely&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You then got a stored procedure which calls this sequence object and returns the next increment.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;Yes, exactly&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What's missing and really important for us to understand is how you call this stored procedure out of SAS and especially how you get the value back into SAS.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;When you call this procedure, the value gets updated and then stored again in the table I mentioned earlier (see my previous reply) (system_id_table). The only thing left to do is to read (select) the value in that table)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Normally it's a FUNCTION that returns a single value where else a Procedure is a whole "program" and can return a whole result set. So not sure if you're using the right terminology.&lt;FONT color="#FF0000"&gt;It's the correct one &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; I've been working on SQL for more than 15 years&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;SQL Stored Procedure:&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://www.w3schools.com/sql/sql_stored_procedures.asp" target="_blank" rel="noopener nofollow noreferrer"&gt;https://www.w3schools.com/sql/sql_stored_procedures.asp&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;As soon as we would see the code that's already working (just change the names) and which loads a single new id into SAS, a lot of ambiguity in your description could be resolved. &lt;FONT color="#FF0000"&gt;So, you want to see how I execute the SP or how I get the new value? As stated earlier, I just select my new value after I executed my SP&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In regards of "looping" in SAS:&lt;BR /&gt;The SAS data step is in a way nothing else than a loop which processes one row at a time. The data step will iterate as many times as there are rows in your table.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;Is there an example I can have to follow? Can you share something with me?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You really need to share some snippet of your code as else there is just way too much guesswork.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;I totally understand and I 'll see if I can do that, altering my code. Had it not been sensitive information, I wouldn't hesitate sharing the minute you asked! Just tell me what exactly you need me to post. Calling the SP, my solution on SAS (which is huge), selecting the value of the executed SP?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The one problem that needs resolution is creation of a set of new ID's on the SQL server side and retrieve this set in SAS. Ideally your stored procedure (if it's one) allows for parameter passing where you can define how many new IDs you need - and then it's a single call. &lt;FONT color="#FF0000"&gt;It returns a single value at a time, thus the loop. Had it been multiple values, I 'd have called for that range, get them in an array and apply them into my dt, but I'm afraid it's not.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Once you've got a set of IDs on the SAS side things will be simple.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;We thought the same thing &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And last but not least:&lt;/P&gt;&lt;P&gt;If you don't find a way to share the relevant code snippets then I'm not sure that "we" can really help you.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;I stated above, please clarify me the things you need me to share and I'll try sharing them&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;tassnh&lt;/P&gt;</description>
      <pubDate>Wed, 14 Sep 2022 10:45:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833301#M20571</guid>
      <dc:creator>tassnh</dc:creator>
      <dc:date>2022-09-14T10:45:13Z</dc:date>
    </item>
    <item>
      <title>Re: Create a loop in a SAS datatable / dataset and execute a store procedure each time</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833328#M20572</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;That system_id is kept in a different table which (after I run my SP) I will select it's value from that table (single table, single column, single row value). Let's call this&amp;nbsp;&lt;/SPAN&gt;&lt;FONT color="#FF0000"&gt;system_id_table&lt;/FONT&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So that might be enough to make some pseudo code to show to process.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sounds like you are doing something like this:&lt;/P&gt;
&lt;P&gt;1) Connect to database.&lt;/P&gt;
&lt;P&gt;2) run procedure in database.&lt;/P&gt;
&lt;P&gt;3) query table in database.&lt;/P&gt;
&lt;P&gt;4) upload data to database.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's assume you already have a connection to the database defined by the libref MYDB.&lt;/P&gt;
&lt;P&gt;So your single instance program might look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
connect using mydb ;
execute by mydb
  ( run_my_stored_procedure )
;
create table new_id as select * from connection to mydb
(select system_id from system_id_table)
;
quit;
data for_upload ;
  merge new_id new_data;
run;
proc append base=mydb.target_table data=for_upload;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What we cannot tell from your description is exactly what it is you want to pull form SYSTEM_ID_TABLE, how to find the "new" id that the process generated, etc etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also do not see how the source dataset you wanted to loop over applies to the process.&lt;/P&gt;
&lt;P&gt;Is it just that you need to call the stored process once for every observation in the dataset?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or are there values in the source dataset that need to serve as inputs to the stored process?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general a simply way to repeat multiple steps for every observation in a dataset is to make a macro that does the steps that takes as input the information that is the dataset.&amp;nbsp; Then just generate one call to the macro for every observation in the dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if the process needs to take the value of NAME from the input dataset, let's call the dataset HAVE, then macro a macro that has a NAME parameter and you can then call it once per observation.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set have ;
  call execute(cats('%nrstr(%mymacro)(name=',quote(trim(name),"'"),')'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Sep 2022 13:11:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833328#M20572</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-09-14T13:11:21Z</dc:date>
    </item>
    <item>
      <title>Re: Create a loop in a SAS datatable / dataset and execute a store procedure each time</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833478#M20573</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/433569"&gt;@tassnh&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1663211924570.png" style="width: 943px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/75251i314A29E0FD3D2F4A/image-dimensions/943x95?v=v2" width="943" height="95" role="button" title="Patrick_0-1663211924570.png" alt="Patrick_0-1663211924570.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Given your many years of SQL experience would you be able to write this in SQL directly via some client (select * from .....). If you can write such code then it's very simple to execute it out-of-SAS in an explicit pass-through SQL block and then just retrieve the result set back to SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1663217621754.png" style="width: 973px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/75255i566EFFD67AA1B6DB/image-dimensions/973x73?v=v2" width="973" height="73" role="button" title="Patrick_0-1663217621754.png" alt="Patrick_0-1663217621754.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Both please. How you call the SP out of SAS to increment the value and then get this value back into SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2022 04:54:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833478#M20573</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-09-15T04:54:30Z</dc:date>
    </item>
    <item>
      <title>Re: Create a loop in a SAS datatable / dataset and execute a store procedure each time</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833487#M20574</link>
      <description>&lt;P&gt;Good morning Patrick,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That statement of mine was an indication for my intentions and not my difficulties.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#339966"&gt;Given your many years of SQL experience would you be able to write this in SQL directly via some client (select * from .....).&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Are you asking me (in this part) if I will get back the result I need if I select it from my table. If so, the answer is yes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In general practice and in my (VS) apps, if and when I need to get the value of the system_id, this is exactly what I do.&amp;nbsp;But VS is way more different than DI and way more "compatible" to MSSQL. In addition, the highlight you posted was my intention to explain the procedure and what is left to do after going through the previous steps.&amp;nbsp;Actually, this is exactly what needs to be done. Select the value in the table and this is the system_id needed. Then, the SP is executed and the value in the table gets updated; and again, you select the new value as your new system_id.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My main problem (and the problem stated from the very beginning) is the loop in my dt.&amp;nbsp;I need to know the exact way to loop through my dt, apprehend my system_id from my table and assign it to the first field (each time) of my _dt.&amp;nbsp;This must loop for as many times as my rows in my dt.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I deeply appreciate your response and I do hope this clears thing better.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Last but not least;&lt;FONT color="#339966"&gt; If you can write such code then it's very simple to execute it out-of-SAS in an explicit pass-through SQL block and then just retrieve the result set back to SAS.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Can you please elaborate on that a bit? What do you mean exactly when you say "execute it out-of-SAS in an explicit pass-through SQL block"? Execute it in SQL Management Studio? Somewhere intermediate? Please keep in mind that, my experience with DI is not huge.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kind regards,&lt;/P&gt;&lt;P&gt;tassnh&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2022 04:47:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833487#M20574</guid>
      <dc:creator>tassnh</dc:creator>
      <dc:date>2022-09-15T04:47:38Z</dc:date>
    </item>
    <item>
      <title>Re: Create a loop in a SAS datatable / dataset and execute a store procedure each time</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833491#M20575</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1663217798907.png" style="width: 580px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/75256i004919789A772220/image-dimensions/580x32?v=v2" width="580" height="32" role="button" title="Patrick_0-1663217798907.png" alt="Patrick_0-1663217798907.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;If you can't share any code for us to understand where you are at right now then it's really hard to provide concrete advice.&lt;/P&gt;
&lt;P&gt;Having said that: It's going to be something pretty close to what&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;just shared as code. ...but again: You need to talk "code" with us for us to be able to amend/extend your code to what you need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2022 04:59:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833491#M20575</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-09-15T04:59:17Z</dc:date>
    </item>
    <item>
      <title>Re: Create a loop in a SAS datatable / dataset and execute a store procedure each time</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833493#M20576</link>
      <description>&lt;P&gt;Good morning Tom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will try to answer in detail and&amp;nbsp;be as thorough as I can. Please find my replies in red:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sounds like you are doing something like this:&lt;/P&gt;&lt;P&gt;1) Connect to database.&lt;/P&gt;&lt;P&gt;2) run procedure in database.&lt;/P&gt;&lt;P&gt;3) query table in database.&lt;/P&gt;&lt;P&gt;4) upload data to database.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;You might want to revert 2 and 3 between one each other. First I select the value of the table, then run my SP (in which the value gets updated) and get the value of the table again. This has to loop for as many times as my rows. Each time I select the value I get a new system_id. At the end, I need to run it once more so that the app will use it for its own purposes.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;Between 3 and 4 there is another (if not more) step. I need to update the value I got from my table in my dt (in DI), at the first field. As stated before, this has to loop for as many times as my line in my dt. Then comes step "4", in which I append my dt in my other table (Document Management Table).&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Let's assume you already have a connection to the database defined by the libref MYDB. &lt;FONT color="#FF0000"&gt;I do, with a specific username / password at a specific instance / db&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;SPAN&gt;Are you asking for this?:&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;Proc sql;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;CONNECT TO ODBC(DATAsrc=H USER=sas_h PASSWORD="{SAS102}xxxxxxxxxxxxxxxxxxxxxx" );&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;EXECUTE ( EXEC [H].[dbo].[sp_next_system_id] ) by ODBC;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;DISCONNECT FROM ODBC;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;quit;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;What we cannot tell from your description is exactly what it is you want to pull form SYSTEM_ID_TABLE, how to find the "new" id that the process generated, etc etc. &lt;FONT color="#FF0000"&gt;As stated before, this is a single column, single row table, which holds the next system_id that will be given by the system (integer). So, in other words, I want to select the value (let's say that the value is 1000) and assign this value to my first field in my dt. I will then need to execute my SP, the SP will increment the value of the system_id table by 1 (so the new value in the table will be 1001) and select it again (for as many times as my rows in my dt) and reassign it to my 2nd line, 1st field of my dt.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#FF0000"&gt;For instance:&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;Proc sql;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp;&amp;nbsp; CONNECT TO ODBC(DATAsrc=H USER=sas_h PASSWORD="{SAS102}xxxxxxxxxxxxxxxxxxxxxx" );&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; EXECUTE ( EXEC [H].[dbo].[sp_nkey] ) by ODBC;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DISCONNECT FROM ODBC;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;quit;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also do not see how the source dataset you wanted to loop over applies to the process.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;This is not a source ds. This is a dt / ds created (generated) from various selections both from DWH and my DB in SQL. From all fields retrieved, the only thing left behind is the system_id which (since this dt will be appended to a production app) need to be given to the row at the very last second and is a new value (not an existing one).&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;To describe it in more detail;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;I am constructing let's say a customer, based on his ID.&lt;/FONT&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;FONT color="#FF0000"&gt;I need to get his name from the table that holds the names, based on his ID.&lt;/FONT&gt;&lt;/LI&gt;&lt;LI&gt;&lt;FONT color="#FF0000"&gt;I need to get his date of birts from the table that holds the dates of birth, based on his ID.&lt;/FONT&gt;&lt;/LI&gt;&lt;LI&gt;&lt;FONT color="#FF0000"&gt;I need to get his address from the table that holds the addresses, based on his ID.&lt;/FONT&gt;&lt;/LI&gt;&lt;LI&gt;&lt;FONT color="#FF0000"&gt;I need to get his teleplhone from the table that holds the telephone numbers, based on his ID&lt;/FONT&gt;&lt;/LI&gt;&lt;LI&gt;&lt;FONT color="#FF0000"&gt;&lt;SPAN&gt;I construct a table (and map it on another table which is a whole different story).&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/LI&gt;&lt;LI&gt;&lt;FONT color="#FF0000"&gt;&lt;SPAN&gt;I also append an empty column on this table.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/LI&gt;&lt;LI&gt;&lt;FONT color="#FF0000"&gt;&lt;SPAN&gt;Now, for each line (which is for each client), I need to assign a system_id, based on my app (document management app).&amp;nbsp;&lt;/SPAN&gt;So, I need to loop through my rows, select the value of system_id from system_id table, execute the SP to increment the value of the system_id by 1 and go to the next line&lt;/FONT&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;The only thing missing is step "7".&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Is it just that you need to call the stored process once for every observation in the dataset?&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;SPAN&gt;Yes and get the value from the system_id table (as explained in detail above)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Or are there values in the source dataset that need to serve as inputs to the stored process?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;SPAN&gt;No. Just the 1st one&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In general a simply way to repeat multiple steps for every observation in a dataset is to make a macro that does the steps that takes as input the information that is the dataset.&amp;nbsp; Then just generate one call to the macro for every observation in the dataset.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;Could you please elaborate a bit in more detail? Please bear in mind that I’m not that experienced in DI.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for all the effort you are putting into this.&lt;/P&gt;&lt;P&gt;Please let me know if there is something else you need me to clarify or coding you might think it will help more.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kind regards,&lt;/P&gt;&lt;P&gt;tassnh&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2022 05:27:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833493#M20576</guid>
      <dc:creator>tassnh</dc:creator>
      <dc:date>2022-09-15T05:27:14Z</dc:date>
    </item>
    <item>
      <title>Re: Create a loop in a SAS datatable / dataset and execute a store procedure each time</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833494#M20577</link>
      <description>&lt;P&gt;Dear Patrick, my answers in red:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you can't share any code for us to understand where you are at right now then it's really hard to provide concrete advice.&lt;/P&gt;&lt;P&gt;Having said that: It's going to be something pretty close to what&amp;nbsp;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159" target="_blank"&gt;@Tom&lt;/A&gt;&amp;nbsp;just shared as code. ...but again: You need to talk "code" with us for us to be able to amend/extend your code to what you need.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;ok, could you indicate me what part of the coding you need me to post?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;My SP? My select from my db/table? The SP call?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;Please check my reply to Tom. Maybe that would sort things out a bit.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;Kind regards,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;tassnh&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2022 05:30:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833494#M20577</guid>
      <dc:creator>tassnh</dc:creator>
      <dc:date>2022-09-15T05:30:28Z</dc:date>
    </item>
    <item>
      <title>Re: Create a loop in a SAS datatable / dataset and execute a store procedure each time</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833611#M20578</link>
      <description>&lt;P&gt;Trying to identify the forest from the bumps on the leaves of the trees your answer is showing:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It sounds like you have a dataset that is a list of customers.&amp;nbsp; The dataset has variables like&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;ID, NAME, DOB, ADDRESS, PHONE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You now want to add a&amp;nbsp;new variable, SYSTEM_ID, by using the stored process in your foreign database.&amp;nbsp; Each procedure call only produces one new value.&amp;nbsp;&amp;nbsp;So if you have 100 customers in your dataset you will want to call the procedure 100 times.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One way to do that is to call the procedure 100 times and extract the new system_id values and save them into a SAS dataset. Then you just need to combine the dataset with the list of 100 customers with the dataset with 100 new system_id values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example (wallpaper code):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Make an empty dataset for storing new system_id values ;
data new_system_ids;
  length system_id 8;
  stop;
run;
proc sql;
  CONNECT TO ODBC (...);
  EXECUTE BY ODBC ( EXEC [H].[dbo].[sp_nkey] ) ;
  insert into new_system_ids(system_id)&lt;BR /&gt;    select system_id from connection to odbc
     (select system_id from system_id)
  ;
  EXECUTE BY ODBC ( EXEC [H].[dbo].[sp_nkey] ) ;
  insert into new_system_ids(system_id)&lt;BR /&gt;    select system_id from connection to odbc
     (select system_id from system_id)
  ;
  EXECUTE BY ODBC ( EXEC [H].[dbo].[sp_nkey] ) ;
  insert into new_system_ids(system_id)&lt;BR /&gt;    select system_id from connection to odbc
     (select system_id from system_id)
  ;
...
disconnect from odbc;
quit;
data want ;
   set have;
   set new_system_ids;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now to generate that "wallpaper" code you could just count how many ids you need by counting how many customers are in your HAVE dataset.&amp;nbsp; You could then use code generation to generate code.&amp;nbsp; SAS macro language is one way to generate code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro system_ids(n);
%local i;
%do i=1 to &amp;amp;n ;
  EXECUTE BY ODBC ( EXEC [H].[dbo].[sp_nkey] ) ;
  insert into new_system_ids(system_id)
    select system_id from connection to odbc
     (select system_id from system_id)
  ;
%end;
%mend;

data _null_;
  call symputx('nobs',nobs);
  stop;
  set have nobs=nobs;
run;

* Make an empty dataset for storing new system_id values ;
data new_system_ids;
  length system_id 8;
  stop;
run;
proc sql;
  CONNECT TO ODBC (...);
  %system_ids(&amp;amp;nobs);
  disconnect from odbc;
quit;
data want ;
   set have;
   set new_system_ids;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;NOTE: If the value is a BIGINT in your foreign database you might need to store it as a string in your SAS dataset since SAS can only represent integers with 15 decimal digits exactly.&amp;nbsp; In that case in the passthru query to pull the new value of system_id cast it as a string first so that you can preserve the uniqueness of the value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS&amp;nbsp; How can the system you described actually work?&amp;nbsp; What happens if two users call the stored procedure at almost the exact same time so that it executes twice before the query to receive the number can run. So when they go to get the value both will take the same value.&amp;nbsp; This is not how this type of autoincrement features work in other datasets I have seen.&amp;nbsp; For example in Oracle it is a FUNCTION that you call to get the next value.&amp;nbsp; So it can be returned as part of a query. Then there is no window of time between generating the value and retrieving it that could cause the race condition I just described.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2022 14:36:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Create-a-loop-in-a-SAS-datatable-dataset-and-execute-a-store/m-p/833611#M20578</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-09-15T14:36:42Z</dc:date>
    </item>
  </channel>
</rss>

