Stock MAME might not do cheats (and even sometimes struggles with modified images) but there are forks, and while I can't speak to MAME then for many other emulators (and PC games in general) external cheat programs are a popular thing with a long history here. Emuhaste, artmoney, cheat engine being the more noted of these but many hex editors will attach to memory, and Scanmem, GameConqueror, Bit Slicer, iHaxGamez and Squalr form many others. You could even program your own fairly easily (it is arguably what many PC trainers are).
Can get tricky when MAME updates versions and because compilers the location of the emulated system's memory changes but they are usually easy enough to find (assuming there are no simple tells then a savestate to compare to a hex editor memory dump of the whole program will usually do the job) so in turn you can define an offset instead (and maybe a pointer read in case it is one of those weird emulators that dynamically assigns memory locations).