DATA Step, Macro, Functions and more

end-of-file with direct access to read data.

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

end-of-file with direct access to read data.

[ Edited ]

Hi,

 

in the Certification book (Base) i found this question:

 

There is no end-of-file condition when you use direct access to read data, so how can
your program prevent a continuous loop?
a. Do not use a POINT= variable.
b. Check for an invalid value of the POINT= variable.
c. Do not use an END= variable.
d. Include an OUTPUT statement.

 

with this answer

 

Correct answer: b
To avoid a continuous loop when using direct access, either include a STOP
statement or use programming logic that executes a STOP statement when the data
step encounters an invalid value of the POINT= variable. If SAS reads an invalid
value of the POINT= variable, it sets the automatic variable _ERROR_ to 1. You can
use this information to check for conditions that cause continuous looping.

 

I understand the use of STOP statement but i can't realy figure out what the "invalid value" can be used. Can someone explain better or with other words what does this answer mean ? And maybe post some example code ?

 

thanks,

Arjuna


Accepted Solutions
Solution
‎09-09-2017 01:05 PM
Super User
Super User
Posts: 7,844

Re: end-of-file with direct access to read data.

[ Edited ]
Posted in reply to arjunascagnetto

Looks like a really poor question.

If you are using a loop of some sort with POINT= option on SET to do access you could also use NOBS= option so you know how many observations there are.

data want;
  do p=1 to nobs;
    set have point=p nobs=nobs;
    output;
  end;
  stop;
run;

If you are reading a list of observation numbers from another dataset then you could also use NOBS to check before even trying to use POINT=.  No need for a STOP statement in this case as the step will stop when it reads past the end of the dataset RECORD_LIST.

data want ;
   set record_list;
   if 1 <= recno <= nobs then set have point=recno nobs=nobs;
   else put 'Hey there is no observation number ' recno ;
run;

Of like they said you could just check the _ERROR_ variable and assume that means you tried to read something that wasn't there and stop.

data want ;
  set sashelp.class point=_n_ ;
  if _error_ then do;
    put 'Somthing happened.' _n_=; 
    stop; 
  end;
  else output;
run;

 

View solution in original post


All Replies
Solution
‎09-09-2017 01:05 PM
Super User
Super User
Posts: 7,844

Re: end-of-file with direct access to read data.

[ Edited ]
Posted in reply to arjunascagnetto

Looks like a really poor question.

If you are using a loop of some sort with POINT= option on SET to do access you could also use NOBS= option so you know how many observations there are.

data want;
  do p=1 to nobs;
    set have point=p nobs=nobs;
    output;
  end;
  stop;
run;

If you are reading a list of observation numbers from another dataset then you could also use NOBS to check before even trying to use POINT=.  No need for a STOP statement in this case as the step will stop when it reads past the end of the dataset RECORD_LIST.

data want ;
   set record_list;
   if 1 <= recno <= nobs then set have point=recno nobs=nobs;
   else put 'Hey there is no observation number ' recno ;
run;

Of like they said you could just check the _ERROR_ variable and assume that means you tried to read something that wasn't there and stop.

data want ;
  set sashelp.class point=_n_ ;
  if _error_ then do;
    put 'Somthing happened.' _n_=; 
    stop; 
  end;
  else output;
run;

 

Super User
Posts: 22,820

Re: end-of-file with direct access to read data.

Posted in reply to arjunascagnetto

Is this the Advanced Guide? The Base certification doesn't cover EOF/POINTER access the last time I checked...though I could easily be mistaken.

 

 

Occasional Contributor
Posts: 5

Re: end-of-file with direct access to read data.

Guide for base programmer certification.
Super User
Posts: 12,996

Re: end-of-file with direct access to read data.

Posted in reply to arjunascagnetto

arjunascagnetto wrote:

Hi,

 

in the Certification book (Base) i found this question:

 

There is no end-of-file condition when you use direct access to read data, so how can
your program prevent a continuous loop?
a. Do not use a POINT= variable.
b. Check for an invalid value of the POINT= variable.
c. Do not use an END= variable.
d. Include an OUTPUT statement.

 

with this answer

 

Correct answer: b
To avoid a continuous loop when using direct access, either include a STOP
statement or use programming logic that executes a STOP statement when the data
step encounters an invalid value of the POINT= variable. If SAS reads an invalid
value of the POINT= variable, it sets the automatic variable _ERROR_ to 1. You can
use this information to check for conditions that cause continuous looping.

 

I understand the use of STOP statement but i can't realy figure out what the "invalid value" can be used. Can someone explain better or with other words what does this answer mean ? And maybe post some example code ?

 

thanks,

Arjuna


Any negative value or value of point greater than the number of records in the data set would be invalid. So if your program sets the point variable to a value outside the range of records in the data set you get into problems.

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 279 views
  • 1 like
  • 4 in conversation