MADRID - The Spanish economy shrank for the first time in two years in the fourth quarter, the start of what economists fear could be a prolonged slump as Madrid implements harsh austerity measures to deflate a massive budget deficit.

Gross domestic product shrank by 0.3 percent in the fourth quarter on a quarterly basis, as forecast in a flash estimate, and after stagnating in the third quarter, final official data showed on Thursday.

A test of the country's attraction to investors comes later on Thursday with a bond sale, including one for seven years. There has been healthy demand recently, boosted by cheap loans to banks and others from the European Central Bank.

0n an annual basis the economy grew by 0.3 percent in the fourth quarter, in line with Reuters forecasts consensus and compared to 0.8 percent in the third quarter.

Some countries in the euro zone may just avoid a recession, but that may be more difficult for Spain. Given the need for fiscal consolidation in the country and the pressure that puts on domestic demand, it's going to be very difficult for Spain to avoid recession, RBS economist Nick Matthews said.

Economic output in the 17-nation currency area fell 0.3 percent in the fourth quarter from the third, official data showed on Wednesday as the sovereign debt crisis crushed a recovery and looked set to push the bloc in to a mild recession.

The Italian economy joined Belgium, Greece and Portugal in formal recession having already shrunk in the third quarter of 2011, the data on Wednesday showed.

Purchasing data from Markit for January showed a slight improvement for the Spanish manufacturing and services industries, but it may not be enough for an economy that has been in recession or close to stagnation for almost five years.

Spain had been growing at an above-average rate since the country entered in to the euro zone monetary union 12 years ago, but the boom was largely due to the housing expansion fuelled by cheap loans and has been struggling since the 2007 crash.

In the fourth quarter, exports were the only sector to show growth with industry surviving solely because of demand outside of Spanish shores, though even that is slowing as the economies of Spain's main trading partners stumble.

Meanwhile, Spain's new government is fighting to reduce a budget deficit it has estimated at 8 percent of GDP in 2011 to a target of 4.4 percent this year, implying necessary savings of an estimated around 45 billion euros.

The International Monetary Fund believes GDP will contract by 1.7 percent in 2012 and the economy will remain weak through next year, weighed down by the government's aggressive austerity measures.

On an annual basis the economy grew by 0.3 percent in the fourth quarter, in line with consensus and compared to 0.8 percent in the third quarter.

In 2011, the economy grew by 0.7 percent compared to a fall of 0.1 percent in 2010.

(Reporting by Paul Day)