You can apply up to 90 days before you plan to enter Spain. It’s advisable to have an appointment with the Consulate at least 30 days in advance because regional authorities need to be consulted about the place where the studies are going to be carried out.