Announcement

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

  • Variables with date

    Hi everybody,

    I want to change the format and modify my variables

    I have my variable A : (11/09/2000 , 10/03/2014, 20/03/2000)
    I have my variable B : (15/10/2015, 15/02/2020, 24/12/21998)

    I want to create C: is B-A the difference between B and A

    Thank you,

  • #2
    Have you read the help files and Manual entries regarding date functions and formatting of date variables? Help us to (want to) help you by telling us what you've looked at before posting -- as advised in the FAQ.
    Related: showing us the output from a brief list and describe of the relevant variables might also help. (Post the output using the CODE delimiters accessible via the advanced editor -- use underlined-A button.) How are we supposed to know what your data structure is?
    Finally, you were politely reminded by Nick Cox yesterday about the forum's preference for members to use their real name (firstname lastname). Please re-register: it's easy. Make a request via the Contact Us button at bottom right of screen.

    Comment


    • #3
      Are A and B numeric or string?

      Please do register with a full real name. Many experienced users are reluctant to support people who don't respect local etiquette.

      Comment


      • #4
        Code:
        gen Anum=date(A,"DMY")
        gen Bnum=date(B,"DMY")
        gen ABdiff=Bnum-Anum
        will give you the difference in days.

        Comment


        • #5
          BTW -- my code was written assuming the variables are strings to begin with. Depending on formatting, they may already be numeric and just display as dates, in which case, ignore what I wrote. Also, those Anum and Bnum variables are the number of days elapsed since January 1, 1960, as you might find from searching on the term "date".

          Comment


          • #6
            Originally posted by malik View Post
            I have my variable A : (11/09/2000 , 10/03/2014, 20/03/2000)
            I have my variable B : (15/10/2015, 15/02/2020, 24/12/21998)
            My guess is that you can simply follow this FAQ on calculating the days between the two dates. Naturally, if your variables are in a string format you will have to convert them to dates.So your code would look like that, more or less.

            Code:
            gen date_a = date(A, "DMY") format date_a %td
            gen date_b = date(B, "DMY")
            format date_b %td gen between = date_b - date_a 
            Kind regards,
            Konrad
            Version: Stata/IC 13.1

            Comment


            • #7
              For just the difference between dates held as strings,

              Code:
               
              gen diff = date(A, "DMY") - date(B, "DMY")
              shows the spirit of one calculation.

              Comment


              • #8
                Hmmm. A bit off-track, but I've been wondering (for years) if:
                To simplify or not to simplify, that is the question.
                Whether 'tis Nobler in the mind to offer

                the simplistic, bite-size approach,
                Or to try to teach the complex, shorter, but less intuitive syntax,
                And by giving complex syntax, confuse users? To loop, to macro —
                to make users see the capabilities of Stata; and by seeing the capabilities, I mean cargo-cult programming,
                It's often more efficient to do things one way, but when I show people tight but complex syntax, I often come back weeks later to find they've copied and pasted their way into trouble, not understanding what each line of code does, blindly confusing commands and options with variable names and/or macro names. Bite-size, so they can see what *each* line does seems generally to be safer. If I need to invoke a macro or loop, I'll often name it "Ben" or name it their own name so it's obvious what's going on. Reifying "i" "j" and "k" is all too common in my experience. Hmm...


                Comment

                Working...
                X