Gary Sanchez took a chance, but his seventh-inning stretch on the bases didn’t work out, and it became the focal point of the Yankees’ 7-6 loss to the Rangers on Sunday at Yankee Stadium.

Sanchez played a big role in bringing the Yankees back from a 7-0 deficit after four innings, hitting a three-run homer to cap a four-run fifth, but the seventh and ninth innings didn’t go well for him.

After Ronald Torreyes led off the seventh with a home run to make it 7-5, Aaron Judge and Sanchez drew consecutive two-out walks from Jose Leclerc. Lefthander Alex Claudio relieved him to face Didi Gregorius, and after Gregorius’ bid for a go-ahead three-run homer hooked just foul, he singled to rightfield. Judge scored to make it 7-6 but the slow-footed Sanchez was thrown out at third by Shin-Soo Choo. Third baseman Adrian Beltre moved up to take the throw, dived and tagged the sliding Sanchez in the back as he was going by.

Sanchez said he ignored third-base coach Joe Espada’s signal to stay at second. “Yeah, definitely that’s a mistake on my part,’’ Sanchez said through a translator. “Should never be the last out on third base. I saw Espada, he was holding me up and I kept on going. I thought I had a chance. I didn’t . . . I was running hard. He made a great throw and got me out.’’

The next batter would have been Chase Headley, who led off the eighth with a double, his third hit of the game. It was his first game back after treatment for back spasms.

advertisement | advertise on newsday

“You hate to make the out there,’’ Headley said of Sanchez. “I think [Choo] made a good play to throw him out. I think most of the time he’s probably safe right there. But you certainly hope not to make that third out. I made my own mistake [in the sixth] trying to steal, so . . . ’’

Joe Girardi took the blame for giving Headley the green light but did not excuse Sanchez. “That’s his decision,’’ the manager said. “And I think he just tried to do too much. It’s part of the game. Sometimes you’re going to make bad reads . . . It can’t be a chance. You’ve got to be sure. And we talked about it. I think it’s maybe the frustration of the two weeks that we’ve been involved in. Sometimes guys try to do too much.’’

After Judge lined a single to left against Matt Bush with two outs in the ninth, Sanchez had a chance to redeem himself, but he was unable to check his swing on a breaking ball away and struck out. “I was trying to get on base or bring the run in,’’ he said. “I just couldn’t do it.’’

Michael Pineda (7-4) had put the Yankees in a hole they couldn’t quite escape. Elvis Andrus had an RBI single and Beltre hit a two-run homer to leftfield in the first inning, Choo belted a three-run homer into the second deck in rightfield with two outs in the second and Drew Robinson homered into the Yankees’ bullpen with two outs in the fourth for his first major-league hit. That gave the Rangers a 7-0 lead.

“This happens when you don’t have command of your pitches,’’ Pineda said.

Yankees videos

The Yankees were dormant until the fifth, when Sanchez followed Judge’s two-out RBI single with a three-run shot over the centerfield wall. At 7-4, the Yankees liked their chances against a shaky Rangers bullpen, but they wound up with their 10th loss in 12 games. They remained in a virtual tie for first place in the AL East with the Red Sox, a percentage point ahead.

The Yankees also lost Aaron Hicks to the disabled list with a right oblique injury and played without Starlin Castro, who had an injection in his right wrist to deal with a nagging injury suffered weeks ago. Jacoby Ellsbury, rehabbing from a concussion, could return to replace Hicks, Girardi said. Castro said he will be back Monday night against the White Sox in Chicago.

“We’re still in a good spot,’’ Girardi said. “It’s the end of June. We need to turn it around. I think this is an important road trip for us and we got to get some things straightened out.’’