Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Database Management

    Hey all!
    I have a problem in db management. In particular, my variable "date" has format %9s and type str8 and I want to create a variable in format %tm.

    I tried to use "gen dates = monthly(date,"DMY") " - and "gen dates = date(date,"DMY")" - to create a new variable from the first one but Stata has created all missing values
    (screenshot in attachments)

    The final target is to create a time series of dates. This would allow me to merge different datasets (monthly and quarterly) by connecting dates' values.
    ty

    Michele
    Attached Files

  • #2
    Please do not post screenshots. Post data examples. This advice and why we ask that are explained in the FAQ you were asked to read before posting. https://www.statalist.org/forums/help#stata

    The same document also explains that precise and informative titles are preferable.

    You have various problems. First, monthly() just accepts second arguments YM or MY. This is explained in the help for monthly().

    Second, your dates are clear to people but not to Stata without specifying a century end.

    This shows some technique. There are other ways to do it, unsurprisingly.

    Code:
    clear
    input str8 date
    "01/01/76"
    end
    
    gen mdate = mofd(daily(date, "DMY", 2050))
    format mdate %tm
    
    list
    
         +-------------------+
         |     date    mdate |
         |-------------------|
      1. | 01/01/76   1976m1 |
         +-------------------+

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Please do not post screenshots. Post data examples. This advice and why we ask that are explained in the FAQ you were asked to read before posting. https://www.statalist.org/forums/help#stata

      The same document also explains that precise and informative titles are preferable.

      You have various problems. First, monthly() just accepts second arguments YM or MY. This is explained in the help for monthly().

      Second, your dates are clear to people but not to Stata without specifying a century end.

      This shows some technique. There are other ways to do it, unsurprisingly.

      Code:
      clear
      input str8 date
      "01/01/76"
      end
      
      gen mdate = mofd(daily(date, "DMY", 2050))
      format mdate %tm
      
      list
      
      +-------------------+
      | date mdate |
      |-------------------|
      1. | 01/01/76 1976m1 |
      +-------------------+
      Michele, to elaborate on this:

      Your dates are written like people would write them. Stata does not understand that format directly, for good reason (e.g. in the US, we put the month first, but most countries put the day first).

      A "real" Stata date is a raw integers that represent the number of days since January 1, 1960 (for daily dates). You can impose a format on the date, and if you do, you will see it formatted like 1/1/1976 when you look at your data. If you don't, you'll just see a big number.

      You can tell Stata to take a text date and convert it to a "real" date, but you need to tell it how the text is formatted. For example, if that first "country" were the US state of Alaska and your data were from a US source, then it would probably be "MDY".
      Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

      When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

      Comment


      • #4
        Welcome to Statalist, Michele.

        Nick gives good advice on using the FAQ to improve the presentation of your questions in the future, and both Nick and Weiwen give good advice on working with dates.

        Stata's "datetime" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

        All Stata manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

        Comment


        • #5
          Thank you all for advices

          I solve my problems.

          Best regards

          Comment

          Working...
          X