The dissociative photoionization of N2 and O2 by synchrotron radiation in coincidence with threshold photoelectrons is used to produce state-selected N+ and O+ atomic ions to study their reactivity. A pure selection of their ground state, N+(3P) and O+(4S), or excited states, N+(1D), O+(2D), and O+(2P), is obtained by the choice of the photon energy and by further discrimination of atomic ions produced with translational recoil energy. Both reactions studied, 15N+ + CD4 and O+ + 13CO2, are of major importance for the chemistry of Titan, Mars, and Venus' ionospheres and are strongly affected by excitation of the parent atomic ion. For the reaction of N+ with methane, DCN+ and DCND+ products coming from the decomposition of a long-lived complex are surprisingly not much sensitive to the N+ excitation, whereas the branching ratio between the dissociative charge-transfer channel, leading to CD3+, which is the main product for the ground-state reaction, and the nondissociative charge-transfer channel, leading ...