Announcement

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

  • how to convert imported time variable to stata time variable

    I downloaded from a text file several variables one of which is a date variable.
    This is how it's organized but I want to transform this variable to STATA time variable because when I try to tsset I get the following msg:

    string variables not allowed in varlist;
    period is a string variable



    Attached Files

  • #2
    Before working with dates and times, any Stata user should thoroughly review the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF. After that, the help datetime documentation will usually be enough to point the way. 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


    • #3
      William gave the best advice on this in #2.

      Olivia Johns It strikes me as odd, to say the least, that you have posted around a five dozen messages, yet you seem to neglect an important request, presented in the FAQ, which is the recommendation to avoid using snapshots. Instead, there is the advice to share data by using CODE delimiters or installing the SSC dataex.

      When one refuses to act according to this very simple recommendation, one leaves the "burden" of (gently providing the solution) to the forum members with expertise and (lots of) goodwill.

      For example, precisely with regards to your query, I had to type the data before starting to spending time so as to approach the problem.

      This notwithstanding, I decided to take this extra step forward and create an example, taking a toy data quite similar to what we get by glimpsing at your snapshot.

      Indeed, I did it for 3 reasons:

      1) I do expect that you eventually act according to the FAQ recommendations in the forthcoming messages.

      2) I'm grateful for having had so many great lessons from fellows of this remarkable forum, and I wish to share what I've learned so far.

      3) Although the manual gives excellent instructions, dealing with date and time in several packages, at the introdutory level, can become a pain-staking task (at least, it was to me), hence I wish to cheer up users so as they (progressively) curb the issue.
      Below, the example:

      Code:
      . set obs 5
      number of observations (_N) was 0, now 5
      
      . input str10 mydate
      
               mydate
        1. "Jan 2017"
        2. "Feb 2017"
        3. "Jun 2017"
        4. "May 2017"
        5. "Apr 2017"
      
      . gen mydate2 = monthly(mydate, "MY")
      
      . list
      
           +--------------------+
           |   mydate   mydate2 |
           |--------------------|
        1. | Jan 2017       684 |
        2. | Feb 2017       685 |
        3. | Jun 2017       689 |
        4. | May 2017       688 |
        5. | Apr 2017       687 |
           +--------------------+
      
      . format mydate2 %tmMon_CCYY
      
      . de
      
      Contains data
        obs:             5                          
       vars:             2                          
       size:            70                          
      ---------------------------------------------------------------------------------------------------------------------------
                    storage   display    value
      variable name   type    format     label      variable label
      ---------------------------------------------------------------------------------------------------------------------------
      mydate          str10   %10s                  
      mydate2         float   %tm..                
      
      
      . list
      
           +---------------------+
           |   mydate    mydate2 |
           |---------------------|
        1. | Jan 2017   Jan 2017 |
        2. | Feb 2017   Feb 2017 |
        3. | Jun 2017   Jun 2017 |
        4. | May 2017   May 2017 |
        5. | Apr 2017   Apr 2017 |
           +---------------------+
      Hopefully that helps!
      Last edited by Marcos Almeida; 24 Jul 2017, 14:12.
      Best regards,

      Marcos

      Comment

      Working...
      X