There is a quick test to test both MIDI timing and Audio.
It involves running a cable from your output back to your input and re-recording a track. Use a audio or midi kick drum so as to ge a nice transient spike.
DO as said above and zoom in and see if they line up. Your audio drivers and Sonar are supposed to work together and automatically adjust. Sonar actually delays playback by the amount of latency of the system so that you hear it and play along in time but your recording is actually ahead by the amount of delay.
I ran this test on my Sound Blaster Audigy and it was out by a random amount,, this is why their drivers are well known garbage.