Announcement

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

  • Constant date and month, changing year variable creation

    Dear all. I am trying to create two date variable whose day and month components will be constant, and the year will be different for each observation depending on the year listed in another 2 variables called start_year and end_year . Specifically, I want to create two variables fix_dec which should be of the format 31dec (start_year) and fix_jan will be of the format 01jan(end_year). I tried working with the code below, but I am not getting any results. Thanks again!
    . gen fix_dec = mdy(31, 12, start_year, "MDY") invalid syntax r(198); . dataex start_year end_year ----------------------- copy starting from the next line -----------------------
    Code:
     * Example generated by -dataex-. To install: ssc install dataex clear input float(start_year end_year) 1972 1990 1975 1987 1975 1976 1975 1983 1977 1999 end

  • #2
    With a function you can get help directly by (in this case)


    Code:
    help mdy() 
    which shows you that the three (and only three) arguments expected are month, day, year in that order -- hence the name of the function.


    Code:
    mdy(12, 31, start_year)
    should work here. Note that your own guessed syntax is inconsistent as well as wrong, as you are explaining 31 as the month.

    Guessing at what the syntax might be is a poor tactic even for the experienced user.

    Comment


    • #3
      Hi Nick, thanks for your response. I now see that I was replacing the date and month in reverse order, however, using the command
      gen fix_dec= mdy(31,12, start_year) yields bizzare results that do not correspond to the dates. This is an example of what I am getting now:


      . dataex start_year fix_dec

      [CODE]
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(start_year fix_dec)
      1975 5843
      1975 5843
      1975 5843
      1975 5843
      1975 5843
      1975 5843
      1975 5843
      1975 5843
      1975 5843
      1975 5843
      1976 6209
      1976 6209
      1976 6209
      1976 6209
      1976 6209
      1976 6209
      --more--
      What do you believe is happening here, and how could I fix it? Thank you so much for your time and support.

      Comment


      • #4
        Nevermind, I just figured it out. I missed the formatting step, but now it works perfectly. Thank you so so much again!
        Last edited by Eleni Moschou; 20 Dec 2021, 08:16.

        Comment


        • #5
          5843 is the right answer for 31 Dec 1975, but it would not be produced by mdy(31,12, 1975) -- which just yields missing, and not any kind of date at all.

          Comment

          Working...
          X