This formula is from the SAS documentation for the FREQ procedure:
I didn't see mention of an adjusted or not adjusted residual, only this formula. You modification of the script is fine, but you can comment out the line with the adjusted formula. You are computing the residual twice.
What do you ;mean "there should be an error in the way pidot# and pdotj# are calculated?"
Why do you prefer the un-adjusted version?