The Dow and the S&P ended up slightly on Friday, with data showing the economy shrank less than expected in the second quarter, lifting investor optimism. The Nasdaq ended down slightly.

For the month, the Dow was up 8.6 percent, its best gain for July since 1989. The S&P was up 7.4 percent and the Nasdaq was up 7.8 percent.

For the week, the Dow was up 0.9 percent, the S&P was up 0.8 percent and the Nasdaq was up 0.6 percent.

For the day, the Dow Jones industrial average <.DJI> unofficially ended up 9.60 points, or 0.10 percent, at 9,164.06. The Standard & Poor's 500 Index <.SPX> was up 0.25 points, or 0.03 percent, at 987.00. The Nasdaq Composite Index <.IXIC> was down 5.80 points, or 0.29 percent, at 1,978.50.

(Editing by Kenneth Barry)