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 at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: use of tempfile


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: use of tempfile
Date   Thu, 19 Apr 2012 09:34:47 +0100

It sounds as if you have precisely one data file and you want to
restructure it. The word description "break the data around this
variable state_code" could mean all sorts of different things.

I suspect that you are reinventing -reshape-. That's just a guess.

What is most obviously missing here is a concrete description of the
underlying problem, i.e. a miniature example of the data you have
_and_ a miniature example of the data structure you want. If you do
that, then more experienced users have a better chance of seeing
immediately that you need -reshape-, or -separate-, or you really do
need to do something ad hoc with multiple -save-s and -merge-s.

The fact that you are using -tempfile-seems incidental.

Nick

On Thu, Apr 19, 2012 at 7:38 AM, Prakash Singh <prakashbhu@gmail.com> wrote:

> I have a stata data for reason I need to break the data around this
> variable state_code and re merge it according to my requirement. I
> have written this code but the data I am getting finally after merging
> has variable for last state only. Important issue is I want the final
> remerged data but not the individual state data in addition.
>
> Any suggestion to improve my code.
>
> tempfile mydata
> use "sales.dta", clear
> local vars "state_code exp_val sales_val"
> levelsof state_code, local(state)
> foreach s of local state {
>  preserve
>  keep if state_code==`i'
>  foreach var of varlist `vars' {
>    ren `var' `var'_`i'
>  }
>  sort common_id
>  save `mydata', replace
>  restore
>  }
>  contract common_id
>  drop _freq
>  foreach s of local state {
>   merge common_id using `mydata'
>   drop _merge
>   sort common_id
>  }
> save "sales_1.dta",replace
> }

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