Hi,

I am having trouble when Stata converts a variable from numeric to a string, as it appears to be incrementing the number by 1.

I have two variables that each hold dates. If one holds a date, the other is blank. Both date variables are numeric variables that are not yet converted to function as dates in Stata. I want a single variable that holds the date info from both variables, and use egen rowtotal. I then use tostring to convert the combined variable to a string so that I can use the Stata's date commands.

Code:
. desc EnrollmentEnd GraduationDate

Variable      Storage   Display    Value
    name         type    format    label      Variable label
-------------------------------------------------------------------------------------------------
EnrollmentEnd   long    %10.0g                Enrollment End
GraduationDate  long    %10.0g                Graduation Date

. egen date_grad_last=rowtotal(EnrollmentEnd GraduationDate),missing
(1,322 missing values generated)

. tostring date_grad_last, gen(stdate_grad_last)
stdate_grad_last generated as str8

. format %15.0g date_grad_last
Below are the inputs and output on a case level. The first and second columns are original variables that hold dates. The third column holds the combined variable (that results from egen rowtotal above), and the last column displays the results of the tostring command. Note that the last digit in the cases below change from "31" to "32" in case 2, and from "01" to "00" in case 14. These are problematic as these entries are dates and 00 and 32 generate errors

Code:
. list EnrollmentEnd GraduationDate date_grad_last stdate_grad_last in 2

     +-------------------------------------------+
     | Enroll~d   Gradua~e   date_g~t   stdate~t |
     |-------------------------------------------|
  2. |        .       20160531   20160532   20160532 |
     +-------------------------------------------+

     +-------------------------------------------+
     | Enroll~d      Gradua~e   date_g~t   stdate~t |
     |-------------------------------------------|
 14. | 20100601          .         20100600   20100600 |
     +-------------------------------------------+
I use the "date" command to convert the last column (string) above to a date variable. As you can see below, the conversion for these cases with 'out of bounds' days ("00" and "32"), and the result is a missing entry.

Code:
gen dtdate_grad_last= date(stdate_grad_last, "YMD")
format %td dtdate_grad_last

. list EnrollmentEnd GraduationDate date_grad_last stdate_grad_last dtdate_grad_last in 2

     +------------------------------------------------------+
     | Enroll~d   Gradua~e   date_g~t   stdate~t   dtdate~t |
     |------------------------------------------------------|
  2. |        .        20160531   20160532 20160532          . |
     +------------------------------------------------------+

. list EnrollmentEnd GraduationDate date_grad_last stdate_grad_last dtdate_grad_last in 14

     +------------------------------------------------------+
     | Enroll~d   Gradua~e   date_g~t   stdate~t   dtdate~t |
     |------------------------------------------------------|
 14. | 20100601          .      20100600   20100600          . |
     +------------------------------------------------------+
Is there a way to avoid the incrementing of number in conversion to string?

Thanks!