turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- FIRSTOBS=MAX Warning

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-03-2017 10:15 AM

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?

Accepted Solutions

Solution

01-03-2017
10:51 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-03-2017 10:34 AM

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.

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-03-2017 10:20 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-03-2017 10:22 AM

@Astounding The docs state that MAX is valid in 9.4 at least.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-03-2017 10:26 AM

MAX is a valid value for FIRSTOBS according SAS documentation, see http://support.sas.com/documentation/cdl/en/ledsoptsref/69751/HTML/default/viewer.htm#p0wjxoxrco6dsg....

Of course there are a lot of work arounds, but I intend to write a macro for several users, so the code should be as simple and understandable as possible.

Of course there are a lot of work arounds, but I intend to write a macro for several users, so the code should be as simple and understandable as possible.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-03-2017 10:32 AM

Try using a different data set name.

What happens with following:

Data want;

set SASHELP.class (firstobs=max);

run;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-03-2017 10:20 AM

Please post the full log.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-03-2017 10:22 AM

**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

Solution

01-03-2017
10:51 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-03-2017 10:34 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-03-2017 10:51 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-03-2017 11:09 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-04-2017 02:53 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-03-2017 03:42 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-04-2017 02:56 AM

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!