The idiomatic expression "to keep someone sane" means to maintain a person's mental stability, prevent them from going insane or losing their mind. Full definition
They'll also help keep you sane in the process. (rookiemoms.com)
We help address the daily joys and struggles of finding a balance between kids and parents while keeping ourselves sane in the process. (goodlifefamilymag.com)
Because, music not only keeps me sane while driving, but also while working around the house. (littletechgirl.com)