It's rare for young babies to get pinworms, but it can happen, especially if your baby goes to daycare or if his siblings have pinworms. Your baby probably swallowed pinworm eggs after he got them on his hands — possibly from touching a toy handled by a child with pinworms — and then put his fingers in his mouth. (The eggs can survive for up to two weeks on some surfaces.)
The eggs then traveled to his large intestine, where they hatched. From there, female pinworms migrated out of your child's intestines to lay their eggs on his anus. As the worms move, they cause intense itching which can disrupt sleep.
When your baby scratches his itchy bottom, the eggs get under his fingernails, and another cycle begins when he puts his hands in his mouth.