Not sure if it matters, but in the UI you can only cancel a Sales Order if the order has not been approved. If it has been approved, you can only close it. So, I would imagine you do it the same way via scripting, loop through the items and set close to true for each item.