The Supreme Court ruled yesterday that raw census data, essentially the only means of challenging the accuracy of the population count, may not be disclosed to the cities and counties trying to prove undercounting.

The unanimous decision, declaring the data confidential and off-limits even in judicial proceedings, could undercut more than 50 pending suits protesting the 1980 count and make future challenges difficult.

Chief Justice Warren E. Burger, writing for the court, said confidentiality is essential to ensure public cooperation with the census.

A spokesman for the U.S. Conference of Mayors said the ruling "seems to foreclose" any review of the 1980 census' accuracy and means that "the major urban centers will continue to be under-represented in legislators for yet another decade."

In another important ruling yesterday, the justices said it was unconstitutional for states to hoard electric energy resources. They unanimously struck down New Hampshire's attempt to ban the out-of-state sale of hydroelectric power generated in the state.

The New Hampshire measure was one of the more extreme devices developed by states over the last few years to reap greater benefits, at the expense of other states, from their energy endowments. The court has now struck down two such efforts, Louisiana's last year and New Hampshire's yesterday, as unconstitutional "protectionist" enterprises.

The census ruling stemmed from challenges by Essex County, N.J., and Denver. Both jurisdictions, fearing the potential loss of federal money and political representation that comes with population decline, joined others across the country in suing the government for allegedly undercounting their populations. They said census takers erroneously classified occupied buildings as vacant.

To prove their claims, they sought the "master address registers" for their areas, listings of addresses, householder's names, vacancy status and other data compiled from postal information as well as responses to census questionnaires.

Census officials argued that the registers and all raw census data were exempt from the federal Freedom of Information Act and from "discovery," disclosure to opposing parties in court proceedings. The justices, agreeing with the 10th U.S. Circuit Court of Appeals and reversing the 3rd Circuit, accepted the government's argument yesterday in Baldrige vs. Shapiro.

Burger said that the Census Act requires secrecy for the data, even when it does not include the names of individuals responding to the census. Congress was not solely concerned with protecting individuals, he said. It was concerned with the protection of data provided by individuals.

"There is no indication in the Census Act that the hundreds of municipal governments in the 50 states were intended by Congress to be the 'monitors' of the Census Bureau," he said. "Only a bar on disclosure of all raw data reported by or on behalf of individuals would serve the function of assuring public confidence."

This, he said, applies no matter how valuable or essential the data might be in suits against the Census Bureau. The court's decision was based on what it saw as Congress' intent under the Census Act, and Congress could alter that by amending the law.

The energy controversy arose in 1980 when the New Hampshire Public Utilities Commission, acting under a turn-of-the century state law, told the New England Power Co. it could no longer export for regional use electricity produced on the Connecticut River in New Hampshire.

The commission said that New Hampshire's population and energy needs were increasing rapidly and that by hoarding the power, that state's customers could save $25 million a year.

Surrounding states, as well as the utility and the energy industry generally, attacked the ban as a major threat to regional energy pooling. They feared a massive disruption of the nation's power market.

Reversing the New Hampshire Supreme Court yesterday, the justices said the plan was inconsistent with the Constitution's commerce clause, designed to ensure the free flow of business among the states and to prevent them from behaving like competing foreign powers.

Burger, writing for the court, said the commerce clause "precludes a state from mandating that its residents be given a preferred right of access, over out-of-state consumers, to natural resources located within its borders."