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]

st: combine foreach and if


From   Vincent Koppelmans <[email protected]>
To   [email protected]
Subject   st: combine foreach and if
Date   Tue, 4 Mar 2014 15:02:37 -0500

I want to loop over a couple of time points in a dataset and filer out certain rows.
I tried to combine a foreach loop with an if statement, but it is not working (it only prints the results using the first value of the numlist):


foreach TIME of numlist 1 5 {
if time == `TIME' {
summ DS_sec_
plot DS_sec_ Subject_ID
}
}


The following code however is working:

foreach TIME of numlist 1 5 {
summ DS_sec_ if time == `TIME'
plot DS_sec_ Subject_ID if time == `TIME'
}

In this simple example the syntax does not differ a lot in length, but the actual code that needs to be executed is much longer. I therefore would prefer a solution that resembles my initial code, because that would mean that I do not have to add the "if time == `TIME'" part after each line.

How can I do this?

Thanks,

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


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