BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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?
5 REPLIES 5
1162
Calcite | Level 5
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;

proc append base=work.mastersum data=work.tmpsum;
run;
[/pre]

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
deleted_user
Not applicable
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;

output;
end;
run;
Cynthia_sas
SAS Super FREQ
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;

proc print data=A(obs=5000);
title 'DATA A';
run;

proc print data=B;
title 'DATA B';
run;
[/pre]
deleted_user
Not applicable
Thank you very much!!
deleted_user
Not applicable
Hi,

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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 2008 views
  • 0 likes
  • 3 in conversation