Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

st: how to have commands in a loop (over strings) shown in log files, without using nested quotes


From   tough luck <tough.luck@live.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: how to have commands in a loop (over strings) shown in log files, without using nested quotes
Date   Tue, 7 Aug 2012 10:40:01 -0400

Hi,
I am trying to run a command with several different "if" conditions, and have both the commands and the results shown in a log file.
The resulting log I want are like this...

log using...
table var1 var2 if var3 == "a", c(n var4)
(...the resulting table...)

table var1 var2 if var3 == "b", c(n var4)
(...the resulting table...)

table var1 var2 if var3 == "", c(n var4)
(...the resulting table...)
log close

If I use a loop to avoid typing the command repeatedly...

foreach val in "a" "b" "" {
table var1 var2 if var3 == "`val'", c(n var4)
}

The problem with the loop is, only the resulting table would show in the log file, not the actual command.

log using...
...table 1...
...table 2...
...table 3...
log close

If I try to do

log using...
foreach ... {
display "cmd..."
cmd...
}
log close

I would need to somehow (e.g. using `"') include double quotes in the cmd string, which could very easily introduce bugs that are hard to fix.

I'm wondering if there is a way to produce these log files without having to type each individual command or going into the whole double quotes inside of a string issue.

Any help and comments would be greatly appreciated.

Thanks,
Xinjia
 		 	   		  
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index