Jeff
Jeff

Reputation: 29

Copying a file exatly the same as the original

I have a .txt file, e.g. the following .txt file(txt_file) has 4 rows, 2nd and 3rd rows have spaces in the front. I am trying to use file statement to make a copy. However, the 2nd and 3rd rows' spaces disappear.

How can I keep the spaces, or an alternative way to keep the spaces, to make sure the copy is the exact same as the original one.

abcd efg hijklmn opqrst
     6bho jghvgu
  3hnopkop
1bji njpb 78nhiobhio

data _null_;
 set txt_file;
  file "c:/txt_file_dup" notitles noprint   ;
run;

Upvotes: 1

Views: 274

Answers (2)

Richard
Richard

Reputation: 27508

Use the FCOPY function.

filename source 'original.txt';
filename destin 'original(copy).txt';

%let rc = %sysfunc(FCOPY(source,destin));

The filenames can be web addresses as well as local file system.

Upvotes: 0

Tom
Tom

Reputation: 51566

The easiest way to copy text is to use the _INFILE_ automatic variable.

data _null_;
   infile 'original_file.txt';
   file 'new_copy.txt' ;
   input;
   put _infile_;
run;

To write data from a dataset to a text file use the PUT statement. To preserve leading spaces in the data use $CHAR or $VARYING format in the PUT statement. So assuming you have dataset named HAVE with one character variable named LINE that is of length 80 you might do something like:

data _null_;
  set have;
  file 'want.txt' ;
  put line $char80.;
run;

or to not write trailing blanks use $VARYING format instead. To use that you need a variable with the number of bytes to write.

data _null_;
  set have;
  file 'want.txt' ;
  len=lengthn(line);
  put line $varying80. len;
run;

Upvotes: 1

Related Questions