avoid sas removing trailing space

60 Views Asked by At

I get a txt file using infile. Each row has a length of 261, but I only need the first 162. The problem is that the last 5 characters are blanks, but I need to keep them to export the entire variable later (after several steps and checks).

data t1;
    infile "&path./&file_name..txt" lrecl=261 firstobs=2;
    input   REG_FULL     $   1-261
        REG_ENVIO    $   1-162
        CODNUM       $ 129-138
        CONTRATO     $  23-31
        FECHA_ENVIO  $  32-39
        ;
run;
data t1_check; set t1; kk=length(REG_ENVIO); run; /*here I realized it was trucated to 157*/
1

There are 1 best solutions below

0
Tom On

SAS character variables are fixed length and always padded to their full length with spaces. So spaces are never removed from the data.

The LENGTH() function just tells you the location of the last character in the variable that is not a space.

I expect you mean you want to write a new TEXT file. SAS can write almost any kind of text file.

So if you want to write all 261 characters then do that.

data _null_;
  set have;
  file 'new_text_file' ;
  put reg_full $char261. ;
run;

And there are other ways. For example you can use the PAD option on the FILE statement when writing a text file and SAS will automatically pad all output lines with spaces to the full length defined by the LRECL= option of the FILE statement.

A more likely source of trouble with your posted code is removal of leading spaces. The normal $ informat (what is implied by your use of column style input) will remove leading spaces. To preserve the leading spaces you would need to read the field using the $CHAR informat instead. So switch to reading the fields using FORMATTED mode input instead.

input 
 /* REG_FULL     $   1-261 */
   @1 REG_FULL $char261.