Hi, all,
I tried to get the last observation of a sas data set by the simple data set option "FIRSTOBS=MAX", and it never works. Has someone an idea, whats wrong with that? (SAS 9.4)
Sample code:
data test; input var1;
datalines;
1
2
3
;
RUN;
data test; set test(firstobs=max); run;
Log:
WARNING: FIRSTOBS option > number of observations in WORK.TEST.
How can MAX be greater than no of observations, if MAX is number of observations?
What are you attempting to do?
Since you are getting 0 observations in your output you need to describe what the expected output is. Note: the max option isn't going to give you the last observation unless it happens to be about the 9.2 quintillionth if I read MAX as the firstobs option correctly.
Note that for your given example you will get the same result for firstobs=100 > you have asked for an observation whose number exceeds the number of the data set observations.
MAX is an option for OBS, but not for FIRSTOBS. If you want to retrieve the last observation from a data set, you can use:
data want;
set have nobs=_nobs_ point=_nobs_;
output;
stop;
run;
Don't forget the STOP statement, or you will have created an infinite loop.
@Astounding The docs state that MAX is valid in 9.4 at least.
Try using a different data set name.
What happens with following:
Data want;
set SASHELP.class (firstobs=max);
run;
Please post the full log.
The full log is
42911 data test; input var1;
42912 datalines;
NOTE: The data set WORK.TEST has 4 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
42917 ;
42918 RUN;
42919 data test; set test(firstobs=max); run;
WARNING: FIRSTOBS option > number of observations in WORK.TEST.
NOTE: There were 0 observations read from the data set WORK.TEST.
NOTE: The data set WORK.TEST has 0 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
What are you attempting to do?
Since you are getting 0 observations in your output you need to describe what the expected output is. Note: the max option isn't going to give you the last observation unless it happens to be about the 9.2 quintillionth if I read MAX as the firstobs option correctly.
Note that for your given example you will get the same result for firstobs=100 > you have asked for an observation whose number exceeds the number of the data set observations.
THANKS,
got my mistake.
MAX is NOT the number of observations in the current data set, BUT the maximal possible number of observations in any data set!
So I will work around that, its a pity, would have been so nice and short.
Please look at this code:
proc sql noprint; select nobs into : num from dictionary.tables where libname='SASHELP' and memname='CLASS'; quit; data junk; set sashelp.class (firstobs=&num); run;
In the Dictionary table the library name and memname are uppercase by default. If you are 100 percent sure that the only memname (dataset) of a given name only occurs one time you could skip the libname but I strongly recommend using it.
The dictionary tables (or views in the SASHELP library) contain all the information that SAS knows about your data sets and variables. Very much worth looking into for automation based on information about them.
Thanks,
fine.
There are several work-arounds, e.g.
data test; set test; x=1; run;
data test(drop=x); set test; by x; if last.x; run;
I just loved the idea to make it simple and did not see my misunderstanding...
Best Uli
@ballardw wrote:
Note: the max option isn't going to give you the last observation unless it happens to be about the 9.2 quintillionth if I read MAX as the firstobs option correctly.
That's not how I read it:
sets the number of the first observation to process to the maximum number of observations in the data set.
I can see how the interpretation can go both ways, but my initial expectation would align with what @UliS was expecting.
Thanks, thats exactly what I thought first, especcially because I read somewhere else, that this would be an error in sas 9.1 or so.
But most probably it was the same misunderstanding , should read
sets the number of the first observation to process to the maximum <POSSIBLE> number of observations in the data set.
Thank you all for your thoughts!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.