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

Hello!

 

I have a folder with multiple .sas file and I want to know what the maximum length of a line is per file.

for e.g:

a  255

b 100

c 300

How can i get that?

 

Thanks for your help!

 

Thanks for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Just read the files.  Do you have the list of files? Or do you just want to read all .sas files in one directory?  Here is the later. 

data want;
   length fname filename $256 ;
   infile '/mydir/*.sas' truncover length=ll filename=fname end=eof;
   retain max_length 0 max_non_blank 0 filename ;
   input ;
   if _n_>1 and fname ne lag(fname) then do;
      output;
      max_length=0;
      max_non_blank=0;
   end;
   filename=fname;
   max_length=max(max_length,ll);
   max_non_blank = max(max_non_blank,lengthn(_infile_));
   if eof then output;
run;

 Updated:  Added FILENAME to retain statement.

 

Note: If you are using Windows operating system the '*.sas' will match filenames with extensions that start with .sas, for example SAS datasets use .sas7bdat extension.  So add more conditions on the OUTPUT statements.  Or use some other method to get the list of files.

if lowcase(scan(filename,-1,'.')) = 'sas' then output;

View solution in original post

7 REPLIES 7
Shmuel
Garnet | Level 18

What do you mean by .sas files? - the suffix .sas is given to program files, and in a program you can split lines to shorten them.

 

If you meant sas datasets, then the observations have variable length but you don't need to care of it.

 

The only meaning I can imagine is for maximum length of a char type variable present in multiple datasets. Is that what you are looking for?

ger15xxhcker
Quartz | Level 8

Thanks for your reply. These are sas program files. Text files.

 
Shmuel
Garnet | Level 18

@ger15xxhcker wrote:

Thanks for your reply. These are sas program files. Text files.

 

Depending on number of program files:
If there are few programs you can read them concatenated in one filename statement:

   filename all_p ("<path and file1.sas>"  "<path and file2.sas>" ...);

otherwise define a filename for each program, read it as a text file.

Assign row with max+ supposed length (try $char500.) and check the real length

using function length(row) and retain the max length by:

retain max_length 0;
max_length = max(max_length, length(row));

If you need more help, post your code and log to show what issues you have.

 

ballardw
Super User

@ger15xxhcker wrote:

Thanks for your reply. These are sas program files. Text files.

 

You may need to define what you mean by "line length". Is it actually the text line or a statement ?

Since a SAS statement ends with a ; that does not have to physically be on the same "line" as the rest of the part of a statement this is a potentially important consideration depending on exactly how you intend to use the resultiing information.

 

I have single Input statements that occupy several  100 lines of code because they look liike

input
   var1 $
   var2 
   var3
...
   var240
;

So what would the line "length" be for something like that?

Tom
Super User Tom
Super User

Just read the files.  Do you have the list of files? Or do you just want to read all .sas files in one directory?  Here is the later. 

data want;
   length fname filename $256 ;
   infile '/mydir/*.sas' truncover length=ll filename=fname end=eof;
   retain max_length 0 max_non_blank 0 filename ;
   input ;
   if _n_>1 and fname ne lag(fname) then do;
      output;
      max_length=0;
      max_non_blank=0;
   end;
   filename=fname;
   max_length=max(max_length,ll);
   max_non_blank = max(max_non_blank,lengthn(_infile_));
   if eof then output;
run;

 Updated:  Added FILENAME to retain statement.

 

Note: If you are using Windows operating system the '*.sas' will match filenames with extensions that start with .sas, for example SAS datasets use .sas7bdat extension.  So add more conditions on the OUTPUT statements.  Or use some other method to get the list of files.

if lowcase(scan(filename,-1,'.')) = 'sas' then output;
ger15xxhcker
Quartz | Level 8

Thank you very much! It works, only the file names are blank in the end table for some reason. What can cause this?

Tom
Super User Tom
Super User

@ger15xxhcker wrote:

Thank you very much! It works, only the file names are blank in the end table for some reason. What can cause this?


Try marking FILENAME to be retained by adding a RETAIN statement (or adding it to the exist RETAIN statement). 

 

At the point where you output a record because you have started a new file you want the value of FNAME from the observation before the one where the name changes, so you need to retain it in the separate variable FILENAME.  Note that the variable used in the FILENAME= option of the INFILE statement will not be included in the output dataset.

 

Note that by default the length value will be truncated at 32767.  If you see files with that value then add the LRECL= option to the INFILE statement. But in that case you can no longer test the _INFILE_ automatic variable to see where the last non-blank character is on the line.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 1179 views
  • 3 likes
  • 4 in conversation