Turn on suggestions

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

Showing results for

Options

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 02-22-2008 02:19 PM
(1073 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thank you very much!!

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

**If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. **

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.