From the Dictionary of Nautical, University, Gypsy and Other Vulgar Tongues first published in 1859.
Sam: to “stand SAM,” to pay for refreshment, or drink, to stand paymaster for anything. An Americanism, origination in the letters U.S. on the knapsacks of the United States soldiers, which letter were jocularly said to be the initials of Uncle Sam (the Government), who pays for all. In use in this country as early as 1827.
(I did not know that Uncle Sam went back so far. I wonder why it was Sam, as opposed to Scott or Steve…)