Statalist


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

Re: st: AW: parse last command from ado file?


From   Nick Winter <nwinter@virginia.edu>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: AW: parse last command from ado file?
Date   Tue, 03 Nov 2009 12:31:44 -0500

Another approach: create your own version of the -tab- command, that puts the command it executes into the review window:

program mytab
	window push tabulate `0'
	tabulate `0'
end

The first line puts the command itself onto the review list. So in your do file, use -mytab- instead of -tab-.


Yet another: create your own version of the -tab- command that figures out which variable has fewer categories:

program mytab2
	capture tab `1'
	local r1 = r(r)
	capture tab `2'
	if r(r)<=`r1' {
		tab `1' `2'
	}
	else {
		tab `2' `1'
	}
end

This isn't perfect because the display width depends on the width of the columns, not just the number, but it's a start.

- Nick Winter




Martin Weiss wrote:
<>
If this is all about -tab-, why not


*************
pr tabswap
	version 10.1
	tab `1' `2'
	tab `2' `1'
end
*************

and yes, I do know I have violated all existing rules for good programming,
but still: Stefan can take this further in any direction he wants to.
- tabswap rep tr- does produce both tables, though...

HTH
Martin


-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von
Stefan.Gawrich@hlpug.hessen.de
Gesendet: Dienstag, 3. November 2009 17:31
An: statalist@hsphsun2.harvard.edu
Betreff: st: parse last command from ado file?

Hi Statalisters,

working in the review window is it possible to access the last command
from an ado file?
My example:
I create a lot of bivariate tables from the review window and e.g. later
copy them to a spreadsheet. Often tables are too wide but fit with rows and columns interchanged.

sysuse auto
tab rep78 trunk // too wide
tab trunk rep78 // fits

I thought about sparing some typing by writing a small ado that swaps
rows and columns of the last shown table.
For this I need to parse the last command inside the ado. If for
instance I could have it in a global (without typing the next line, of
course)
global tabcmd "tab rep78 trunk, row col"


...I could write a program like this:
program define tt
local commapos = strpos("$tabcmd",",")
local options = substr("$tabcmd",`commapos',.)
local commapos1 = `commapos' - 1
local tabc = substr("$tabcmd'",1, `commapos1')
di "`tabc'"
local tab : word 1 of `tabc'
local var1 : word 3 of `tabc'
local var2 : word 2 of `tabc'
`tab' `var1' `var2' `options'
end

and exchange rows and columns of the last table by simply typing "tt".
(A more sophisticated program could also swap row and column options and
so on)


Regards
Stefan



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


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

--
--------------------------------------------------------------
Nicholas Winter                                 434.924.6994 t
Assistant Professor                             434.924.3359 f
Department of Politics                  nwinter@virginia.edu e
University of Virginia          faculty.virginia.edu/nwinter w
PO Box 400787, 100 Cabell Hall
Charlottesville, VA 22904

*
*   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   |   What's new   |   Site index