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]

Re: st: How to Match Observations Over Different Date variables


From   Muhammad Anees <anees@aneconomist.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: How to Match Observations Over Different Date variables
Date   Wed, 17 Aug 2011 16:19:48 +0500

Yes Nick, I can create the datenew variable using same name on two
different data files, and then will serve as ID variable to match the
data on, after all files are formatted.

Basically I needed to match two stock prices which are listed on two
time variables where the two time variables are having different empty
dates as the initial post describes. Basically I have to split and
then merge the two data files, based on the date variable matching the
two stock prices at the same dates. I have achieved my required format
of data following  Tirthankar. My do file actually contain an extra
line for renaming the date variable which is surely not needed when
working on two different data files.

I am thankful for the guidance. My problem has been resolved using the
discussion and suggestions.

On Wed, Aug 17, 2011 at 1:18 PM, Nick Cox <njcoxstata@gmail.com> wrote:
> On a minor note, why do you have code like
>
>  generate datenew1 = date(date1, "DMY")
>  format mydate %td
>  ren datenew1 date
>
> ?
>
> The -rename- is not needed. You can use the name you want at the
> outset. Also, what is -mydate-? Presumably the need is to -format- the
> variable you just created.
>
>  generate date = date(date1, "DMY")
>  format date %td
>
> On Wed, Aug 17, 2011 at 8:25 AM, Muhammad Anees <anees@aneconomist.com> wrote:
>> Thanks tirthankar!
>>
>> You suggestion does the job!. I appreciate your suggestion.
>>
>> However, looking into the suggestion, I tried a version of a do file,
>> which looks like below! I would appreciate if you correct me.
>>
>> generate datenew1 = date(date1, "DMY")
>> format mydate %td
>> ren datenew1 date
>> sort date
>> save date1
>> generate datenew2 = date(date2, "DMY")
>> format mydate %td
>> ren datenew2 date
>> sort date
>> save date2
>> merge 1:1 dates using `data1',
>>
>>
>> On Wed, Aug 17, 2011 at 11:44 AM, Tirthankar Chakravarty
>> <tirthankar.chakravarty@gmail.com> wrote:
>>> /*******************************************/
>>> clear*
>>> tempfile data1 data2
>>> input str10 date str1 comma float year float stock1
>>> Jan1,2005    40
>>> Jan2,2005    40
>>> Jan3,2005    40
>>> Jan4,2005    40
>>> Jan5,2005    40
>>> Jan8,2005    40
>>> Jan9,2005    40
>>> Jan10,2005    40
>>> Jan11,2005    40
>>> Jan12,2005    40
>>> end
>>> g datestr = date+"-"+string(year)
>>> g dates = daily(datestr, "MDY")
>>> drop date comma year datestr
>>> format dates %td
>>> save `data1', replace
>>>
>>> clear*
>>> input str10 date str1 comma float year float stock2
>>>  Jan1,2005     30
>>>  Jan2,2005     30
>>>  Jan3,2005     30
>>>  Jan4,2005     30
>>>  Jan8,2005     30
>>>  Jan9,2005     30
>>>  Jan11,2005     30
>>>   Jan12,2005     30
>>>   Jan13,2005     30
>>>   Jan14,2005     30
>>> end
>>> g datestr = date+"-"+string(year)
>>> g dates = daily(datestr, "MDY")
>>> drop date comma year datestr
>>> format dates %td
>>> save `data2', replace
>>>
>>> // merge
>>> merge 1:1 dates using `data1',
>>>
>>> // create the individuals dates
>>> g date1 = dates if(inlist(_merge, 3, 1))
>>> g date2 = dates if(inlist(_merge, 3, 2))
>>> format date* %td
>>>
>>> li
>>> /*******************************************/
>>>
>>> T
>>>
>>> On Tue, Aug 16, 2011 at 10:30 PM, Muhammad Anees <anees@aneconomist.com> wrote:
>>>> Dear Listers!
>>>>
>>>> I have been working on stock prices of different firms from Jan 1,
>>>> 2005 to October 30, 2010. The problem I face is that every stock price
>>>> is obtained and is listed with somewhat different date variables and
>>>> all the date variables are not axactly matching. I need to analyse
>>>> stock prices after matching them over dates. I know how to format the
>>>> date variables in Stata with the Help of Nick and others after a
>>>> discussion on this list, but even working for weeks, I could not
>>>> figure out how to match the stock prices over a similar date index.
>>>>
>>>> My example of data looks like below:
>>>>
>>>> Date1          Stock1          Date 2          Stock2
>>>> Jan1,2005    40                 Jan1,2005     30
>>>> Jan2,2005    40                 Jan2,2005     30
>>>> Jan3,2005    40                 Jan3,2005     30
>>>> Jan4,2005    40                 Jan4,2005     30
>>>> Jan5,2005    40                 Jan8,2005     30
>>>> Jan8,2005    40                 Jan9,2005     30
>>>> Jan9,2005    40                 Jan11,2005     30
>>>> Jan10,2005    40                 Jan12,2005     30
>>>> Jan11,2005    40                 Jan13,2005     30
>>>> Jan12,2005    40                 Jan14,2005     30
>>>> and so till last date on the date1 variable.
>>>>
>>>> Now what I want is to have the data like
>>>>
>>>> Date1          Stock1          Date 2          Stock2
>>>> Jan1,2005    40                 Jan1,2005     30
>>>> Jan2,2005    40                 Jan2,2005     30
>>>> Jan3,2005    40                 Jan3,2005     30
>>>> Jan4,2005    40                 Jan4,2005     30
>>>> Jan5,2005    40                 .                     .
>>>> jan6,2005      40                .                      .
>>>> .                       .                  .                      .
>>>> .                       .                  .                      .
>>>> Jan9,2005    40                 Jan9,2005     30
>>>> Jan10,2005    40                 .                     .
>>>> Jan11,2005    40                 Jan112005     30
>>>> Jan12,2005    40                 Jan122005     30
>>>> and so on.
>>>>
>>>> Now my new data should look like above. What I need is to have a
>>>> missing value for date2 and stock2 if it has missing stock data on a
>>>> date where date1 and stock1 has a stock data. It further implies both
>>>> the stocks should be arranges on a single data variable if stock1 is
>>>> present and stock is not, stock2 should treated is missing. It is
>>>> evident that if both stocks are missing then it would not be a real
>>>> problem is both have missing date. My real problem is that I could not
>>>> arrange the the stocks over a single date variable.
>>>> I would be thankful to your suggestion and guidance.
>
> *
> *   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/
>



-- 


Regards

Anees

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