In general, for short term stay, rent, for long ter, buy. But other than that, what do you do?
I think for the cities you chose, San Francisco (or for that matter, surrounding areas - I have to pitch for the wonderful Marin) and New York; all have been and will continue to appreciate because of the vibrant job market, the location, the precious supply vs. demand, the gateway from foreign country to the U.S., the immigrants, ..., etc There are just a lot of potential here! .
Even though most of the housing market are slowier than before, S.F. onlyl has 2.4 month of inventory and Marin has only 3.9 months of inventhory, both areas are doing quite well. I also have a 40 years trend chart which shows that during the past 40 years, Marin housing price pretty much went down once, at around 1980, by around 5% only . The housing price went up all other years.
I guess what I am trying to say, is that personally, I would try to 'scrounge up' enough for mortgage payments than to rent. I have so many people telling me that how lucky they were to have gotten into the market before, otherwise they would not be able to afford their houses now. I think the same thing will be said by others a few years later.
There is appreciation, tax advantage, enjoyment of living in your own house (paint, remodel, landscape to your delight); and a basis to trade up later. If you don't start now, it'd truely be difficult to buy later in these cities
Check out this Historical Real Estate Quotes to Ponder. It will give you some perspective. http://www.sylviasellsmarin.com/Historical+Real+Estate+Marke
Good Luck! Either way, enjoy your stay - I have so much fun in both cities Amazing places; especially when you are young! .... Now that I think of this, maybe you should just rent and enjoy all things those cities can offer .. :-) ....