Announcement

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

  • Change last 4 digits in String type


    How can I change last 4 digits of co_end_variable to finyear ?

    For the data below, for example, 01jan2001-> 01jan1998 and 28oct2003 -> 28oct2002



    . dataex co_end_date finyear

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str9 co_end_date str4 finyear
    "01jan2001" "1998"
    "28oct2003" "2002"
    "31dec1998" "1993"
    "01nov2001" "1999"
    "01mar2001" "1993"
    "28dec2003" "2002"
    "31dec1993" "1993"
    "01mar1998" "1994"
    "01jan2023" "2000"
    "01nov1993" "1992"
    "01dec2000" "2000"
    "01oct1994" "1992"
    "23apr2001" "1998"
    "31oct1995" "1994"
    "31dec1994" "1994"
    "01jan2023" "1996"
    "01apr1994" "1993"
    "27jul1998" "1993"
    "01jul2001" "2000"
    "01aug1994" "1994"
    "12may1996" "1993"
    "01apr2000" "1992"
    "25oct2002" "1998"
    "31dec1994" "1994"
    "31dec1994" "1994"
    "01jul2002" "1996"
    "06may1999" "1999"
    "31dec1993" "1993"
    "27jan2004" "2004"
    "15mar1996" "1993"
    "03may1995" "1993"
    "31dec1994" "1992"
    "31mar1993" "1993"
    "28apr1993" "1993"
    "01feb1993" "1993"
    "01apr1997" "1994"
    "01may2018" "1995"
    "01may2001" "2001"
    "14mar2005" "1998"
    "01oct1995" "1995"
    "01jan2023" "1992"
    "01jan2023" "1992"
    "08jun2020" "2019"
    "31oct1997" "1994"
    "01jan1998" "1995"
    "31may1994" "1993"
    "31dec2000" "1992"
    "10may2000" "1997"
    "01jan2023" "1992"
    "31dec1998" "1998"
    "31aug1996" "1994"
    "01dec2003" "2001"
    "02nov1996" "1996"
    "30jun2002" "1999"
    "01aug2008" "2001"
    "15jan2009" "2000"
    "14oct1993" "1992"
    "01apr2003" "2000"
    "17apr1997" "1995"
    "01jan2023" "1994"
    "01jan2023" "1992"
    "30apr1994" "1992"
    "01jan2023" "1992"
    "01sep2002" "1993"
    "15jul2003" "2001"
    "01may1998" "1997"
    "31dec2006" "2006"
    "01apr1999" "1997"
    "01oct1996" "1996"
    "02nov1995" "1995"
    "01feb1998" "1994"
    "01jan2023" "1992"
    "30jun1995" "1992"
    "01jan2023" "1995"
    "01aug2006" "2002"
    "17feb2009" "1997"
    "06sep2011" "2010"
    "01aug1996" "1993"
    "18mar1997" "1997"
    "01may2000" "1997"
    "26jun1998" "1997"
    "06may1999" "1996"
    "01jan1997" "1993"
    "17jun1994" "1993"
    "21dec1999" "1999"
    "01aug1994" "1993"
    "01jan2023" "1995"
    "27jul1995" "1995"
    "07feb1998" "1992"
    "31dec1995" "1995"
    "30apr1995" "1992"
    "01oct1993" "1992"
    "01jan2023" "1996"
    "01jan2023" "1998"
    "28feb2006" "2004"
    "28feb1997" "1995"
    "01jan2000" "1997"
    "31dec1997" "1997"
    "31dec2008" "2004"
    "08jan2008" "2003"
    end
    ------------------ copy up to and including the previous line ------------------

    Listed 100 out of 24526 observations
    Use the count() option to list more

    .

    Last edited by BB Kim; 04 Mar 2023, 18:54.

  • #2
    Code:
    replace co_end_date = substr(co_end_date, 1, 5) + finyear
    That said, I wouldn't do that. Neither co_end_date nor finyear is really useful as a string date. I would convert both of them to numeric variables and then do a substitution of the year using some datetime functions:

    Code:
    destring finyear, replace
    
    gen _co_end_date = daily(co_end_date, "DMY"), after(co_end_date)
    assert missing(_co_end_date) == missing(co_end_date)
    format _co_end_date %td
    drop co_end_date
    rename _co_end_date co_end_date
    
    replace co_end_date = mdy(month(co_end_date), day(co_end_date), finyear)
    Yes, this code is lengthier and a bit harder to understand. But it converts a nearly useless variable, co_end_date, and a minimally useful variable, finyear, into variables that you can use for calculations.

    Comment

    Working...
    X