Announcement

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

  • Split a numeric variable

    Hi,

    I have a variable called "bslstart" in which the format is:
    01 Jan 16
    01 Dec 15
    01 Sep 14


    I would like to extract the last two numbers in the variable to get a format that display the year in a four-digit format. Any help to perform that command would be very appreciated

    Thanks,
    Sebastian

  • #2
    Please note https://www.statalist.org/forums/help#realnames where we request the use of given names and family names and how to fix that. "The Seal" clearly doesn't qualify.

    Your question is ambiguous, as format in Stata's sense of the word, shown by

    Code:
    describe bslstart
    is not specified.

    Please note https://www.statalist.org/forums/help#stata as indicating better ways to show your data. We don't want to have to guess whether you are showing a string variable, a numeric date variable, or a numeric variable with value labels, as the answer differs in each case.
    Last edited by Nick Cox; 20 Feb 2019, 08:08.

    Comment


    • #3
      Hi Nick,

      Sorry for that, I will try to change my username to my real name.

      Also apologies for not being more specific in my question. The data I´m working with is described below:

      storage display value
      variable name type format label variable label

      bslstart long %tdD_m_Y Bokslutsstart

      Again, many thanks!

      Comment


      • #4
        So, it's a numeric daily date variable. It follows from

        Code:
        help datetime
        -- which you should surely study -- that

        Code:
        gen year = year(bslstart)
        will give the year.

        Comment


        • #5
          Adding to Nick's response, if what you wanted was to display
          Code:
          01 Jan 16
          as
          Code:
          01 Jan 2016
          then
          Code:
          format bslstart %tdDD_Mon_CCYY
          should do the trick.
          Code:
          . clear
          
          . set obs 1
          number of observations (_N) was 0, now 1
          
          . generate d = td(1jan2016)
          
          . format d %tdD_m_Y
          
          . list, clean noobs
          
                      d  
              01 Jan 16  
          
          . format d %tdDD_Mon_CCYY
          
          . list, clean noobs
          
                        d  
              01 Jan 2016
          Stata's "date and time" 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

          Working...
          X