<?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 macro to run a simulation N times in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/macro-to-run-a-simulation-N-times/m-p/873907#M38764</link>
    <description>&lt;P&gt;hello SAS community,&lt;/P&gt;
&lt;P&gt;I attach here a code I am using to run a simulation. The code has several steps, and the final data table mean2 has the average of a quantity per decile of another variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to run each simulation N times. That is, for each set of values for the "starting" variables nobs, nvars, etc (which I change manually), I would like to run the code N times, stack all the resulting files mean2_1, mean2_2,...,mean2_N&amp;nbsp; and take averages of the variable of interest for each decile.&lt;/P&gt;
&lt;P&gt;For example, if the file that includes all the simulations is called mean2_N with N rows of data, the data file I want should be&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc means&amp;nbsp; data=mean_N noprint;&lt;BR /&gt;var _0 _1 _2 _3 _4 _5 _6 _7 _8 _9;&lt;BR /&gt;output out= WANT mean(_0)=A_0 etc&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;Thank you all very much!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 04 May 2023 15:40:11 GMT</pubDate>
    <dc:creator>costasRO</dc:creator>
    <dc:date>2023-05-04T15:40:11Z</dc:date>
    <item>
      <title>macro to run a simulation N times</title>
      <link>https://communities.sas.com/t5/New-SAS-User/macro-to-run-a-simulation-N-times/m-p/873907#M38764</link>
      <description>&lt;P&gt;hello SAS community,&lt;/P&gt;
&lt;P&gt;I attach here a code I am using to run a simulation. The code has several steps, and the final data table mean2 has the average of a quantity per decile of another variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to run each simulation N times. That is, for each set of values for the "starting" variables nobs, nvars, etc (which I change manually), I would like to run the code N times, stack all the resulting files mean2_1, mean2_2,...,mean2_N&amp;nbsp; and take averages of the variable of interest for each decile.&lt;/P&gt;
&lt;P&gt;For example, if the file that includes all the simulations is called mean2_N with N rows of data, the data file I want should be&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc means&amp;nbsp; data=mean_N noprint;&lt;BR /&gt;var _0 _1 _2 _3 _4 _5 _6 _7 _8 _9;&lt;BR /&gt;output out= WANT mean(_0)=A_0 etc&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;Thank you all very much!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2023 15:40:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/macro-to-run-a-simulation-N-times/m-p/873907#M38764</guid>
      <dc:creator>costasRO</dc:creator>
      <dc:date>2023-05-04T15:40:11Z</dc:date>
    </item>
    <item>
      <title>Re: macro to run a simulation N times</title>
      <link>https://communities.sas.com/t5/New-SAS-User/macro-to-run-a-simulation-N-times/m-p/873913#M38765</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It sounds like you may want to modify this data step:&lt;/P&gt;
&lt;PRE&gt;data sim0;
    array x_ {&amp;amp;nvars};

    do j=1 to &amp;amp;nobs;
        do i=1 to dim(x_);
             x_(i)= &amp;amp;mean + &amp;amp;vol*rannor(3452083); /*https://online.stat.psu.edu/stat482/book/export/html/663*/
	s=rand('Uniform');
end;
        output;
    end;
    drop i j;
	
run;&lt;/PRE&gt;
&lt;P&gt;To add a number of runs:&lt;/P&gt;
&lt;PRE&gt;data sim0;
    array x_ {&amp;amp;nvars};

Do run= 1 to 10; /* or yet another macro variable*/
    do j=1 to &amp;amp;nobs;
        do i=1 to dim(x_);
             x_(i)= &amp;amp;mean + &amp;amp;vol*rannor(3452083); /*https://online.stat.psu.edu/stat482/book/export/html/663*/
	s=rand('Uniform');
end;
        output;
    end;
end; /* of the Run loop*/
    drop i j;
	
