SAS Optimization, and SAS Simulation Studio

turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-22-2008 02:19 PM

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?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to deleted_user

02-22-2008 03:18 PM

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to 1162

02-22-2008 05:26 PM

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;

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to deleted_user

02-23-2008 05:45 PM

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]

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]

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Cynthia_sas

02-25-2008 11:30 AM

Thank you very much!!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to deleted_user

03-31-2009 06:28 PM

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.

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.