Announcement

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

  • Recode function not working correctly

    Hi all,

    I have the below code:

    Code:
    recode DateDiff (7/13=1 "Returned after 7 days")(14/27=2 "Returned after 14 days")(28/max=3 "Returned after 28 days"), generate(ReturnDates)
    With the intention that the difference between two dates is recoded into one of the above three categories. However, as you can see in the attached screenshot this is working perfectly for everything except row 2 (which should be 'Returned after 7 days", but instead is showing as "Returned after 14 days"). I cannot figure out what's happening here. Can anyone else?!

    In case it's helpful, the code I've used for DateDiff is:

    Code:
    gen DateDiff = LastSeen2 - OnboardingDate2
    Thank you!
    Attached Files

  • #2
    Hi Andrew,

    to me, it looks as if the code worked as intended; you did not specify a recode instruction for the range from 0 through 6 --- thus, Stata just copies values in that range to the new variable. Observation 2 receives the original value 2 in the same way, as observation 4 receives the value 0.

    I suggest to include a recode instruction like
    Code:
    (nonmissing=-1 "other")
    into your recode-statement in order to prevent this. See the help recode for details and example code that illustrates this behavior.

    Regards
    Bela
    Last edited by Daniel Bela; 22 Jun 2018, 07:02. Reason: added link to help page

    Comment


    • #3
      Hi Bela,

      That was such a silly mistake to make! Thank you so much for spotting it. I've now fixed the code.

      Thanks!

      Comment

      Working...
      X