The U.S. foreign trade deficit grew to a record $39.5 billion between April and June, the government said yesterday, but officials and analysts said factors affecting prices masked improvement.

The second-quarter deficit followed the previous record $38.8 billion deficit between January and March, which the Commerce Department revised upward from $38.3 billion.

Yesterday's report covered monthly figures on merchandise trade already released but presented the numbers on a balance-of-payments basis, which excludes military sales and the costs of insurance and shipping.

If the deficit continued for the rest of the year at the pace of the first six months, it would reach $156.6 billion on this basis, compared with $144.3 billion in 1986, the department said.

But government officials and analysts said the dollar's decline was turning around the trade shortfall in volume terms, which eventually would show up in a smaller deficit in dollars.

"The trend is clearly for improvement in the trade deficit in real {inflation-adjusted} terms," Commerce Undersecretary for Economic Affairs Robert Ortner said.

In yesterday's report, export volume increased twice as quickly as the volume of imports.

Ortner said the deficit would improve with the dollar at current levels and predicted a smaller deficit in the current, July-September quarter.

"Even at this level of the dollar, we will continue to see improvement," he said.

The 50 percent drop in the dollar against the German mark and Japanese yen over the past couple of years has made U.S. exports cheaper abroad. At the same time, prices of imports bought by Americans have gone up.

"Import prices are rising sharply in response to the lower dollar, but higher prices keep the {trade} numbers from improving," said C. Fred Bergsten, who heads the private Institute for International Economics. "In a few quarters, the value of imports will also start to go down."

Bergsten said adjusting for prices and adding trade in services, the deficit fell by $40 billion at an annual rate since the last year's third quarter.

Still, the report of another record deficit could make it more difficult for the administration to convince Congress to refrain from passing legislation to limit imports when lawmakers return Sept. 9.

Bergsten said he believed the dollar would have to fall further for the deficit to continue to improve.

But administration and U.S. monetary officials, possibly worrying about a surge in inflation, say they do not want the dollar to go lower.

Federal Reserve Board Governor Robert Heller, Austria for a conference, said he preferred a stronger dollar. "The basic confidence of domestic and foreign investors has to be preserved," Heller said.

His remarks followed comments Tuesday by U.S. Trade Representative Clayton Yeutter that a weaker U.S. currency was not the complete solution to America's trade deficit.

Yesterday's balance-of-payments report showed second quarter imports of $99.5 billion, up 4 percent, while exports rose 5 percent to $60 billion. Both the volume and prices of imports rose 2 percent, but export volume rose 4 percent and prices only 1 percent.

The deficit with Western Europe rose by $1 billion, as it did with the newly industrialized Asian nations the department said.

The deficit with Japan gained $100 million during the quarter while falling to $1.3 billion with Canada. The deficit with Latin America, except Mexico, dropped by $400 million.

Petroleum imports rose 15 percent to $10 billion. Nonpetroleum imports, led by cars from Japan and South Korea, increased 3 percent.

Exports increased for machinery, chemicals and consumer goods. The largest gains by area were Canada, $1 billion, Latin America, $500 million, and $300 million each in Japan and Western Europe.