This can be a very frustrating situation! There are many possibilities. It could be your dog gets distracted when out in the yard and forgets about going only to remember when he comes back in. It could be that he is fearful of something outside or just insecure so he holds it until he's inside and feels safer. If you are using an invisible fence, that sometimes can trigger some dogs to avoid pooping outside because of negative associations with the yard. Sometimes, the behavior can be due to the dog feeling cold outside and not wanting to stay long in the yard. Dogs with low thyroid levels may resent being outside due to feeling more cold than usual so they may want to go back inside fast. If this is a new behavior, it may be worthy of telling the vet just in cases there's something medical going on.