Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: use of tempfile
From
Nick Cox <[email protected]>
To
[email protected]
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 <[email protected]> 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/