Did you mean JFK-MAD or MAD-JFK? I don't think JFK-MAD would work. I think once you clear customs you are not going to be able to get back on the train to T4S and then T4. In any case, is this worth it?
I did mean MAD-JFK.
Once you get back to T4 with the MAD-JFK ticket you can clear security again and then take the train back to T4S. Once in T4S there is nothing stopping you from getting back on the train to T4 or your exit point by taking the elevators down to the train. At least I don't recall anything stopping a person from doing that.
Now is it "worth it"? I'm not sure it is about $1,500-$2,000 dollar hold on your credit card and about 15-20 minutes going back and forth through security and on the train.
If I personally had a 3+ hour layover in MAD I would do it. I'd probably not do it if I was just leaving the airport for my hotel.