Announcement

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

  • Adjusting Date Format

    Hello,

    Could anyone help with a command that takes my year variable in the format "xxxx-xx" and changes it into the format of "xxxx-xxxx". For example if I have "2016-17" I would want to change it to "2016-2017". Thank you.

  • #2
    You haven't said what is the earliest case you have. This assumes that you may have some 20th century examples.

    Code:
    clear 
    input str7 twoyear 
    "1998-99"
    "1999-00"
    "2001-02"
    "2022-23"
    end 
    
    gen wanted = subinstr(twoyear, "-", "-19", 1) if real(substr(twoyear, 1, 4)) <= 1998 
    replace wanted = subinstr(twoyear, "-", "-20", 1) if real(substr(twoyear, 1, 4)) >= 1999
    
    list 
    
         +---------------------+
         | twoyear      wanted |
         |---------------------|
      1. | 1998-99   1998-1999 |
      2. | 1999-00   1999-2000 |
      3. | 2001-02   2001-2002 |
      4. | 2022-23   2022-2023 |
         +---------------------+
    If all the years are from this century then

    Code:
    gen wanted = subinstr(twoyear, "-", "-20", 1)
    should suffice.

    For most purposes I would want to hold the years as single integers and use value labels for display.

    Comment

    Working...
    X