Two weeks ago, in a radio interview while vacationing at his California ranch, President Reagan declared, "I am basically opposed to the idea of punitive sanctions" against South Africa.

At the same time, he was telling aides privately that he would veto a sanctions bill even at the risk of a major confrontation with Congress.

Yesterday, Reagan partially reversed himself and put into effect many of the same limited economic sanctions against the white-minority government of South Africa that he had earlier opposed. In so doing, Reagan followed a common pattern of his presidency, holding out to the very last before retreating when threatened with certain defeat.

The decision to impose the limited sanctions and to embark on a more activist effort to influence South Africa took shape when the president returned last week to Washington to receive a blunt assessment from his senior advisers. According to informed officials, they told him that without quick action, his five-year policy of "constructive engagement" toward South Africa could well be repudiated by Congress. He was at risk, he was told, of losing all control over U.S. diplomacy toward Pretoria.

These officials, led by national security affairs adviser Robert C. McFarlane and joined last week by White House chief of staff Donald T. Regan, pointed out that Congress would cement the sanctions into law, making it more difficult for Reagan to move the Pretoria government toward reform.

By using an executive order to impose some sanctions on his own, as he did yesterday, Reagan was told, he could retain the flexibility to lift sanctions later if reforms were forthcoming, officials added. Reagan's constitutional prerogatives to make foreign policy, he was told, would be challenged if Congress imposed its own sanctions.

The officials also told the president that a confrontation with Congress would do little to achieve real change in South Africa, but that a compromise would send a powerful signal, and "the only way to do that would be to make a gesture of goodwill toward the Congress," as one put it.

Secretary of State George P. Shultz described this argument yesterday by saying Reagan wanted to "send a single message to the government of South Africa . . . on behalf of all Americans, on behalf of all the government, on behalf of the Congress and the presidency, that apartheid must come to an end . . . ."

Reagan's top advisers added one more powerful argument: a defeat on South Africa, in which a threatened presidential veto was overridden, would probably spill over into the administration's crowded legislative agenda this fall. It could complicate Reagan's push for tax revision and weaken him in battles with Congress over spending, trade and agricultural issues, they said.

Officials said they feared a "contentious" first phase of the president's fall push that would put him directly at odds with the Republican leadership.

Another factor that apparently influenced Reagan was the argument advanced last week by Senate Foreign Relations Committee Chairman Richard G. Lugar (R-Ind.) that many of the measures in the bill were not punitive sanctions of the type Reagan had said would hurt South Africa's 21 million blacks.

"Lugar's argument was very persuasive in terms of what the sanctions did," said a White House official. Reagan yesterday imposed sanctions he said were aimed directly at apartheid, but sidestepped more severe measures pending in Congress that he said would penalize South Africans generally.

Moreover, according to informed officials, Reagan was receptive to the idea of limited sanctions because there had been little progress toward negotiations in South Africa since the Aug. 15 address by President Pieter W. Botha, who rejected major reforms of apartheid, the nation's system of rigid racial segregation.

Although Reagan had remained optimistic about what he viewed as progress in South Africa, officials said it was obvious that negotiations were not beginning within the framework of a few weeks that McFarlane had set publicly.

Reagan's move to avert what seemed a certain legislative defeat was similar to other policy shifts of his presidency. A major one, on taxes, occurred after a big tax cut in his first year. Despite later vows that he would not accept tax increases, he later consented to four of them.

By most accounts, the concern about a legislative defeat was held by top White House officials for a month or more, but Reagan's views apparently were not changed until last week. Yesterday, one senior official, speaking to reporters at the White House on condition he not be identified by name, said Reagan's views were "maturing over a course of months . . . . We've been saying many of these things behind the scenes for a long, long time, and the president has endorsed our having said these things now."

Several officials said they think the president waited too long to impose limited sanctions, giving the unavoidable appearance that he was desperately avoiding a legislative defeat.

"We could have done this in June or July," one official said.