In memory of Raquel Maritza Gilbert beloved wife of the second author.Abstract. Let S be a closed Shimura variety uniformized by the complex n-ball. The Hodge conjecture predicts that every Hodge class in H 2k (S, Q), k = 0, . . . , n, is algebraic. We show that this holds for all degrees k away from the neighborhood ]n/3, 2n/3[ of the middle degree. We also prove the Tate conjecture for the same degrees as the Hodge conjecture and the generalized form of the Hodge conjecture in degrees away from an interval (depending on the codimension c of the subvariety) centered at the middle dimension of S. Finally we extend most of these results to Shimura varieties associated to unitary groups of any signature. The proofs make use of the recent endoscopic classification of automorphic representations of classical groups by [1,58]. As such our results are conditional on the stabilization of the trace formula for the (disconnected) groups GL(N ) ⋊ θ associated to base change. At present the stabilization of the trace formula has been proved only for the case of connected groups. But the extension needed is now announced, see §1.13 for more details.