Is there any research/study that looked at the extent to which the prospect of a guaranteed-for-life job (tenure) motivate researchers to stay in academia?
I am most interested in the United States and the field of computer science.
I'll respond based on anecdotal evidence, but over 30+ years, worrying about my several PhD students, and partly in a role as director of grad studies in math, giving career advice, etc. My experience is in math, not comp sci, though.
In any case, even 25 years ago, it was clear (both anecdotally and documentably by the AMS at the time, although not so accessibly, due to lack of internet...) that the "golden days" of academic math (at least) were waning, because of growth of PhD production that surpassed growth of academic positions. There had indeed been a boom of branch campuses, but that had roughly stabilized c. 1990. Some very good people (including some of my own students) got tenure at a branch campus in rural areas, but/and decided that the heavy teaching load and isolation was not what they'd signed on for.
By the mid-90s, the job market was consistently tight enough so that new PhDs with partners very often opted for non-academic trajectories, but so they'd not have to commute crazy distances, and so they could have a family with both parents participating. Hard to argue with that. The former "monastic" model of academe, or of infinitely-accommodating spouse, etc., is not acceptable these days. (I've tried long-distance stuff myself, too, and, sad-to-report, it really only works well when one is secretly glad to be apart... uh,...)
In the last several years, some of the most able and motivated grad students have had the sense to realize that, in particular, their chances to make a big-enough contribution to get a permanent job at an R1 place were pretty slim... and they didn't want to stay in academe "at all costs", so shifted their energies to avenues where being smart, motivated, and hard working had a better chance to pay off (these days). (See @DavidZ's comment.)
I've followed up with some of the people who did stay in academe and settled in relatively remote areas at small branch campuses, and, while they're "ok", they have "confessed" that it wasn't at all what they'd hoped for. Some speculation (obviously plausible) that without forced retirement, there's a lot of "waiting around" for better positions to open. (Not unlike situations in Europe, as I gather from friends there, where saturation had occurred much earlier, apart from mandatory retirement issues.)
The palpable corporatization and commodification (or at least the greater visibility of it) of higher education does also seem to have a consistently disenchanting effect on undergrads' choices about what to do after their B.S. in math (e.g., going to work for an insurance company... is (even?) more attractive now than it may have been in the past, and for some people seems vastly more viable than "grad school"...)
Although I suppose one should always be wary that possibly one's own cumulative disenchantment interferes with perceptions of others' opinions, I do have a rough idea of our local "stats" about academic versus non-academic positions for PhD'd people: once upon a time, everybody went into academe, but nowadays perhaps as many as 1/3 of each year's PhD's do not.
Even disregarding the mandatory retirement issue, it's simply not a time of expansion for higher ed... but/and the element of Ponzi-scheme of academe has no mechanism built in to reduce PhD production without finding someone else (adjunct faculty!?!) to exploit... or else (in the case of math) teaching loads would skyrocket, etc.
(This answer is about tenure in the United States.)
One convincing economic interpretation of tenure is that it is a form of non-monetary compensation. Of course, not every field is ripe with high-paying industry jobs. But academia is generally full of bright and motivated people who could do other things if they chose to leave academia, and salaries in academia are often lower than jobs in industry. This is particularly true in computer science at the moment, because of the booming tech sector. Tenure helps universities keep at least some faculty who might otherwise leave.
Sure, someone might be able to raise their salary by leaving academia, but they would also gain more employment risk. Small businesses (e.g. startup companies) often fail, and even larger businesses often lay off workers by the hundreds. By comparison, universities may seem quite stable, and with tenure someone has a much higher expectation of not needing to look for alternate employment. So the security of tenure may help to compensate (through lower risk) for the lower salary.
There are changing circumstances in higher education, and the analysis just presented may not be as compelling as it was 20 year ago. Not all universities seem very stable nowadays. And there are challenges with the tenure system that evolved in the 20th century. But perhaps the bigger question is whether academia, by promoting security over salary, might over-attract people who are simply risk-averse, rather than those would would be the best professors.