Bookmark and Share

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]

st: use of tempfile


From   Prakash Singh <[email protected]>
To   statalist <[email protected]>
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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index