DATA Step, Macro, Functions and more

FIRSTOBS=MAX Warning

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

FIRSTOBS=MAX Warning

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
Super User
Posts: 10,500

Re: FIRSTOBS=MAX Warning

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.

View solution in original post


All Replies
Super User
Posts: 5,082

Re: FIRSTOBS=MAX Warning

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.

Super User
Posts: 17,829

Re: FIRSTOBS=MAX Warning

Occasional Contributor
Posts: 9

Re: FIRSTOBS=MAX Warning

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.
Super User
Posts: 17,829

Re: FIRSTOBS=MAX Warning

Try using a different data set name. 

 

What happens with following:

 

Data want;

set SASHELP.class (firstobs=max);

run;

Super User
Posts: 17,829

Re: FIRSTOBS=MAX Warning

Please post the full log. 

Occasional Contributor
Posts: 9

Re: FIRSTOBS=MAX Warning

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
Super User
Posts: 10,500

Re: FIRSTOBS=MAX Warning

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.

Occasional Contributor
Posts: 9

Re: FIRSTOBS=MAX Warning

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.

 

Super User
Posts: 10,500

Re: FIRSTOBS=MAX Warning

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.

Occasional Contributor
Posts: 9

Re: FIRSTOBS=MAX Warning

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

Super User
Posts: 17,829

Re: FIRSTOBS=MAX Warning


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:

 

MAX

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.

Occasional Contributor
Posts: 9

Re: FIRSTOBS=MAX Warning

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

MAX

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!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 12 replies
  • 354 views
  • 2 likes
  • 4 in conversation