Yes, in the United States, driving while impaired by certain prescription or over-the-counter medications can result in legal consequences.
Having a valid prescription does not automatically make it safe or lawful to drive when a medication has impaired your ability to operate a vehicle.
Specific laws and enforcement procedures vary by state.
The important distinction is between simply having a medication in your system and actually being impaired while driving.