CONCORD, N.H. — A federal appeals court has upheld the constitutionality of New Hampshire’s first-in-the-nation law making doctors’ prescription writing habits confidential.

The ruling Tuesday by the 1st Circuit U.S. Court of Appeals in Boston overturns one last year in New Hampshire saying the law unconstitutionally infringed on free speech.

The ruling could affect the future of a similar law in Maine that has been on ice since last December when a federal district judge in Bangor found it unconstitutional.

The appeals court said the New Hampshire law, intended to thwart hard-sell tactics by drug companies to doctors, is a valid step to promote the delivery of cost-effective health care.

“Even if the Prescription Information Law amounts to a regulation of protected speech — a proposition with which we disagree — it passes constitutional muster,” the court said.

“In combating this novel threat to cost-effective delivery of health care, New Hampshire has acted with as much forethought and precision as the circumstances permit and the constitution demands,” the court said.

Drug companies use the information to target and tailor sales pitches to particular doctors. Patients’ names are not included in the data.

Less than a month after the law took effect in 2006, IMS Health Inc. of Norwalk, Conn., and Verispan LLC of Yardley, Pa., sued to have it declared unconstitutional.

The companies, which collect, analyze and sell prescription information, said the law went too far. They argued it violated free speech, endangered public health and impeded research.

U.S. District Judge Paul Barbadoro in Concord threw out the law in April 2007. Another federal judge, John Woodcock, subsequently ruled against a similar law in Maine, relying heavily on the New Hampshire decision.

In Maine, Sharon Treat, executive director of the National Legislative Association on Prescription Drug Prices, said Wednesday that the appeals court ruling gives a green light to all states wanting to restrict access to prescribing information.

Treat noted that the timing of the appeals court decision comes as newly elected legislators around the country are setting their legislative agendas and drafting proposals for the 2009 session.

“This is an early holiday gift,” she said.

Maine’s law, which would allow individual doctors and other prescribers to “opt out” of allowing companies to access their prescribing information, is less restrictive than New Hampshire’s. Anticipating an appeal of Woodcock’s 2007 decision, Treat said she hopes Maine will someday “enact a flat-out ban like New Hampshire has.”

Supporters of the laws argue that drug companies use the data to manipulate doctors and aggressively market off-label uses for drugs, driving up health care spending and improperly interfering with doctors’ professional practices.

The data-mining companies don’t apologize for making a profit and stress that the information they gather also is used by researchers, law enforcement and government agencies.

The data are considered invaluable by the tens of thousands of drug company salespeople who use it to identify doctors’ drug preferences, whether they favor brand-name medicines over generics, and whether they have been willing to prescribe new drugs to the market.

Meg Haskell of the BDN staff contributed to this report.