Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: use random values in a calculation

 From Eric Booth To "" Subject Re: st: use random values in a calculation Date Mon, 4 Jul 2011 17:06:07 +0000

```<>
On Jul 4, 2011, at 9:52 AM, Zhi Su wrote:
> The ratio to multiply one of the variables could be 0 too.

The opt* variables created in my example already contained zero.  Type -tab opt1- after it is created in my example to see this.
(The -replace- commands in the opt* loop start with .05 because zero was already created as part of the -generate- command in that loop)

> Probably
> I do not ask the question right. The variables v1, v2, v3, v4 are
> known.

Yes, but not to me.  So that why I included this part of my solution:
>>
>> **create fake v1-v4 variables for example:
>> forv nn = 1/4 {
>>   g v`nn' = runiform()*10
>>   }
to create v1-v4.  You can obviously omit this part if you've already got your data loaded into memory.

> The ratios multiply them are chosen from (0,0.05,0.1,0.15,0.2).
> Then each variable multiplied by a ratio are sum up. Totally, there
> are 5*5*5*5=625 combination for these four variables multipled by a
> ratio. Then I generate 625 variables for 625 types of sum.
>

In your original post you said that the ratios (opt*) are _randomly_ chosen to create sum 625 variables, which is what my example gave you...now it sounds more like you want every possible combination of the opt* and v1-v4 variables (?)  [Though that's going to provide a lot more than 625 combinations]  Here's an example for every v* x opt* combination:

***********************
clear
set obs 1000 // //changed from 625 obs
g var = "s" + string(_n)  //thanks to Nick Cox for pointing out this change

**create fake v1-v4 variables for example:
//skip this if you are using your real v1-v4
forv nn = 1/4 {
g v`nn' = runiform()*10
}
l in 1/9  //view fake v1 - v4 vars

**multiply each v1 by each ratio:
foreach var of varlist v1-v4 {
foreach n1 in 0  05 1  15  2  {
foreach n2 in 0  05 1  15  2  {
foreach n3 in 0  05 1  15  2  {
foreach n4 in 0  05 1  15  2  {

g s`var'_`n1'_`n2'_`n3'_`n4' = (v1*`n1')+(v2*`n2')+(v3*`n3')+(v4*`n4')

} //end n1 loop
} //end n2 loop
} //end n3 loop
} //end n4 loop
} //end var loop
**also, you may want to check out -tuples- from SSC

************************

You should (1) provide an example of what you want -- give us some example and/or hypothetical lines of data & variables that you have as well as some lines/variables that you want to see and (2) explain how/why either of the 2 examples do not solve your issue -- we can't guess what's not doing what you expect here (for instance, the first solution I gave provided 625 'sum' variables -- did you mean observations?  did you expect to see the v1-v4 variables retained in the dataset? etc...)

- Eric
__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
ebooth@ppri.tamu.edu

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```