May Department Stores Co. and Associated Dry Goods Corp. ended a month's stormy courtship yesterday by agreeing to a merger that will create the nation's second-largest department store operator.

Under terms of the agreement, May would exchange 0.86 share of its common stock for each Associated share outstanding. The actual price of the acquisition will depend on the price of May stock when the transaction closes. The deal requires approval from both boards of directors, both companies' shareholders and federal regulators. Neither firm would speculate about when that would be.

At May's closing price yesterday, $71.87 1/2, down $3, the merger offer would be worth $61.81 a share, or $2.5 billion.

That price would be less than May's original unsolicited offer of a swap of May stock worth $66 for each share of Associated, or $2.7 billion, which Associated's management had termed "inadequate." The value of the final agreement will exceed that original offer only if May's stock moves substantially above its current price.

Associated investors reacted unfavorably to the agreement, and the stock fell $3.25 to $58.75. Before the original takeover offer was made, Associated stock traded at $46.37 1/2

May is the nation's third-largest department store operator and the parent of the local Hecht Co. chain. The St. Louis company reported profits of $235.4 million on sales of $5 billion in the fiscal year that ended Feb. 1.

Associated, of New York City, is the nation's fifth-largest department store operator, owning 14 retailers, including Lord & Taylor, Caldor's and Loehmann's. The company had earnings of $119.7 million on sales of $4.4 billion in the fiscal year that ended Feb. 2.

The merger would create the second-largest department store operator, behind Federated Department Stores Inc., and fourth-largest retailer, behind Sears, Roebuck and Co., K mart Corp. and J. C. Penney Co., analysts said.

The merger "will create a real retailing powerhouse," said Jim Abrams, a May spokesman.

Joseph H. Johnson, chairman and chief executive of Associated, originally led the resistance to May's offer, but said yesterday he was "delighted" that the two companies "could now proceed together toward building one of the great retailing companies in the world."

May launched its surprise takeover attempt June 20, offering to exchange each Associated share for May stock worth $66, a ratio of 0.75 share of May for each Associated share.

After Associated's board of directors failed to reply promptly, May went to Associated's stockholders directly with a tender offer of $60 cash a share.

Associated then rejected the swap offer and said it would consider selling its individual operations and repurchasing its shares.

Then on Monday, Associated said it would accept an exchange ratio of 0.882 May share for each Associated share, which would have been worth about $2.8 billion at the time.

By accepting the offer yesterday, Associated indicated that it had not found a better offer elsewhere for the company as a whole or for its parts, analysts said.

"They didn't find a white knight, so they had to do the deal," said William N. Smith, an analyst with Smith Barney, Harris Upham & Co.

Both boards of directors are scheduled to meet Friday.

The companies would not say what role Associated's management might have after the merger. A statement released by May said only that David C. Farrell, May's chairman and chief executive, "very much looked forward to working with Mr. Johnson and the Associated organization in this endeavor."