Officials are at a loss to explain how they allowed a homeless, mentally ill man accused of stealing a soda to languish in jail for 17 months.

Edward Perez's attorney, his court-appointed psychiatrist, the judge in his case and Lake County jail officials all apparently believed he had been released a year ago.

The mistake was not discovered until this month, after a new warden ordered a review of all inmates' files, Sheriff Roy Dominguez told the Post-Tribune of Merrillville for a story Friday.

"This is very unfortunate," Judge Sheila Moss said. "This is a guy who apparently needed services, and he should have been somewhere where he could get that, rather than sitting in our county jail, which is already overcrowded."

The jail released Perez, 22, and transferred him to a mental health clinic Feb. 7, Dominguez said.

Perez had stayed in the jail's medical wing since July 2, 2005, after allegedly stealing a bottle of Pepsi from a Wal-Mart in Schererville, Indiana, Dominguez said.

A police report that referred to the man as "Edward Hammer-Perez" said that before stealing the soda, he said he had just gotten out of jail and wanted to go back. He listed the state psychiatric hospital in Logansport, Indiana, as his address.

In February 2006, the psychiatrist went to the jail to evaluate Perez only to be told he already had been released, Moss said. The judge said that after the psychiatrist informed her, she deferred to defense attorney Fred Flores, who agreed that his client was not behind bars.

Moss, noting that Perez appeared in some records as "Edward Hammer-Perez," speculated he might have been jailed under a different name than appeared on his court file.

The jail kept Perez in custody because it never received a release order, said Mike Higgins, a sheriff's spokesman. Moss said she had seen no need to issue such an order because Perez was "by all accounts, already free."

A telephone message left by The Associated Press seeking comment from Flores was not immediately returned Friday. An employee at the clinic where Perez was taken said officials authorized to comment were unavailable until Monday.