Announcement

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

  • [panal date] year variables settings

    hi, I am new to use STATA and I am seeing this forum a lot to learn the things so I am thankful to all the users here.
    and that's how and why I came to ask a question here.

    I am using panal data, and the time variable is 'year' from 1995-2014.
    and there is a variable, 'when you started this job' it is year/month (like 2008m3). and when I take a look at it, the variable type is 'int'
    and I wanna keep this variable just with years, so that I can count the 'lenghs of service'
    (for example, if one panel started to work at '1992m5', I wanna make a new variable 'lenghs of service' that is 4-24.

    so I tried like 'generate LS = year-when you started this job+1, but it isn't working.
    so, I think I should keep that 'year/month' variable as just the number of years.

    I would be so thankful so get some helps or advices, thanks in advance.

  • #2
    Code:
    gen length_of_service = year - yofd(dofm(when_you_started_this_job)) + 1
    Stata date and datetime variables are fairly complicated. But if you will be using Stata on an ongoing basis, learning about them is essential. Read the chapter in the PDF manuals that are part of your Stata installation. Run the command -help datetime- and then click on the blue link "(View complete PDF manual entry)." It's a long read, and you won't remember all the details. But you will come away from it with a general understanding of the different types of date and datetime variables, the various functions that create them and that transform one type to another, or extract parts from them. With that understanding in place, you will be able to function more independently, consulting the excellent help files for the details as needed. The time you invest in doing this will soon be amply repaid.

    In this instance, it was possible to make a reasonable guess at the solution from the information you supplied. But, in general, whenever you want help with code, it is wise to provide example data from your Stata data set. And the helpful way to do that is by using the -dataex- command. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      sorry for late reply, and thanks a lot for your help and advices, sir!
      I will defenitely check the manuals, and also I just ordered some books to learn it better.
      also, your code works perfectly, so again, I appreciate it!

      Comment

      Working...
      X