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.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Nick Cox <njcoxstata@gmail.com> |

To |
"statalist@hsphsun2.harvard.edu" <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 eachvariable takes a value equal to 0, 1, 2. I want to write a programthat 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 achieveit with a single - forvalues - loop2. 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 someoperation for each combination of v1 & v2. Here I can achieve itwith two nested -forvalues- loops3. Similarly, if v1, v2, v3 are supplied, I can achieve it withthree nested -forvalues- loopsThe problem can get out of hand if v1, v2, v3, ..., vn is suppliedto the program as I need to hard-code the nested loops structures upto a large value of n.An alternative is to run one - forvalues - or -forvalues- loop withall permutations of v1, v2, v3,..., vn already determined in a localmacro 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/

**References**:**st: looping over a variable list of variables***From:*Pradipto Banerjee <pradipto.banerjee@adainvestments.com>

- Prev by Date:
**st: looping over a variable list of variables** - Next by Date:
**Re: st: Re: Using parent id variables in household data with Stata** - Previous by thread:
**st: looping over a variable list of variables** - Index(es):