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]

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


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: How to Match Observations Over Different Date variables
Date   Wed, 17 Aug 2011 09:18:39 +0100

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 <[email protected]> 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
> <[email protected]> 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 <[email protected]> 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/


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