run;&lt;/PRE&gt;
&lt;P&gt;Then include a BY RUN in all of the following steps. For any of your code that is already using By processing make RUN the first by variable.&lt;/P&gt;
&lt;P&gt;Then the final proc Means would have BY Run as well and get a summary for each of the Runs.&lt;/P&gt;
&lt;P&gt;AFTER that if want analysis ACROSS the runs then add another Proc Means/summary/ whatever analysis that does not use the By Run.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2023 15:58:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/macro-to-run-a-simulation-N-times/m-p/873913#M38765</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-05-04T15:58:31Z</dc:date>
    </item>
    <item>
      <title>Re: macro to run a simulation N times</title>
      <link>https://communities.sas.com/t5/New-SAS-User/macro-to-run-a-simulation-N-times/m-p/873940#M38773</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;This works well.&lt;/P&gt;
&lt;P&gt;The only part I am not sure of is whether&amp;nbsp; the retain command works the same way after I include 2 by's.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Before I had:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data sim6;&lt;BR /&gt;set sim5;&lt;BR /&gt;by id;&lt;BR /&gt;retain y;&lt;BR /&gt;if first.id then do;&lt;BR /&gt;y=&amp;amp;s;&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;shares=y;&lt;BR /&gt;y= y*(1-prop_sold);&lt;BR /&gt;end;&lt;BR /&gt;drop shares;&lt;BR /&gt;rename y=shares;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now I have:&lt;/P&gt;
&lt;P&gt;data sim6;&lt;BR /&gt;set sim5;&lt;BR /&gt;by run id;&lt;BR /&gt;retain y;&lt;BR /&gt;if first.id then do;&lt;BR /&gt;y=&amp;amp;s;&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;shares=y;&lt;BR /&gt;y= y*(1-prop_sold);&lt;BR /&gt;end;&lt;BR /&gt;drop shares;&lt;BR /&gt;rename y=shares;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;Does this piece of the code work the same way with two by's?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you again!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2023 17:19:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/macro-to-run-a-simulation-N-times/m-p/873940#M38773</guid>
      <dc:creator>costasRO</dc:creator>
      <dc:date>2023-05-04T17:19:08Z</dc:date>
    </item>
    <item>
      <title>Re: macro to run a simulation N times</title>
      <link>https://communities.sas.com/t5/New-SAS-User/macro-to-run-a-simulation-N-times/m-p/873979#M38774</link>
      <description>&lt;P&gt;Retain works exactly the same.&lt;/P&gt;
&lt;P&gt;The question would be the First. and/or&amp;nbsp; last. variables when used for resetting the Retained values The separate by&amp;nbsp; variables each have there own first and last. So while within a RUN group you still have first and last for the Id variable as needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Considerations might arise if you sort data though.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A very useful exercise is to create a data set then process that with 2 or 3 by variables and create variable to capture the first and last values so you can examine them in a data set. Dummy code of an example of the second bit.&lt;/P&gt;
&lt;PRE&gt;data examine;  
   set have;
   by a b c;
   Firsta = First.A;
   Firstb = First.B;
   Firstc = First.C;
   Lasta = Last.A;
   Lastb = Last.B;
   Lastc = Last.C;
run;   &lt;/PRE&gt;
&lt;P&gt;I wouldn't make the Have set big having 3 or 4 values of each variable should be sufficient. Make sure to different combinations. This requires sorted data of course for BY processing.&lt;/P&gt;
&lt;P&gt;Addition learning from using not sorted values and add the option NOTSORTED to the BY statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2023 19:04:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/macro-to-run-a-simulation-N-times/m-p/873979#M38774</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-05-04T19:04:19Z</dc:date>
    </item>
    <item>
      <title>Re: macro to run a simulation N times</title>
      <link>https://communities.sas.com/t5/New-SAS-User/macro-to-run-a-simulation-N-times/m-p/873993#M38775</link>
      <description>&lt;P&gt;Thanks, I'll try that. In this case I think I am OK, as the dataset used with retain and first is sorted already, so the first observation in each run id group is the one I want.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2023 20:12:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/macro-to-run-a-simulation-N-times/m-p/873993#M38775</guid>
      <dc:creator>costasRO</dc:creator>
      <dc:date>2023-05-04T20:12:22Z</dc:date>
    </item>
  </channel>
</rss>

