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

I'm trying to extract a subset of records from a fixed-width data file using an infile statement and looking for the substring '2018' at a specific location.  My goal is to output the matching records into the same fixed-width file format from which they were extracted.  However, the original file contains records in a fixed-width 4000 character string with the last 989 characters as padded spaces.  No matter what I do I can't seem to retain the padded spaces at the end.  The resulting data file (temp.txt) has records that end at position 3019.  How do I retain the padded spaces in the new file?

 

To be clear, I can import both the original and created fixed-width files into SAS using the same import code.  That's not my goal.  My goal is to retain the padded spaces so the file will be recognized as the acceptable file format by the system to which it is submitted.

 

Thanks!

 

data test;
missing; infile "C:\TEMP\fixed_width_4000.txt" lrecl=4000 truncover; input lines $1-4000; if substr(lines,1,4) eq '2018'; run; Options noQuoteLenMax; data _null_; set test; file "C:\TEMP\temp.txt" lrecl=4000 n=4 MOD; put lines; run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

First thing, congratulations on not destroying your original file. I was afraid you going to use the same in and out file name with potential disaster.

 

Simplest might be to add the PAD option to your file statement.

data _null_;
	set test;
	file "C:\TEMP\temp.txt" lrecl=4000 n=4 MOD PAD;
	put lines;
run;

I assume that you are using MOD for testing as your final result doesn't want the previous short lines plus the correct lines.

 

View solution in original post

3 REPLIES 3
ballardw
Super User

First thing, congratulations on not destroying your original file. I was afraid you going to use the same in and out file name with potential disaster.

 

Simplest might be to add the PAD option to your file statement.

data _null_;
	set test;
	file "C:\TEMP\temp.txt" lrecl=4000 n=4 MOD PAD;
	put lines;
run;

I assume that you are using MOD for testing as your final result doesn't want the previous short lines plus the correct lines.

 

Tom
Super User Tom
Super User

Use the $CHAR informat/format to preserve leading spaces.

data _null_;
  infile "C:\TEMP\fixed_width_4000.txt" lrecl=4000 truncover;
  file "C:\TEMP\updated.txt" lrecl=4000 ;
  input lines  $char4000.; 
  if substr(lines,1,4) eq '2018';
  put lines $char4000.;
run;

Or just use the _INFILE_ automatic variable.

data _null_;
  infile "C:\TEMP\fixed_width_4000.txt" lrecl=4000 truncover;
  file "C:\TEMP\updated.txt" lrecl=4000 ;
  input ;
  if substr_infile_,1,4) eq '2018';
  put _infile_;
run;

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2502 views
  • 3 likes
  • 4 in conversation