<?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 loop which applies the same calculations over a set of tables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/create-loop-which-applies-the-same-calculations-over-a-set-of/m-p/406447#M98970</link>
    <description>Thank you both for your help.&lt;BR /&gt;I've done the loop as explained by Astounding. It works &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
    <pubDate>Mon, 23 Oct 2017 10:07:49 GMT</pubDate>
    <dc:creator>krish_101</dc:creator>
    <dc:date>2017-10-23T10:07:49Z</dc:date>
    <item>
      <title>create loop which applies the same calculations over a set of tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-loop-which-applies-the-same-calculations-over-a-set-of/m-p/406352#M98933</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have different data tables&amp;nbsp;named&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;NB_PRODUCT_GROUP1_1,&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;NB_PRODUCT_GROUP1_2,.....,&amp;nbsp;NB_PRODUCT_GROUP1_24&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to do the following for each of the above&amp;nbsp;tables located in&amp;nbsp;&lt;SPAN&gt;"C:...\Output_Calculation_Sets\Source"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;1. select about 96 columns (TIme, Period length,...,surplus)&lt;/P&gt;&lt;P&gt;2. transpose the data&lt;/P&gt;&lt;P&gt;3. export the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;data into csv to &amp;nbsp;the folder&amp;nbsp;&lt;SPAN&gt;"C:...\Output_Calculation_Sets\results"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;repeat the above for all 24 tables to have 24 csv named&amp;nbsp;nb_grp1_1.csv,&amp;nbsp;nb_grp1_2.csv,&amp;nbsp;nb_grp1_3.csv,...,nb_grp1_24.csv&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I'm using the following method, I've added a variable &amp;amp;yer to the source table so that I get a table name with a variable attached to it NB_PRODUCT_GROUP1_&amp;amp;yer. I then run the code below by changing&amp;nbsp;%let yer= 1 to 24 (very manual and time consuming)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Can someone help me to create a code which can loop the above.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;As bonus, instead of having 24 csv it would help to have all of them in one csv file with tabs&amp;nbsp;nb_grp1_1,&amp;nbsp;nb_grp1_2,&amp;nbsp;nb_grp1_3,...,nb_grp1_24&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks in advance.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Krish&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;LIBNAME Source&amp;nbsp;BASE "C:...\Output_Calculation_Sets\Source";&lt;BR /&gt;%let yer= 1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table work.nb_grp1_&amp;amp;yer&lt;BR /&gt;as select&lt;/P&gt;&lt;P&gt;Time,&lt;BR /&gt;Period_Length,&lt;BR /&gt;DISC_RATE,&lt;/P&gt;&lt;P&gt;.......,&lt;BR /&gt;MORT_ASS,&lt;/P&gt;&lt;P&gt;Surplus&lt;/P&gt;&lt;P&gt;FROM Source.NB_PRODUCT_GROUP1_&amp;amp;yer;&lt;BR /&gt;QUIT;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc transpose&lt;BR /&gt;DATA = &lt;SPAN&gt;work.nb_grp1_&amp;amp;yer&lt;/SPAN&gt;&lt;BR /&gt;name=Time let OUT = WORK.InputSetFormatted;&lt;BR /&gt;id Time;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc export&lt;BR /&gt;DATA = &lt;SPAN&gt;work.nb_grp1_&amp;amp;yer&lt;/SPAN&gt;&lt;BR /&gt;dbms=csv&lt;BR /&gt;outfile="&lt;SPAN&gt;C:...\Output_Calculation_Sets\results&lt;/SPAN&gt;\nb_grp1_&amp;amp;yer..csv"&lt;BR /&gt;replace;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Oct 2017 10:38:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-loop-which-applies-the-same-calculations-over-a-set-of/m-p/406352#M98933</guid>
      <dc:creator>krish_101</dc:creator>
      <dc:date>2017-10-22T10:38:02Z</dc:date>
    </item>
    <item>
      <title>Re: create loop which applies the same calculations over a set of tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-loop-which-applies-the-same-calculations-over-a-set-of/m-p/406358#M98937</link>
      <description>&lt;P&gt;Most of what you ask for is relatively easy.&amp;nbsp; But you are not ready yet.&amp;nbsp; You need to have a working process for 1 file, and what you have posted is just not working yet&amp;nbsp; &amp;nbsp;... missing RUN statements, transposing data with illegal commands, then using the input for the transpose instead of the output.&amp;nbsp; Once you have a working process, you can automate it in this way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro loop;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%do yer=1 %to 24&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; Your working process goes here ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%mend loop;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%loop&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, if you attempt this before your process is actually working, you are just turning a problem into a larger problem.&lt;/P&gt;</description>
      <pubDate>Sun, 22 Oct 2017 11:39:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-loop-which-applies-the-same-calculations-over-a-set-of/m-p/406358#M98937</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-10-22T11:39:17Z</dc:date>
    </item>
    <item>
      <title>Re: create loop which applies the same calculations over a set of tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-loop-which-applies-the-same-calculations-over-a-set-of/m-p/406365#M98941</link>
      <description>&lt;P&gt;Astounding already described a macro loop solution for you, which is what you are asking for, to create a loop...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An alternative that avoids the need to loop (and avoids the macro language) would be to concatenate your 24 dataset together, and add a variable for GroupID (1-24).&amp;nbsp; With that, you shouldn't need a loop.&amp;nbsp;&amp;nbsp; You just select the variables you want, transpose them with a BY statement for group (I think), and export the data.&amp;nbsp;to CSV.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You say you would prefer one CSV with multiple tabs instead of 24 CSVs.&amp;nbsp; A CSV file doesn't support tabs, because it's just a text file.&amp;nbsp; With the above dataset you could easily make one CSV file with all the data identified by GroupID.&amp;nbsp; Or you could use PROC PRINT with a BY statement and ODS EXCEL to make an excel file with one worksheet per by group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm a big fan of macro looping.&amp;nbsp; But there are many situations where macro looping can be replaced with by group processing.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Oct 2017 12:07:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-loop-which-applies-the-same-calculations-over-a-set-of/m-p/406365#M98941</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2017-10-22T12:07:50Z</dc:date>
    </item>
    <item>
      <title>Re: create loop which applies the same calculations over a set of tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-loop-which-applies-the-same-calculations-over-a-set-of/m-p/406447#M98970</link>
      <description>Thank you both for your help.&lt;BR /&gt;I've done the loop as explained by Astounding. It works &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Mon, 23 Oct 2017 10:07:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-loop-which-applies-the-same-calculations-over-a-set-of/m-p/406447#M98970</guid>
      <dc:creator>krish_101</dc:creator>
      <dc:date>2017-10-23T10:07:49Z</dc:date>
    </item>
  </channel>
</rss>

