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 on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: save a subset of data


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: save a subset of data
Date   Thu, 22 Mar 2012 07:14:16 +0000

Brendan's solution is direct and would no doubt be the solution that
occurs to many users. It's however a puzzle why -save- does not have
the flexibility to -save- some of the dataset, which is predictable
enough as something you might want to do. If there is a good reason
for this it is tied up with Stata's logic of keeping track of whether
your dataset has changed, as a safety feature that (for example) warns
you if you try to -exit- without having -save-d changes to your data.

. sysuse auto, clear
(1978 Automobile Data)

. di c(changed)
0

. keep make mpg

. save march22
file march22.dta saved

. di c(changed)
0

c(changed) answers one question: has the data changed since last
-save-d; and the answer is here 0, meaning No, and the answer is
undoubtedly correct from Stata's point of view. However, this way the
user could easily lose a great deal of work. This is not a bug or a
misfeature; -keep- is destructive and the user gets precisely what is
requested; as in many folk tales, the punishment is that you got
exactly what you asked for.

 However, there is a safer way to do this and it is given by
-savesome- (SSC) which is in its entirety

. ssc type savesome.ado
*! NJC 1.0.1 9 August 2011
*! NJC 1.0.0 25 April 2001
program def savesome
        version 7.0
        syntax [varlist] [if] [in] using/ [ , * ]
        preserve
        qui {
                if `"`if'`in'"' != "" { keep `if' `in' }
                keep `varlist'
        }
        save `"`using'"', `options'
end

Here it is again in practice

. sysuse auto, clear
(1978 Automobile Data)

. savesome make mpg using march22 , replace
file march22.dta saved

. di c(changed)
0

This time c(changed) is likely to be what the user hopes for.

Nick

On Wed, Mar 21, 2012 at 10:18 PM, Brendan Halpin <brendan.halpin@ul.ie> wrote:
> On Wed, Mar 21 2012, tashi lama wrote:
>
>> I want to list the variables and save... may be sth like this...
>>
>>
>> list var1 var2, saving(filename)
>
> keep var1 var2
> save filename
*
*   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