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: An UltraEdit script for echoing a command within a loop


From   Kieran McCaul <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: An UltraEdit script for echoing a command within a loop
Date   Thu, 24 May 2012 09:13:44 +0800

...

Note: UltraEdit is a text file editor so if you don't use it, what follows might not be of much use to you.

I was prompted to post this by a comment Ronan made earlier this week http://www.stata.com/statalist/archive/2012-05/msg00966.html

Briefly, the following would produce the results of a series of t-tests:

foreach var of varlist camcogt cvlttotal cvltsdfr cvltldfr dsdf   {
	forvalues inter = 0/1  {
		foreach time of num  3 12 24  {
			forvalues sex = 0/1   {
				ttest `var' = 0 if  time==`time' & intervention==`inter' & sex==`sex'
			}
		}
	}
}

The problem is that while the output will show the results from each t-test, the actual t-test command that produced each of them is not echoed back, and as a consequence interpreting the results will be difficult to say the least.

A solution is to insert a line above the t-test command which displays the line:
di `"ttest `var' = 0 if  time==`time' & intervention==`inter' & sex==`sex'"' 
ttest `var' = 0 if  time==`time' & intervention==`inter' & sex==`sex'

Having to do this every time you have commands within a loop is somewhat cumbersome, but it can be automated UltraEdit using the following script:

UltraEdit.activeDocument.selectLine();
UltraEdit.activeDocument.copy();
var strlen = (UltraEdit.clipboardContent.length);
var clip1 = UltraEdit.clipboardContent.substr(0,strlen-2);
var clip2 = "di `\"" + clip1.replace(/^\s*|\s*$/g,'') + "\"' ";
UltraEdit.clipboardContent = clip2; 
var nLineNumber = UltraEdit.activeDocument.currentLineNum;
UltraEdit.activeDocument.gotoLine(nLineNumber-1,0);
UltraEdit.insertMode();
UltraEdit.activeDocument.insertLine();
UltraEdit.activeDocument.paste();
 

Save this to a file with the extension .js and then in UltraEdit go to the Scripting Menu and click on Scripts and Add the file and assign a hotkey (I use Alt+D).

Now if I open the t-test program in UltraEdit, I simply have to put the cursor anywhere in the line with the t-test command and then hit Alt D.  The -display- line will be automatically inserted above the t-test line.

I've been using this for a while and it's worked fine.  

I'd also like to acknowledge the help of my colleague Zoe Hyde (the girl without the dragon tattoo), who sorted out the regular expression syntax for me.



______________________________________________
Kieran McCaul MPH PhD
WA Centre for Health & Ageing (M577)
University of Western Australia
Level 6, Ainslie House
48 Murray St
Perth 6000
Phone: (08) 9224-2701
Fax: (08) 9224 8009
email: [email protected]
http://myprofile.cos.com/mccaul 
http://www.researcherid.com/rid/B-8751-2008

"One can be beaten up in school simply by referring to oneself as one" - Sheldon
__________________________________________________________________________
IMPORTANT: This email is intended for the use of the individual addressee(s) named above and may
contain information that is confidential, privileged or unsuitable for overly sensitive persons with low
self-esteem, no sense of humour or irrational religious beliefs. If you are not the intended recipient, 
any dissemination, distribution or copying of this email is not authorised (either explicitly or implicitly) 
and constitutes an irritating social faux pas.


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