Only problem is that Game Maker doesn't directly work with conditions. He'd need to use "if" for everything in that manner. (Other kewl ways too, advanced though)
Create the border and make it follow the view. If Sonic hits the right border, set his speed to something slower than the view is moving. If he hits the left border, set his speed to something faster than the view. He will sorta bounce off the edges, but won't be able to leave it. You can tune it to make it almost not notable. Alias, if the view moves at a speed of 4, make Sonic run at 3.5 when he hits the edge. It will be a minor bounce, and Sonic will stay in the view.