We have measured the production of π+ and π− in 200-, 300-, and 400-GeV p−p and 400-GeV p−d collisions for transverse momenta (p⊥) ranging from 0.77 to 7.67 GeV/c. At large values of x⊥=2p⊥s, where s is the c.m. energy, we have fitted the p−p data to the form A(1−x⊥)bp⊥−n; we obtain n=8.2±0.5 for π+ and 8.5±0.5 for π−. At x⊥>0.3 the π+π− ratio in p−p collisions rises appreciably with increasing x⊥ whereas the π+π− ratio obtained from the difference of p−d and p−p cross sections is ∼ 1.0 at all x⊥.