Almost certainly the connection between the encoder and transponder.
Some time ago, I had a similar problem and found a poor connection in the plug at the transponder end and just a couple of weeks ago a friend had the same problem in his aircraft and found a wire in the plug no longer connecting properly.