BookmarkSubscribeRSS Feed
jbs23
Calcite | Level 5

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

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

Kurt_Bremser
Super User

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?

jbs23
Calcite | Level 5

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

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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. 

Kurt_Bremser
Super User

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;

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 5 replies
  • 745 views
  • 0 likes
  • 3 in conversation