How should I approach the issue of students not writing their name and student number on an exam?
Y'all have never proctored or taken SATs?

Give only the answer sheets out first, and have students complete the important information before they even get the questions.

If you don't give the questions until students have filled out the relevant information on the answer sheet, they won't be able to ignore the answer sheets, in lieu of answering questions.

It may require a bit of a rethink as to the time available for the test (i.e. take five minutes away from testing and put it onto "administrivia"), but this might just be time well spent.

You could even incentivize students to arrive early to complete this task quickly by saying that the question packs will be given out as soon as all students have filled out their information; any student who shows up to class (up to five minutes) early will be given their answer sheet to fill out.  If all students have their answer sheets filled out by the time "##:##", the tests will be handed out then.  Anyone who shows up late will have to complete their information on the answer sheet at the front of the room before being given the questions.

Or, have part of the ID process be that students have to show you their IDs AND a completed information section on their answer sheet before they get the questions (or, maybe before they take their seats).

If it's an important step in the process, make sure it has a defined step in the process.

To forestall the comments I know are coming: Yeah, I know: Young adults should be able to follow directions.  OTOH, negative reinforcement doesn't have the learning results you think it does. Don't penalize students for a bad process.