The Prophet ﷺ said, "When one of you arrives at a gathering, let him give the greeting of peace. If he then wishes to sit, let him sit. Then when he gets up to leave while the people are still seated, let him give the greeting of peace, for the first is no more deserving than the last."
حدثنا يحيى عن ابن عجلان قال حدثني سعيد
عن النبي ﷺ قال إذا انتهى أحدكم إلى المجلس فليسلم فإن بدا له أن يجلس فليجلس ثم إن قام والقوم جلوس فليسلم فليست الأولى بأحق من الآخرة
Its chain of transmission is sahih. Abu Dawud narrated it, 4/253, no. 5208, in "Manners," "Giving greetings when rising from a gathering," and Tirmidhi narrated it in "Seeking Permission," "What has been said about giving greetings upon rising," 5/62, no. 2706, and graded it hasan.