Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

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

st: use of tempfile

From   Prakash Singh <>
To   statalist <>
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 {
  keep if state_code==`i'
  foreach var of varlist `vars' {
    ren `var' `var'_`i'
  sort common_id
  save `mydata', replace
 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:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index