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: Show Command Output in Foreach Loop ...


From   Matthew McKay <matthew.mckay@anu.edu.au>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Show Command Output in Foreach Loop ...
Date   Thu, 4 Jul 2013 00:40:21 +0000

I have taken the following  from Sergiy's response and turned it into a very short program. 
This way the code doesn't become harder to read

** Program pe: Print Execute **
program define pe
	version 12.0
	if `"`0'"' != "" {
		display as text `"`0'"'
		`0'
		display("")
	}
end

One can just prepend pe to each line within the foreach loop. 

sysuse auto
levelsof rep78, local(levs)
foreach lev in `levs' {
  	pe regress price weight if rep78==`lev'
}

Cheers, Matthew


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Sergiy Radyakin
Sent: Monday, 1 July 2013 4:07 PM
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: Show Command Output in Foreach Loop ...

Matthew, it has been an observation in this list that you get a much higher chances of getting responses and faster responses if you supplement your question with a starter like this:

sysuse auto
levelsof rep78, local(levs)
foreach lev in `levs' {
  regress price weight if rep78==`lev'
}
** eof

Then all one has to do is a little edit to the code to get the answer you seek:

sysuse auto
levelsof rep78, local(levs)
foreach lev in `levs' {
  local cmd `"regress price weight if rep78==`lev'"'
  display `"`cmd'"'
  `cmd'
}
** eof

Best, Sergiy



On Sun, Jun 30, 2013 at 10:31 PM, Matthew McKay <matthew.mckay@anu.edu.au> wrote:
> Statalist,
>
> I have seen a few earlier remarks in 2007 about showing commands during foreach loops.
> Option 1:
> Set trace on
> Set tracedepth 1
> This doesn't work that well for me as it clumps the commands together and then the regression output together.
>
> Since then, is there a better way to show the issued command during a foreach loop?
> Why can't it just simply show the executed lines and the regression as if the code was running outside of a foreach loop in one long sequence?
>
> I have multiple datasets that I would like to run the same extensive set of analysis on.
> The smcl log file output is not very reader friendly without the command breaks printed in between each regression.
>
> Cheers,
> Matthew
> Phd Student
> ACDE, Crawford School
>
>
> *
> *   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/
*
*   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/


-----
No virus found in this message.
Checked by AVG - www.avg.com
Version: 2013.0.3345 / Virus Database: 3204/6454 - Release Date: 07/01/13


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