Al-Adab Al-Mufrad, Book 42, Hadith 45 (In-Book)

Abu Hurayra reported that the Prophet, may Allah bless him and grant him peace, said, "When a man comes to a gathering, he should give the greeting. When he sits down and then thinks that he should leave before the gathering has broken up, he should give the greeting. Neither is more of a duty than the other."

حَدَّثَنَا خَالِدُ بْنُ مَخْلَدٍ، قَالَ‏:‏ حَدَّثَنَا سُلَيْمَانُ بْنُ بِلاَلٍ قَالَ‏:‏ حَدَّثَنِي مُحَمَّدُ بْنُ عَجْلاَنَ قَالَ‏:‏ أَخْبَرَنِي سَعِيدٌ، عَنْ أَبِي هُرَيْرَةَ، عَنِ النَّبِيِّ صلى الله عليه وسلم قَالَ‏:‏ إِذَا جَاءَ الرَّجُلُ الْمَجْلِسَ فَلْيُسَلِّمْ، فَإِنْ جَلَسَ ثُمَّ بَدَا لَهُ أَنْ يَقُومَ قَبْلَ أَنْ يَتَفَرَّقَ الْمَجْلِسُ فَلْيُسَلِّمْ، فَإِنَّ الأُولَى لَيْسَتْ بِأَحَقَّ مِنَ الأخْرَى‏.‏

sunnah.com