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

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

4 REPLIES 4
ballardw
Super User

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

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9
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?
ballardw
Super User

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;

 

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

thanks a  lot!

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

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