You do it because you are scared, your brain is a primal state of being during a panic attack and all it cares about is stopping the panic and perceived danger around it so you do what you know works. It's a very basic and primal reaction!
Do you have any coping skills to use during panic attacks such as grounding or breathing exercises? Nothing will calm you down as fast as medication but sometimes taking the stress down from a 10 to a 9 can be all you need to make it through the panic attack without slipping up. One thing that works for me is going through my senses and saying things I see, smell, hear, feel etc. For example "the wall is white, the floor is cold, i hear a fan etc." It has really helped me ground myself in panic attacks.
I know where you've been though. I used to be addicted to Klonopin and Xanax and would over medicate all the time. It's such such a hard thing to overcome but I know you can do it! I use weed fairly often now and if that's legal in your state I really recommend giving it a try! It chills you out without all the nasty side effects.
Best of luck to you!