The outgoing permanent secretary of the Scottish Government has blamed a lack of financial sustainability on economic “shocks” he branded “unprecedented”.