T his paper considers mathematical optimization for the multistage train formation problem, which at the core is the allocation of classification yard formation tracks to outbound freight trains, subject to realistic constraints on train scheduling, arrival and departure timeliness, and track capacity. The problem formulation allows the temporary storage of freight cars on a dedicated mixed-usage track. This real-world practice increases the capacity of the yard, measured in the number of simultaneous trains that can be successfully handled. Two optimization models are proposed and evaluated for the multistage train formation problem. The first one is a column-based integer programming model, which is solved using branch and price. The second model is a simplified reformulation of the first model as an arc-indexed integer linear program, which has the same linear programming relaxation as the first model. Both models are adapted for rolling horizon planning and evaluated on a five-month historical data set from the largest freight yard in Scandinavia. From this data set, 784 instances of different types and lengths, spanning from two to five days, were created. In contrast to earlier approaches, all instances could be solved to optimality using the two models. In the experiments, the arc-indexed model proved optimality on average twice as fast as the column-based model for the independent instances, and three times faster for the rolling horizon instances. For the arc-indexed model, the average solution time for a reasonably sized planning horizon of three days was 16 seconds. Regardless of size, no instance took longer than eight minutes to be solved. The results indicate that optimization approaches are suitable alternatives for scheduling and track allocation at classification yards.