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: RE: changing observations under one variable


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   RE: st: RE: changing observations under one variable
Date   Tue, 24 Apr 2012 20:18:05 +0100

The examples show that -date()- and -ym()- can be used to convert dates. I've given a reference with several paragraphs of detail. I've encouraged you to read the help for yourself. 

Nick 
[email protected] 

Rudy Hover

Thank you for the tip on how to display dates. 

However, it does not actually convert any value? I need to change the value 199901 to what stata uses, i.e. 468. 

This works when the input is in the format -29jan1999- and then using 

gen monthdate=mofd(input)

As you described.

But it does not work when the input is in the format -199901-.

I need to actually convert both formats to the -468- format because later on I want to merge using the date as unique identifying variable. 

On Apr 24, 2012, at 20:35, Nick Cox <[email protected]> wrote:

> There is help on dates, date formats and date functions in abundance. At some point it becomes inefficient not to read it.
> 
> I tend to type things like 
> 
> . di %tm 468
> 1999m1
> 
> which tells me that as a monthly date 468 means January 1999. As explained in the documentation, Stata always uses the first possible date in 1960 as 0. That also can be checked 
> 
> . di %tm 0
> 1960m1
> 
> Also, Stata won't know that 199901 means a date that is in January 1999 if you present that to Stata as a daily date, as you did. You need to convert such dates. For example, 
> 
> . di %td date("199901" + "1", "YMD")
> 01jan1999
> 
> or 
> 
> . di %tm ym(1999, 1)
> 1999m1
> 
> Steve Samuels and I just published a relevant Tip with various specific advice, so I won't expand further. 
> 
> SJ-12-1 dm0062  . . . . . . . . . Stata tip 105: Daily dates with missing days
>        . . . . . . . . . . . . . . . . . . . . .  S. J. Samuels and N. J. Cox
>        Q1/12   SJ 12(1):159--161                                (no commands)
>        presents strategies for dealing with daily dates where
>        some observations only provide the month and year
> 
> 
> Nick 
> [email protected] 
> 
> Rudy Hover
> 
> Thank you Nick. 
> The reason I want to change the data is that I need to merge it with a dataset that contains the dates in the '199901' format. 
> 
> Because of that just displaying the data in another way won't help me out in the end. 
> 
> I used your tip to generate a variable that is just year and month
> gen monthdate=mofd(date)
> 
> So now for instance 29jan1999 becomes 468 (do you know why 468?)
> 
> However if the underlying cell is in the format '199901' then the operator
> gen monthdate=mofd(date)
> creates a value for 199901 that equals 6567.
> 
> So then I have a mismatch, since both 29jan1999 and 199901 should be be replaced/used to create the same (new) value.
> 
> How can I let stata perform this?
> 
> On Apr 24, 2012, at 3:39 PM, Nick Cox wrote:
> 
>> You can apply a different -format- to get a display of just year and month. 
>> 
>> . di %tdCCyyNN mdy(4,24,2012)
>> 201204
>> 
>> You can get a date variable that is just year and month by using -mofd()- as in 
>> 
>> gen monthdate = mofd(date) 
>> 
>> . di %tm mofd(mdy(4,24,2012))
>> 2012m4
>> 
>> This is all documented. 
> 
> Rudy Hover
> 
>> The variable 'date' has type -long-.
>> 
>> The format is -%d-
>> 
>> The label is -Date of Observation-
> 
> On Apr 24, 2012, at 2:08 PM, Nick Cox wrote:
> 
>>> The word format means so many things! "29jan1999" is not a format in the sense of -format-. It might the result of setting a particular format? 
>>> 
>>> Is -date- a string variable? Is it a numeric variable with a date format assigned. 
>>> 
>>> Do you want another string variable? Do you want a numeric variable with a specified display -format-? 
> 
> Rudy Hover
> 
> I have one variable called 'date' with observations in the format '29jan1999' until '19dec2006', so the last working day of the month, the month and then the year. I want to change this to the format '199901' or '200612', so I don't need the day anymore, only the year and then the month. Could you help me how to replace?

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