STOCKHOLM, AUG. 26 -- After nearly a quarter century of disagreement on issues ranging from nuclear weapons to Vietnam and Nicaragua, Sweden and the United States are preparing to proclaim their friendship when Prime Minister Ingvar Carlsson visits Washington early next month.

Officially neutral since the early 19th century, Sweden tries to keep its distances from both superpowers. Bilateral relations with the Soviet Union have been strained recently over a series of issues, including the suspected presence of Soviet spy submarines in Swedish waters and a territorial dispute in the Baltic Sea.

At the same time, relations with the United States have warmed, a situation that Sweden attributes more to the absence of direct bilateral differences and U.S. acceptance of general foreign policy differences than to any changes in Swedish outlook.

Carlsson's week-long trip will be the first state visit by a Swedish head of government to the United States since 1952, when president Truman was host to prime minister Tage Erlander. Erlander also paid an unofficial visit to president Kennedy in 1961.

In subsequent decades, despite thriving bilateral trade and cooperation on a number of issues, close cultural ties and a shared belief in democratic traditions, the relationship between the countries was more memorable for its valleys than its peaks.

To anticommunist America, Sweden has often seemed a sanctimonious thorn in the western side, preaching peace and nonintervention while discounting the responsibilities of superpower status. To neutralist Sweden, the United States sometimes has appeared a bossy behemoth, sharing with the Soviet Union an immature tendency to use force as a first resort in international conflicts.

Much of the controversy between the two countries swirled around socialist Olof Palme, who served as prime minister from 1969 to 1976, and again from 1982 until his assassination in February 1986. A flamboyant and outspoken figure, Palme unnerved and irritated a series of American administrations.

When Swedes demonstrated against the U.S. war in Vietnam during the 1960s and 1970s, Palme often led their marches. In the early 1970s, relations became so strained that Washington refused to accept an ambassador from Stockholm for more than a year.

A leading advocate of nuclear disarmament, Palme described President Reagan's Strategic Defense Initiative as "very dangerous" for world peace. In 1985, the State Department protested when Palme called Reagan's "so-called crusade against communism in Nicaragua . . . nothing more than a plundering expedition against poor peasants."

In more recent years, Carlsson told a group of American reporters today, relations have been based on an "agreement to disagree over certain issues."

In many bilateral areas, Washington and Stockholm have been of like minds. Last spring, following efforts by Stockholm to crack down on smuggling of high-technology information to Eastern Europe and the Soviet Union, the United States made Sweden the first non-NATO country to be exempted from licensing requirements on technology exports.

Carlsson said he still plans to make Sweden's opposition to U.S. policy in Central America and southern Africa focal points in his talks with Reagan. He said the Reagan administration has failed to respect international law in Nicaragua and he described the Sandinista government there as legitimately elected.

Sweden is one of the few Western European countries still supplying significant economic aid to Nicaragua, and has been a leading supporter of a regionally negotiated peace there.

But Carlsson said that his coming visit has "symbolic value" in demonstrating that, despite differences, U.S.-Swedish relations are rooted in friendship.

The Washington trip comes at an opportune time for Carlsson, whose 18-month-old administration began with the crisis of Palme's assassination. Although Carlsson has been widely credited with wise financial management and skillful political stewardship of the Social Democratic government, he has been burdened with a series of problems unprecedented in traditionally tranquil Sweden.

First came the shock of Palme's murder, followed by the government's failure to solve it and by evidence of police incompetence. Then the Soviet nuclear disaster at Chernobyl sent waves of radiation into Sweden and caused fear bordering on panic.

Recently there have been embarrassing revelations that one of Sweden's leading weapons and explosives producers, Nobel Industries and its subsidiaries, illegally shipped arms and dynamite to prohibited countries, including Iran, and may have bribed officials in the Indian government to win a lucrative contract.

Carlsson denied that Washington's invitation was designed to signal a level of U.S. approval that had been withheld from Palme. The subject of a state visit, he said, had first been broached by the Americans before Palme's death.

But there is little question that in style, if not necessarily in substance, Carlsson, 47, is a much easier person for Washington to deal with than Palme.

Carlsson insisted today that he and Palme, whom he served as deputy prime minister, never disagreed on "any important policy issue." Like Palme, he spent a formative period in the United States, where his graduate studies at Northwestern University and travels around the country constituted what he calls "one of the best years of my life."

But whereas Palme came from an upper-class background and shot to prominence as Erlander's young alter ego, Carlsson is from the working class and worked his way up through the Social Democratic Party ranks.

Where Palme was eloquent and flamboyant, Carlsson is staid and often stilted. In the words of many of his countrymen, who take a certain pride in their reputation for stolid homogeneity, Carlsson is "more Swedish" than his predecessor.

"In all important respects," Carlsson said, "I stand up for the same policy. But . . . I am a different person than Olof Palme."