DATA Step, Macro, Functions and more

Read only files shorter than some lenght in plain text (.txt)

Reply
Occasional Contributor
Posts: 7

Read only files shorter than some lenght in plain text (.txt)

Hi everyone!!

 

In order to find errors what i want to do is to read only the files on the text plain (.txt) that have a lenght of less than 46.

I thought about first load all the lines with his input of the variables, and then create one conditional with sums of lenght of all variables but i guess there is a easy and shorter way.

 

 

 

Thanks for the help!!

 

ps; sorry for my english

Super User
Super User
Posts: 9,227

Re: Read only files shorter than some lenght in plain text (.txt)

Sorry, not a clue what you are on about.  You mean you have a text file, and you want to read it line by lin and if the line is less than 46 characters you want to increment a counter?  If so:

data want;
  infile "abc.txt";
  input;
  retain total;
  if lengthn(_input_) < 46 then total=sum(total,1);
run;

Super User
Posts: 9,612

Re: Read only files shorter than some lenght in plain text (.txt)

To help in defining your issue:

 

- you have a bunch of text files, named *.txt, in a certain location (directory)

- you want to find files that have a record (line) length of less than 46

- do you want to flag a file if one line is less than 46, or all lines?

- as a corollary, do those files all have fixed-length records (lines), or is line length variable within each file?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 7

Re: Read only files shorter than some lenght in plain text (.txt)

Sorry   and 

 

 

@1 &cursoacademico 4.
		@5 &tipodocumento 1.
		@6 &documento $ 9.
		@15 &sexo 1.
		@16 &grupoedad 2.
		@18 &curso 3.
		@21 &centro 8.
		@29 &situacionsocial 1.
		@30 &situacionlaboral 1.
		@31 &entrada $ 8.
		@39 &salida $ 8.;

 

 

Super User
Super User
Posts: 9,227

Re: Read only files shorter than some lenght in plain text (.txt)

So this is your file description:

@1 &cursoacademico 4.
		@5 &tipodocumento 1.
		@6 &documento $ 9.
		@15 &sexo 1.
		@16 &grupoedad 2.
		@18 &curso 3.
		@21 &centro 8.
		@29 &situacionsocial 1.
		@30 &situacionlaboral 1.
		@31 &entrada $ 8.
		@39 &salida $ 8.;

Why the & before the text such as &salida?  That is macro code.  

 

Anyways, back to your problem.  Where do you arrive at the above specification - i.e. each of these variables should be that given length?  If you have that written down, and the file does not conform to that spec then the file should be rejected outright and returned to the sender to get fixed.  You can try to fix it yourself, but then what would happen if it changes next time, or someone else tries to reproduce what you have done or take over your work.  Remember, specification is far more important than coding or anything else, it is the key basis for any work and if that is not correct and acurate nothing else will be. 

Super User
Posts: 9,612

Re: Read only files shorter than some lenght in plain text (.txt)

Before making code flexible, make it run correctly.

Therefore eliminate all macro references; there is absolutely no sense engaging in macro programming when you haven't mastered basic SAS data step code yet.

 

If you want to flag single records on length, use this construct:

data want;
infile "name_of_file" length=lvar;
input
  /* your variable list here */
;
if lvar > 46
then errorflag = 'Y';
else errorflag = 'N';
run;

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 5 replies
  • 93 views
  • 0 likes
  • 3 in conversation