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

Hi everyone. Recently i was asked by apprentice about SAS function end=eof and to my shocking, i know nothing about this.

Upon Google search, i only managed to find 1 PDF file about end of file but i still dont understand the purpose of it.

The link is :

http://support.sas.com/resources/papers/proceedings09/074-2009.pdf

 

Does anyone have a better explanation or elaboration of end=eof ? I have never used that in my life.

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

You can specify end=eof in the set statement. Eof will then be a binary variable (0/1) with the value 1 for the last observation in the data set and 0 otherwise. Eof is automatically dropped. However, for demonstrational purposes, I assign the value to another variable var below. See the example below

 

data test;
    set sashelp.class end=eof;
    var=eof;
run;

proc print data=test;
run;

This enabled you to eg. keep only the last observation in a data set like this

 

data test2;
    set sashelp.class end=eof;
    if eof=1;
run;

View solution in original post

9 REPLIES 9
PeterClemmensen
Tourmaline | Level 20

You can specify end=eof in the set statement. Eof will then be a binary variable (0/1) with the value 1 for the last observation in the data set and 0 otherwise. Eof is automatically dropped. However, for demonstrational purposes, I assign the value to another variable var below. See the example below

 

data test;
    set sashelp.class end=eof;
    var=eof;
run;

proc print data=test;
run;

This enabled you to eg. keep only the last observation in a data set like this

 

data test2;
    set sashelp.class end=eof;
    if eof=1;
run;
imdickson
Quartz | Level 8

Thanks for the clear explanation and sample.

To my understanding, this would be the only purpose of using end=eof right?

PeterClemmensen
Tourmaline | Level 20

No. But it is a classic use of the end= Option 🙂

 

Sometimes users want to keep all observation except the last one. Also a nice use for the End= Option.

Kurt_Bremser
Super User

There are multiple uses for this. Imagine you need to write a csv where the last line is expected to hold the number of records only:

data _null_;
file "myoutfile.csv" dlm=',' dsd;
set sashelp.class end=eof;
retain recordcount 0;
if _n_ = 1 then put 'name,sex,age,height,weight';
put name sex age height weight;
recordcount + 1;
if eof then put recordcount;
run;
Patrick
Opal | Level 21

@imdickson wrote:

Thanks for the clear explanation and sample.

To my understanding, this would be the only purpose of using end=eof right?


There are quite a few use cases for the end= dataset option. You use it whenever something needs to happen when reading the last observation from table(s). Below another common use case.

data demo;
  do while(not eof);
    set sashelp.class end=eof;
    output;
  end;
  stop;
run;
ErikLund_Jensen
Rhodochrosite | Level 12

Hi @imdickson 

 

I have a comment as a supplement to @PeterClemmensen 's good explanation. You will get a lot of hits in google, if you search for  "sas end = dataset option" instead.

 

The SAS END= dataset option can take any valid SAS variable name as argument. The use of "eof" (end-of-file) as variable name is a tradition from way back, but not part of the syntax, so this works the same way;

 

data a;
	do i = 1 to 10; output;
end;
data b; set a end=last_observation;
	if last_observation then put i=;
run;

 

If a data step has more than one Set statement, it is often useful to have a check for last observation in each statement, which requires different variable names.

 

 

 

 

 

ballardw
Super User

Please not that I do not recommend using that particular combination: end=eof

 

Because eof is also an option for a set statement which

As is eov

sustagens
Pyrite | Level 9
Is there an equivalent "start" option to get the first row/start of file?
PeterClemmensen
Tourmaline | Level 20

@sustagens please start a new thread for this question. That way, we keep different subjects apart and help future users. Thank you

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 9 replies
  • 54249 views
  • 4 likes
  • 7 in conversation