The number of observations in a data set is stored as a part of the data set's metadata. There are multiple ways to retrieve this number and some are better than others.
Thanks to Art Carpenter for this tip. Read more details in this paper from WUSS 2017: Building Intelligent Macros Using Metadata Functions with the SAS Macro Language.