<?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: Repeat a section of code (multiple steps) for n iterations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Repeat-a-section-of-code-multiple-steps-for-n-iterations/m-p/360648#M84960</link>
    <description>&lt;P&gt;Thats really not a good idea. &amp;nbsp;Basically you are repeating running that code 100 times, so 100 times the resources. &amp;nbsp;You could of course simplfy your code in one quick step:&lt;/P&gt;
&lt;PRE&gt;data sim_wd_new;
  set sim_wd;
  do i=1 to 100;
    sim_value=col1 + rand("normal", 0, _rmse_);
    output;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;This will create 100 random values to the new file, i.e. multiply sim_wd * 100 records. &amp;nbsp;Then you have one transpose procedure and no need of the append. &amp;nbsp;Of course not being able to see the full picture this is only a suggestion.&lt;/P&gt;</description>
    <pubDate>Tue, 23 May 2017 08:12:21 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2017-05-23T08:12:21Z</dc:date>
    <item>
      <title>Repeat a section of code (multiple steps) for n iterations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Repeat-a-section-of-code-multiple-steps-for-n-iterations/m-p/360631#M84950</link>
      <description>&lt;P&gt;I've got 4 steps of code that I'd like to repeat 100 times, appending the results each time to a data set (last procedure in the block of code).&amp;nbsp; I think I should be either using a Do Loop or Macro to achieve but I can't work out the correct syntax to make it happen.&amp;nbsp; The code is below.&amp;nbsp; Basically I want something that will treat this as a block and repeat it n times. Some direction would be appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Sim_WD;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Sim_WD;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Sim_value=COL1 + rand(&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"Normal"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;, &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;, _RMSE_);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;sql&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;create&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;table&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; MD_append &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;select&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Site &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Site,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Year &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Year,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Season &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Season,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Type &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Type,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;RegYear &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; RegYear,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;max(Sim_value) &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; MD &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;from&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Sim_WD&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;group&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Site, Year, Season, Type, RegYear;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;run&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;transpose&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;Data&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=MD_Append &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;out&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=MD_Append (&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;Drop&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=_Name_)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;prefix&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=MD;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;var&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; MD;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;id&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; RegYear;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Site Year Season Type;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;run&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;append&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;base&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=Work.md_hist_wd &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;data&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=work.md_append;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 23 May 2017 06:11:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Repeat-a-section-of-code-multiple-steps-for-n-iterations/m-p/360631#M84950</guid>
      <dc:creator>jo1</dc:creator>
      <dc:date>2017-05-23T06:11:09Z</dc:date>
    </item>
    <item>
      <title>Re: Repeat a section of code (multiple steps) for n iterations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Repeat-a-section-of-code-multiple-steps-for-n-iterations/m-p/360636#M84953</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I'm not sure appending&amp;nbsp;the dataset n-times is the most performant way to achieve what you want, but without further explanations or test data it's difficult to say.&lt;/P&gt;&lt;P&gt;Anyway one way to loop would be with a macro, something like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro repeat(n);
   %do i=1 %to &amp;amp;n.;
      data Sim_WD;
         set Sim_WD;
         Sim_value=COL1 + rand("Normal", 0, _RMSE_);
      run;

      PROC SQL;
         CREATE TABLE MD_append AS
            SELECT Site AS Site,
                   YEAR AS YEAR,
                   Season AS Season,
                   TYPE AS TYPE,
                   RegYear AS RegYear,
                   max(Sim_value) AS MD
            FROM Sim_WD
            GROUP BY Site,
                     YEAR,
                     Season,
                     TYPE,
                     RegYear
         ;
      QUIT;

      proc transpose Data=MD_Append out=MD_Append (Drop=_Name_)
         prefix=MD;
         var MD;
         id RegYear;
         by Site Year Season Type;
      run;    
    
      %if &amp;amp;i. eq 1 %then %do;
         data Work.md_hist_wd;
            set work.md_append;
         run;
      %end;
      %else %do;
         proc append base=Work.md_hist_wd data=work.md_append;
         run;
      %end;
   %end;
