Which state's retirees are the best prepared?

Millions of Americans are ill-prepared to retire. But a lot depends on where you live.

Retirement security is a combination of having enough savings and pension income to cover costs associated with aging, especially medical bills. As older workers continue to postpone retirement, job market conditions also play a critical factor.

Those characteristics vary widely across the U.S., which makes retirement security somewhat easier to achieve in some states than others, according to a new analysis from the National Institute on Retirement Security (NIRS). The research and advocacy group, which represents public and private retirement service providers, ranked the 50 states on a score of 1 to 10 based on selected income, cost and labor market data.

Overall, the analysis echoes numerous studies showing that most Americans lack the savings needed to cover their living and health-care costs in retirement. The average American has saved less than a year's salary, according to NIRS' executive director, Diane Oakley.

"That's OK if we were all 30 years old—we would be on target," she said. "The reality is there are a lot of baby boomers who are in their 50s and 60s at the same level."

Since 2007, fewer households are covered by traditional defined benefit pension plans, as established employers scale back or freeze existing plans and start-ups typically offer only defined contribution plans, Oakley said.

Read MoreCan you afford to live to 100?

For many retirees, meanwhile, housing and health-care costs continue to rise. The result is that savings are stretched even further. And the strain varies widely state by state, according to the NIRS analysis.

To see how retirees are faring across the country, the NIRS developed a scorecard that rates states based on eight measures of financial security: Share of private workers enrolled in a retirement plan at work; the average defined contribution account balance; the marginal tax rate on pension income; the average out-of-pocket spending for Medicare patients; the average Medicaid spending per elderly patient; the share of older households spending 30 percent or more of income on housing costs; the unemployment rate of people 55 and older; and the median hourly earnings of workers 55 and older.

In California, for example, the average defined contribution account balance held by the minority of Golden Staters who have saved anything fell from $25,440 in 2000 to $23,381 in 2012. The health-care burden for older Californians, meanwhile, is among the heaviest in the country, with Medicaid payments of $9,449 per older beneficiary (among the lowest in the country) and out-of-pocket Medicare costs of $1,890 (among the highest).

Only three states had higher housing cost burdens. And California taxes pension income at nearly 6 percent.

With not enough saved to retire, labor market conditions also play a role in retirement security

"The new retirement plan is to keep working," said Oakley.

That also puts older Californians at a disadvantage when it comes to retirement security, according to the NIRS. As of 2012, the state had a jobless rate for older workers of 8.5 percent—second highest behind Nevada. And while the $15.93 median hourly wage for older workers was among the highest in the nation, that group lost ground from the $16.74 median hourly wage in 2007.

Read MoreHow to pay for Medicare's future

Retirees in Wyoming, meanwhile, are in relatively good shape. While the share of workers covered by an employer retirement plan has fallen since 2000, the average $33,553 in defined contribution savings is among the nation's highest. The state also had a zero percent marginal tax rate on pension income.

Costs for Wyoming retirees get an above average score, thanks in part to average Medicaid payments of $27,781 for older beneficiaries and relatively low housing costs, with just 23 percent of older households paying 30 percent or more of their income for housing. Wyoming ranked 14th in Medicare costs, with an average $1,643 in out-of-pocket costs.

Wyoming also ranked highly in job opportunities, with a relatively high median wage for older workers at $15.00 per hour and a relatively low unemployment rate of 3.7 percent for that age group.

Nationwide, about two-thirds of all American workers report that they or their spouses have saved for retirement. Nearly 80 percent of full-time workers have done so, according to the latest annual survey by the Employee Benefit Research Institute (EBRI).

Among those with a retirement plan, including an IRA, defined benefit or defined contribution plan, about a third reported having saved at least $100,000.

For those without a retirement plan, the outlook is even worse. Two-thirds of that group report saving more than $1,000 for retirement, and only about a quarter of them have determined how much they'll need to retire, according to EBRI.

Read MoreFidelity: 401(k) contributions hit record highs

In response to the dismal level of retirement readiness, President Barack Obama recently proposed setting up automatic IRAs at the federal level, and has launched the myRA (my retirement account), a voluntary program for workers not covered by a retirement plan at work. The Treasury Department has yet to release enrollment details about those plans.

Many states have begun exploring the idea of providing their own retirement savings programs for workers who don't have access to an employer-sponsored plan.

These so-called automatic IRAs would enroll you in a retirement savings plan, unless you specifically tell your employer you want to "opt out" of the program. It turns out that millions of other American workers would be a lot further ahead saving for retirement if they were automatically enrolled, according to an analysis by EBRI.

The idea has been studied by two dozen states, but only one has adopted it so far. This year, Illinois became the first state to put in place a new program to expand retirement savings plans for workers who don't already have them.

Starting in 2017, employers who have been in business for more than two years and have more than 25 workers on the payroll will be required to automatically sign up their workers for a state-sponsored IRA account. The plans will be funded by an after-tax paycheck deduction of at least 3 percent. (Washington state has moved to set up a marketplace offering workers various options, but the companies are not required to offer them.)

Read MoreCould automatic IRAs boost nest eggs by $490 billion?

For the states, the idea is one strategy aimed at heading off the budget impact of a major shortfall of retirement savings as older workers who can't make ends meet rely more heavily on state-funded social programs.

"The states are there for help with Medicaid, food, heating assistance—all of those are program where we have some type of safety net," said Oakley. "So there are going to be more demands on our safety nets."