Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Re: new variable from 2 datasets


From   "Eric G. Wruck" <[email protected]>
To   [email protected]
Subject   Re: st: Re: new variable from 2 datasets
Date   Sat, 12 Nov 2005 14:31:06 -0500

I don't think that'll work, Friedrich, since Wanli says the variable names are the same in both files.  Now if he (perhaps a she -- je ne sais pas) knows that each data set has the same ten variables & each has N observations, then something like this would be possible:

use dataset1.dta, clear
  append datatset2.dta

  foreach var of varlist ....{
    replace `var' = `var' - `var'[_n - N] if _n > N
  }

Or to put it more concretely in terms of an example, assume dataset1 has variables x & y & dataset2 the same variables; each has four observations.  After appending,


  +-------+
  | x   y |
  |-------|
  | 1   2 |
  | 2   3 |
  | 3   4 |
  | 4   5 |
  | 1   2 |
  |-------|
  | 2   3 |
  | 3   3 |
  | 4   5 |
  +-------+

. foreach var of varlist x y {
  2.     replace `var' = `var' - `var'[_n - 4] if _n > 4
  3. }
(4 real changes made)
(4 real changes made)

. l, noobs

  +--------+
  | x    y |
  |--------|
  | 1    2 |
  | 2    3 |
  | 3    4 |
  | 4    5 |
  | 0    0 |
  |--------|
  | 0    0 |
  | 0   -1 |
  | 0    0 |
  +--------+


If the data sets were identical, observations 5-8 would contain all zeros -- not the case here.  I haven't given any thought to string variables, but undoubtedly something quasi-similar could be done for them.

Hope this helps,

Eric

 

>Wanli,
>
>Rename the variables in one file, merge the data and then calculate
>the difference.
>
>Friedrich Huebler
>
>--- Wanli Zhao <[email protected]> wrote:
>> I have 2 datasets. Each one has 10 variables and they share the
>> same names
>> across the 2 data files. Is there a quick way to generate the third
>> file
>> which has the differenced values for each variables in the 2
>> original files?
>> Thanks.
>>
>> Cheers,
>> Wanli
>
>
>
>		
>__________________________________
>Yahoo! FareChase: Search multiple travel sites in one click.
>http://farechase.yahoo.com
>*
>*   For searches and help try:
>*   http://www.stata.com/support/faqs/res/findit.html
>*   http://www.stata.com/support/statalist/faq
>*   http://www.ats.ucla.edu/stat/stata/


-- 

===================================================

       Eric G. Wruck
       Econalytics
       2535 Sherwood Road
       Columbus, OH  43209

       ph:      614.231.5034
       cell:    614.330.8846
       eFax:    614.573.6639
       eMail:   [email protected]
       website: http://www.econalytics.com

====================================================
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index