BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
UliS
Fluorite | Level 6

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?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

12 REPLIES 12
Astounding
PROC Star

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.

UliS
Fluorite | Level 6
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.
Reeza
Super User

Try using a different data set name. 

 

What happens with following:

 

Data want;

set SASHELP.class (firstobs=max);

run;

Reeza
Super User

Please post the full log. 

UliS
Fluorite | Level 6

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


ballardw
Super User

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.

UliS
Fluorite | Level 6

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.

 

ballardw
Super User

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.

UliS
Fluorite | Level 6

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

Reeza
Super User

@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.

UliS
Fluorite | Level 6

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!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 12 replies
  • 2195 views
  • 5 likes
  • 4 in conversation