The Senate yesterday voted to let the Reagan administration keep U.S. Marines in Lebanon for as many as 18 more months. The 54-to-46 agreement was virtually along party lines, with only three Republicans dissenting, while two Democrats backed the measure.

Voting one year to the day after President Reagan dispatched the first Marine unit to Beirut, the Senate approved a resolution essentially the same as the 18-month authorization passed by the House of Representatives Wednesday.

The resolution now goes to the president for his signature.

Reagan called the vote "a great victory for . . . a responsible, bipartisan foreign policy" and said it proves that "Americans stand united, we speak with one voice and we fulfill our responsibilities as a trustee of freedom, peace and self-determination."

The action by both chambers marked the first time Congress sought to invoke the War Powers Resolution, passed in 1973 after U.S. troops were withdrawn from fighting in the undeclared war in Vietnam.

It ends prolonged debate between the White House and legislators not only about U.S. involvement in the Lebanese civil war but also about prerogative--the president's right to deploy troops without congressional authorization.

The House had given the measure bipartisan support, with the Democratic leadership and nearly half of the Democratic members joining Republicans to pass the 18-month authorization.

But debate and the Senate vote yesterday were highly partisan, as Republicans sought support of Reagan's policy on Lebanon and Democrats labeled it a possible first step toward another Vietnam.

Senate Democrats George J. Mitchell (Maine) and Edward Zorinsky (Neb.) backed the president, while Republicans Mark O. Hatfield (Ore.), Lowell P. Weicker Jr. (Conn.), and William V. Roth Jr. (Del.) voted against the 18-month authorization.

On an earlier roll call, the Senate split, 55 to 45, precisely on party lines, in defeating a Democratic alternative that would have brought the Marine contingent home within 60 days unless Reagan, in a detailed report to the legislators, could persuade Congress to extend the deployment.

Several Republican senators had made evident this week their misgivings about the 18-month authorization.

House Speaker Thomas P. (Tip) O'Neill Jr. (D-Mass.) revealed yesterday that at one point Senate Majority Leader Howard H. Baker Jr. (R-Tenn.) asked the White House if it could live with a 12-month authorization, which Baker thought would pass more easily.

After Reagan replied that 18 months was the only time limit he would accept, strong pressure from the White House and their Senate colleagues brought most of the wavering Republicans back into the fold.

Doubters such as Dan Quayle (R-Ind.) and Mark Andrews (R-N.D.) held out until the last minute.

As the clock ticked out on the crucial roll call, Baker collared them on the Senate floor and delivered a forceful lecture. Both voted in favor.

The measures declares that the Marine Corps deployment in Lebanon is covered by the War Powers Resolution. Reagan has expressed reservations about giving Congress some control over dispatching troops to hostile areas but has agreed to sign it in order to get the 18-month authorization.

The first Marine amphibious unit was sent to Beirut Sept. 29, 1982, as part of a four-nation peace-keeping force and has been augmented to 1,600 men. Stationed near the Beirut airport with orders to shoot only when fired upon, the Marines have taken 43 casualties, including five dead.

Democratic senators warned forcefully yesterday that such losses might be only the beginning of a military escalation that could lead to another Vietnam.

"Some say that Lebanon is not Vietnam," Sen. Edward M. Kennedy (D-Mass.) said. "But I reply, we must not give the president the power to turn it into one."

Kennedy said the 18-month authorization means that Congress has said to Reagan, "Take 1,600 Marines and the battleship New Jersey and call us in 1985."

Baker replied that he, too, had "grave doubts" about sending the Marines to Lebanon in the first place. Now that they are there, though, Baker said, "it would be a mistake of major proportions" to withdraw them prematurely.

Unlike Wednesday's House debate, filled with impassioned speeches and tense exchanges, the three-day Senate debate resembled an academic exercise. Most of the time the only senators who stopped in to listen to the debate were those who had made up their minds on the issue.

The real dynamics took place off the floor, as Baker and Vice President Bush worked to keep Republicans in line and Minority Leader Robert C. Byrd (D-W.Va.) urged Democrats to unify in opposition.

During the congressional debate this week, the administration sent a team of White House and State Department officials to the Capitol for a lobbying effort aimed almost entirely at Republicans.

The Senate defeated a number of Democratic amendments yesterday. Sen. Claiborne Pell (D-R.I.) offered a substitute resolution authorizing deployment only through March, 1984, but, with Charles McC. Mathias Jr. (R-Md.) the only Republican to vote for it, the measure was defeated, 62 to 38.

The Senate measure was different only technically from the House version, calling on Reagan to report to Congress on Lebanon every three months, while the House had said every two months. Last night, the House voted, 253 to 156, to accept the Senate language, sending the measure to Reagan for signature.