I'm working with a Monte Carlo simulation code. In each iteration of my "Do loop" I generate a series of random numbers from a lognormal population. Then for each iteration I add all those random numbers. How can I create a new variable or data set containing only those totals, in other words, containing the result of the sum of the random numbers in each iteration?
How are you doing the addition? I can think of several solutions to your question, but some might fit better with your existing code.
One solution is to use PROC SQL to calculate the sum and store it as a macro variable. Then you can add this variable to a data set at the end of each loop.
Another solution is to use PROC MEANS and create a temporary output dataset with the sum for that loop. Then I would use PROC DATASETS to append that result to a 'master' dataset. I prefer this solution.
[pre]
proc means data=work.loopdata;
var number;
output out=work.tmpsum sum=sum;
run;
I'm sure there are others and variations of the ones I mentioned, but as I said, some of these may be better suited to your program.
Message was edited by: 1162
This is the code, x3 is a cumulative variable. When the second DO finishes (j=x1), x3 has the total sum for the first iteration of the first DO. What I want is to create a data set with those totals. Because I have to work with that data, caculating percentiles, etc..
data A;
do i=1 to 1000;
x1=ranpoi(123,68);
x3=0;
do j=1 to x1;
x2=exp(3.52918406+0.65656975*rannor(123));
x3=x3+x2;
output;
end;
Hi:
I'm not sure whether this is what you want. Consider the difference in output between DATA set A and data set B in this program. I'm not going to post the PROC PRINTS, because they're both too lengthy. Note that I've added a variable called WHEREAT so you can see in the PROC PRINT where each ROW was output from (the inner loop or the outer loop).
cynthia
[pre]
data A B;
do i=1 to 1000;
x1=ranpoi(123,68);
x3=0;
do j=1 to x1;
x2=exp(3.52918406+0.65656975*rannor(123));
x3=x3+x2;
** output to A for every iteration through the inner loop;
whereat = 'inner loop for j';
output A;
** output to B only when J=x1 in the inner loop;
if j=x1 then output B;
end;
whereat = 'outer loop for i';
** output to A for every iteration in the outer loop;
output A;
end;
run;
I am trying to estimate a cost model in proc model and obtain predicted values by varying the inputs. Can the Monte Carlo (or solve statement) do this? That is, instead of varying the parameter estimates of the model, I want to allow the input variables to vary (which of course will vary the parameter estimates) and then obtain predicted values for the cost function.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Registration is open
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss. Register now and lock in 2025 pricing—just $495!