Bookmark and Share

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


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

Re: st: a, perhaps, not so really silly question


From   Eric Booth <[email protected]>
To   "<[email protected]>" <[email protected]>
Subject   Re: st: a, perhaps, not so really silly question
Date   Tue, 8 Mar 2011 19:28:21 +0000

<>
On Mar 8, 2011, at 12:52 PM, Scott Talkington wrote:
> 
> Another typical problem I have that's a bit more complicated than the "tab" example involves executing a series of commands on pairs of variables.  There is lots of discussion on nested "for" loops, but how would one *not* nest the loops?  Specifically I want to execute the following list of commands on paired variables x and y:
> 
> tabxml, dv(`y') ivd(rchurch) options(n) split(mono) osvy save( C:\data\NLSY Children of 79\Deliverables/`x'IN)
> tabxml, dv(`y') ivd(rchaste) options(n) split(mono) osvy save( C:\data\NLSY Children of 79\Deliverables/`x'IIN)
> tabxml, dv(`y') ivd(rchurch) options(per) tf(2) split(mono) percent(row) osvy save( C:\data\NLSY Children of 79\Deliverables/`x'IP)
> tabxml, dv(`y') ivd(rchaste) options(per) tf(2) split(mono) percent(row) osvy save( C:\data\NLSY Children of 79\Deliverables/`x'IIP)
> tabxml, dv(`y') ivd(churchx) options(per nototal) tf(2) split(rprom) percent(row) osvy save( C:\data\NLSY Children of 79\Deliverables/`x'IIIPa)
> tabxml, dv(`y') ivd(churchx) subcond(mono==1) options(per nototal) tf(2) split(rprom) percent(row) osvy save( C:\data\NLSY Children of 79\Deliverables/`x'IIIPb)
> tabxml, dv(`y') ivd(churchx) subcond(mono==0) options(per nototal) tf(2) split(rprom) percent(row) osvy save( C:\data\NLSY Children of 79\Deliverables/`x'IIIPc)
> 
> What I've been doing is manually replacing the items in quotations above the commands and just running the same do file over and over, but it's getting kind of tedious for 20 variable pairs.
> 
> loc x="DroppedOut"
> loc y="drop"
> 
> then replace with
> 
> loc x="RepeatedGrade"
> loc y="rgrade"
> 
> and run again, etc.  Seems like I should  be able to use a for loop with variable pairs instead of single variables, but it just escapes me how to do it.  Does no one ever want to do something like this?


My earlier post hinted at this issue.  
If you want to run every combination of x and y then this would work:

foreach x in "DroppedOut" "RepeatedGrade"  "Whatever" {
	foreach y in "drop" "rgrade" "otherstuff" {
tabxml, dv(`y') ivd(rchurch) options(n) split(mono) osvy save( C:\data\NLSY Children of 79\Deliverables/`x'IN)
tabxml, dv(`y') ivd(rchaste) options(n) split(mono) osvy save( C:\data\NLSY Children of 79\Deliverables/`x'IIN)
tabxml, dv(`y') ivd(rchurch) options(per) tf(2) split(mono) percent(row) osvy save( C:\data\NLSY Children of 79\Deliverables/`x'IP)
tabxml, dv(`y') ivd(rchaste) options(per) tf(2) split(mono) percent(row) osvy save( C:\data\NLSY Children of 79\Deliverables/`x'IIP)
...etc.....
	}
}
Would run the above commands with "Droppedout" as x and then "drop" as y, then "Droppedout" as x and "rgrade" as y and so on....



If you don't want every possible combination, but only specified pairs, you can use something like this:

loc vars   DroppedOut drop     RepeatedGrade rgrade     Whatever  otherstuff   
token `"`vars'"'
while `"`1'"' != "" {
	di "`1' and `2'"
tabxml, dv(`2') ivd(rchurch) options(n) split(mono) osvy save( C:\data\NLSY Children of 79\Deliverables/`1'IN)
tabxml, dv(`2') ivd(rchaste) options(n) split(mono) osvy save( C:\data\NLSY Children of 79\Deliverables/`1'IIN)
tabxml, dv(`2') ivd(rchurch) options(per) tf(2) split(mono) percent(row) osvy save( C:\data\NLSY Children of 79\Deliverables/`1'IP)
tabxml, dv(`2') ivd(rchaste) options(per) tf(2) split(mono) percent(row) osvy save( C:\data\NLSY Children of 79\Deliverables/`1'IIP)
......etc......
		macro shift 2
	}

Which would run on DroppedOut as x or `1' and then drop as y or `2', then it would skip to the next pair and run Repeatedgrade as x and rgrade as y and so on...

__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]
Office: +979.845.6754






*
*   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index