I have a SQL Server Script that works but I would like to add one more feature.  Finally, I would like to select the top row (max date) per group.  I've managed to create row numbers (rn) per group and attempted adding in the code  WHERE rn=1 in several locations but always get errors.  Is there a way I can select the top row per group (NewTab.sid) either by using the row numbers or some other means?
SELECT NewTab.sid,
       NewTab.pid,
       NewTab.edate,
       NewTab.codeid,
       NewTab.SStat,
       p.BaseStart,
       p.BaseEnd,
       ROW_NUMBER() OVER (PARTITION BY NewTab.sid ORDER BY NewTab.edate DESC) AS rn
FROM PrTab p
     INNER JOIN
     -- first set
     (SELECT c.sid,
             c.pid,
             c.edate,
             c.codeid,
             'NS' AS Status
      FROM ClTab c
      WHERE c.codeid IN ('var1', 'var2')
      UNION
      -- second set
      SELECT c.sid,
             c.pid,
             c.edate,
             c.codeid,
             'XS' AS Status
      FROM ClTab c
      WHERE c.codeid IN ('var3', 'var4')
      UNION
      -- third set
      SELECT c.sid,
             c.pid,
             c.edate,
             c.codeid,
             'SM' AS Status
      FROM ClTab c
      WHERE c.codeid IN ('var5', 'var6')) NewTab ON p.pid = NewTab.pid
WHERE NewTab.edate < p.BaseEnd
ORDER BY NewTab.sid ASC,
         NewTab.edate DESC;
 
    