Help using Base SAS procedures

reading from buffer

Accepted Solution Solved
Reply
Super Contributor
Super Contributor
Posts: 440
Accepted Solution

reading from buffer

[ Edited ]

Hi,

i have a  file that  i need to read in and  if data in the position 29-31 is not missing then stop or abort the process.

The file has no headers so i am using the code below and after running it i dont see much happening in the logs and i have purposely put data in the position given above. Is this below right? Any one please

 

 data _null_;
 infile file;
 input @;
 if substr(_infile_,29,31) ne'' then stop;
 run;

 

Thx


Accepted Solutions
Solution
‎05-17-2016 04:47 PM
Super User
Posts: 10,550

Re: reading form buffer

[ Edited ]

You will likely get unexpected results with

 if substr(_infile_,29,31) ne'' then stop;

As that substr says to start at position 29 and look at the next 31 positions

You intended

 if substr(_infile_,29,3) ne'' then stop;

to read a length of 3 characters

 

 

The log should tell you how many records were read. If there was something in the first line of the file then the log would show 1 record read. Assuming you had more lines in your file then the count would show the first line with something in that position. So since you say you made sure there was something in the first line then the program stops on the first line.

 

Note, you could read that position directly:

 

input test 29-31 @;

if test ne '' then stop

View solution in original post


All Replies
Solution
‎05-17-2016 04:47 PM
Super User
Posts: 10,550

Re: reading form buffer

[ Edited ]

You will likely get unexpected results with

 if substr(_infile_,29,31) ne'' then stop;

As that substr says to start at position 29 and look at the next 31 positions

You intended

 if substr(_infile_,29,3) ne'' then stop;

to read a length of 3 characters

 

 

The log should tell you how many records were read. If there was something in the first line of the file then the log would show 1 record read. Assuming you had more lines in your file then the count would show the first line with something in that position. So since you say you made sure there was something in the first line then the program stops on the first line.

 

Note, you could read that position directly:

 

input test 29-31 @;

if test ne '' then stop

Super Contributor
Super Contributor
Posts: 440

Re: reading from buffer

Thanks ballardw
Yes i meant 3 not 31. Thank you!
So i guess instead of aborting the process in such cases i can simply write the bad records to a dataset so in that case i will have access to those records. Right?
Super User
Posts: 10,550

Re: reading from buffer

Yes you could redirect the result to a different data set.

 

data good (drop= LongStr test) bad (keep=LongStr);  /*<= TWO separate data sets that will have different variables*/

   Length LongStr $ 200; /*large enough to hold the longest expected _infile_ result*/

   infile file;

   length test $ 3.;

   input test $ 29-31 @;

   If test ne '' then do;

      output bad;

      input; /* to advance to next record*/

   end;

   Else do;

      Input  <your statements to read the data>;

      <any other statements>

      Output good; /* need to explicitly write to the other set*/

   end;

run;

 

Super Contributor
Super Contributor
Posts: 440

Re: reading from buffer

thanks a  lot!

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 357 views
  • 1 like
  • 2 in conversation