Announcement

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

  • Adjust date variable MMJJJJ to JJJJ

    Hi,
    I have a variable "date" in the following form: MMJJJJ or MJJJJ (so I sometimes have 5, sometimes 6 digits).
    Now I want to generate a variable in the following form: JJJJ. So I want to drop the first digit if the date has the form: MJJJJ; or I want to drop the first two digits if it is denoted MMJJJJ:
    Alternatively, I want to keep the last four digits in each observation.
    How do I implement that?
    Looking forward to your responses ;-)

  • #2
    J presumably means "year" in your first language. Stata documentation and functions all use Y for year.

    As you've not explained whether you have numeric or string date variables, specific advice depends on that detail.

    Please read the FAQ Advice to see an explanation of our strong reference for full real names e.g. Angela Merkel.

    Comment


    • #3
      ok thanks,
      the type is long

      Comment


      • #4
        Originally posted by BurghauserBua View Post
        the type is long
        In that case,
        gen year = datevar - (floor(datevar/10000) * 10000)
        will retain (up to) the last 4 digits.

        Comment


        • #5
          You could also use the mod() function:
          Code:
          gen year = mod(datevar,10000)

          Comment


          • #6
            That's exactly what I wanted! Thanks a lot to both of you ;-)

            Comment

            Working...
            X