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]

st: use of tempfile


From   Prakash Singh <prakashbhu@gmail.com>
To   statalist <statalist@hsphsun2.harvard.edu>
Subject   st: use of tempfile
Date   Thu, 19 Apr 2012 12:08:39 +0530

Dear stata listers

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
}


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