%mend repeat;
%repeat(100);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Cheers&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 May 2017 06:38:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Repeat-a-section-of-code-multiple-steps-for-n-iterations/m-p/360636#M84953</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2017-05-23T06:38:25Z</dc:date>
    </item>
    <item>
      <title>Re: Repeat a section of code (multiple steps) for n iterations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Repeat-a-section-of-code-multiple-steps-for-n-iterations/m-p/360637#M84954</link>
      <description>&lt;P&gt;If you are interested in why it is not a good idea to repeat simulation code in a macro loop like this check out this article by Rick Wicklin:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.sas.com/content/iml/2012/07/18/simulation-in-sas-the-slow-way-or-the-by-way.html" target="_blank"&gt;http://blogs.sas.com/content/iml/2012/07/18/simulation-in-sas-the-slow-way-or-the-by-way.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 May 2017 06:43:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Repeat-a-section-of-code-multiple-steps-for-n-iterations/m-p/360637#M84954</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-05-23T06:43:07Z</dc:date>
    </item>
    <item>
      <title>Re: Repeat a section of code (multiple steps) for n iterations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Repeat-a-section-of-code-multiple-steps-for-n-iterations/m-p/360648#M84960</link>
      <description>&lt;P&gt;Thats really not a good idea. &amp;nbsp;Basically you are repeating running that code 100 times, so 100 times the resources. &amp;nbsp;You could of course simplfy your code in one quick step:&lt;/P&gt;
&lt;PRE&gt;data sim_wd_new;
  set sim_wd;
  do i=1 to 100;
    sim_value=col1 + rand("normal", 0, _rmse_);
    output;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;This will create 100 random values to the new file, i.e. multiply sim_wd * 100 records. &amp;nbsp;Then you have one transpose procedure and no need of the append. &amp;nbsp;Of course not being able to see the full picture this is only a suggestion.&lt;/P&gt;</description>
      <pubDate>Tue, 23 May 2017 08:12:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Repeat-a-section-of-code-multiple-steps-for-n-iterations/m-p/360648#M84960</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-05-23T08:12:21Z</dc:date>
    </item>
    <item>
      <title>Re: Repeat a section of code (multiple steps) for n iterations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Repeat-a-section-of-code-multiple-steps-for-n-iterations/m-p/360951#M85057</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I take your point but I'm not sure what I need can be achieved another way.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To give you a better explanation of what I'm trying to do:&amp;nbsp; I'm calculating POE50 and POE10 values for electrical maximum demand data.&amp;nbsp; This sections of the code is doing the weather normalisation procedure.&amp;nbsp; I need to&amp;nbsp;take the relationship (regression)&amp;nbsp;between the max demand and the max/min temperature for a season and to apply that to all the historic weather data days I have for that site and then calculate the maximum demand for each year.&amp;nbsp; If I had 15 years of historic&amp;nbsp;weather that would result in 15 maximum demands for that&amp;nbsp;relationship.&amp;nbsp; I then need to add a random error to each of the historic weather data days (by taking a draw from a distribution with mean 0 and standard deviation equal to the standard error of the regression&amp;nbsp;of the relationship) to produce another set of 15 maximum demands.&amp;nbsp; Each time this is done (100) I another set of maximum demands is added.&amp;nbsp; Then 10 and 50 POE demand values are taking from these resulting observations. I'm doing this for multiple sites etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I realise that this is doing a lot but it was currently done in excel with macros and I am converting it to SAS - this is a massive improvement &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jo&lt;/P&gt;</description>
      <pubDate>Tue, 23 May 2017 22:37:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Repeat-a-section-of-code-multiple-steps-for-n-iterations/m-p/360951#M85057</guid>
      <dc:creator>jo1</dc:creator>
      <dc:date>2017-05-23T22:37:54Z</dc:date>
    </item>
    <item>
      <title>Re: Repeat a section of code (multiple steps) for n iterations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Repeat-a-section-of-code-multiple-steps-for-n-iterations/m-p/361047#M85104</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;this improvement sounds good to me. You will need to evaluate the programming effort with the running time of your program and the gain in robustness or efficacy.&lt;/P&gt;&lt;P&gt;I would&amp;nbsp;say, give it a try and come back to the community (with sample program &amp;amp; test data)&amp;nbsp;if you need optimization feedback.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 May 2017 07:37:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Repeat-a-section-of-code-multiple-steps-for-n-iterations/m-p/361047#M85104</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2017-05-24T07:37:36Z</dc:date>
    </item>
  </channel>
</rss>

