Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
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. NickOn 23 Nov 2012, at 18:28, Pradipto Banerjee <pradipto.banerjee@adainvestments.com > wrote:
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 - 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 some operation for each combination of v1 & v2. Here I can achieve it with two nested -forvalues- loops3. Similarly, if v1, v2, v3 are supplied, I can achieve it with three nested -forvalues- loopsThe 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/