Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: storing values from within "by varname :" command

From   "Michael Blasnik" <>
To   <>
Subject   Re: st: storing values from within "by varname :" command
Date   Sun, 22 Oct 2006 11:43:02 -0400

This type of code can be useful in some circumstances, but I think you should first look at -statsby- which basically does the same thing for almost any Stata command that returns results, only faster and in a single line.

Michael Blasnik

----- Original Message ----- From: "David Elliott" <>
To: <>
Sent: Sunday, October 22, 2006 10:19 AM
Subject: Re: st: storing values from within "by varname :" command


Unless the command you are iterating for the by variables allows some
way to save values, or if you actually add that feature to the ado you
are calling, I don't believe what you want is possible within the
context of the by command.

There is an alternative, however:

// This assumes variables region and group are numeric
// If they are strings the commands postfile and post and the ifcmd must
// be altered accordingly

tempname h
postfile `h' region_level group_level count using mycounts, replace

levelsof region, local(l_region)
levelsof group, local(l_group)

foreach region_level of local l_region {
foreach group_level of local l_group {
count if region==`region_level' & group==`group_level'
// count is returned in r(N) so
local count=`r(N)'
post `h' (`region_level') (`group_level') (`count')

postclose `h'
use mycounts, clear


With a little work, this could be adoed to accept a more general case
of passthru commands and capture the postfile output, even
transferring variable and value labels to the new file.
*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index