Statalist


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

Re: st: local containing (all) variables' names in a dataset


From   David Kantor <kantor.d@att.net>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: local containing (all) variables' names in a dataset
Date   Mon, 17 Aug 2009 09:51:15 -0400

At 09:38 AM 8/17/2009, Augusto wrote:

I have to collapse a series of datasets and I would appreciate your
help. The issue is that these datasets have different variables, but
always the same variable by which I want to collapse. So I have to run
the following command:

collapse (mean) id (sum) [all other variables here], by(name)

And I would like to embed this in a foreach loop of the type

local datasets `"dataset1 dataset2 dataset3"'
foreach X of local datasets {
     use `X', clear
     collapse (mean) id (sum) [all other variables here], by(name)
     save, replace
}

The issue for me is how to create a local that contains the names of
all variables in each dataset, except for "name" and "id". Any
suggestions are appreciated!


Set-up:
local nameandid "name id"

Within your loop put this:

  describe using `X' , varlist
  local vlist1 : list r(varlist) - nameandid
  use `X', clear
  collapse (mean) id (sum) `vlist1', by(name)

----
I have not tested the above; not sure if r(varlist) is legal in that context. If not, do something like this after the describe:
  local vlist0 "`r(varlist)'"
  local vlist1 : list vlist0 - nameandid
----
HTH
--David

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