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

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.

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
  • 4 replies
  • 1068 views
  • 1 like
  • 2 in conversation