Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# Re: st: looping over a variable list of variables

 From Nick Cox To "statalist@hsphsun2.harvard.edu" Subject Re: st: looping over a variable list of variables Date Fri, 23 Nov 2012 18:54:35 +0000

```Using -egen-'s -group()- function in every case might help.

In Stata local macros are not regarded as variables.

Nick

```
```
```
```Hi,

```
Suppose I have a set of variables v1, v2, v3, ..., vn where each variable takes a value equal to 0, 1, 2. I want to write a program that can do the following:
```
```
1. If the program is supplied with v1, then it loops over v1 = 0, 1, 2 and does some operation for each value of v1 . Here I can achieve it with a single - forvalues - loop
```
```
2. If the program is supplied with v1 & v2, then it loops over v1 = 0,1,2 and v2=0,1,2 (i.e. a total of 9 loops), and does some operation for each combination of v1 & v2. Here I can achieve it with two nested -forvalues- loops
```
```
3. Similarly, if v1, v2, v3 are supplied, I can achieve it with three nested -forvalues- loops
```
```
The problem can get out of hand if v1, v2, v3, ..., vn is supplied to the program as I need to hard-code the nested loops structures up to a large value of n.
```
```
An alternative is to run one - forvalues - or -forvalues- loop with all permutations of v1, v2, v3,..., vn already determined in a local macro variable.
```
```
Is there way / function that can return all permutations of v1, v2, v3 ..., vn in a local macro variable(s) ?
```

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