Announcement

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

  • Count the number of years

    Hi,

    I have a question of calculating the number of years in Stata.

    In the data, each ID has an Issue_Date in form '25 Jun 05' or '01 Aug 85'. The baseline date is 201509, and I would like to calculate the years each ID was in business.

    For instance, if the Issue_Date is '04 May 96', the years of business would be 13 years.

    I think I would have to change the format of the date then do the calculation...

    It would be really helpful if I can get advice! Thank you very much.

  • #2
    Please give an example using dataex. There is still scope for guessing whether you are talking about numeric or string variables here, or even worse dates that are value labels.

    Note that display format will not be the issue, but just possibly variable or storage type, something quite different.

    Comment


    • #3
      I'm sorry for the confusion.

      Issue_Date is a long variable in form "day month year" such as "05 May 85".

      I have also a month variable which is a numeric variable in form "201509".

      I would like to calculate the number of years each ID was in business, having the baseline as 201509. For instance, if the Issue_Date is "05 May 85", I would like to subtract from 201509 and calculate the years. (2015 - 1985 + 1 = 31 years).

      Thank you very much!

      Comment


      • #4
        What you say is contradictory. A variable of type long cannot have string values. Perhaps you mean that it has a particular display format. Perhaps you mean that it has value labels. Guessing what you mean is not fun.

        This is exactly why we ask: Please give an example using dataex. To quote from the FAQ Advice, and adding emphasis:


        The merits of dataex are that we see your data as you do in your Stata. We see whether variables are numeric or string, whether you have value labels defined and what is a consequence of a particular display format. This is especially important if you have date variables. We can copy and paste easily into our own Stata to work with your data.

        Comment


        • #5
          This is the what I get from

          Code:
          codebook First_Issue_Date
          Due to server issue, I cannot directly post using dataex, thank you!

          Click image for larger version

Name:	df.png
Views:	1
Size:	10.3 KB
ID:	1563800

          Comment


          • #6
            The format of First_Issue_Date is %tdD_m_Y and I would like to just extract the year...

            Comment


            • #7
              Try

              gen year = year(First_Issue_Date)


              hth,
              Jeph

              Comment


              • #8
                Alternatively: Your variable month is not a standard monthly date variable. To make it comparable with a daily date variable you need some version such as


                Code:
                gen betterdate = mdy(mod(month, 100), 15, floor(month/100))
                -- here using the 15th of the month as a convention to get a daily date, You may have a better convention, e.g. the first of the month (easy) or the last of the month (harder but entirely soluble).

                Once you have two daily dates -- the other being First_Issue_Date according to #5 and Issue_Date according to #3 and #1 -- the problem is then just subtracting one from another.


                Comment


                • #9
                  Thank you very much!!

                  Both methods work very well!

                  Comment

                  Working...
                  X