Wednesday, July 13, 2022

Split long string: VARCHAR + data _null_

As we know, the maximum length for character variable is 32767. To handle the string which is longer than 32767, we can split it, store in dataset and recomine it. Below is the sample code:
filename source "file_with_long_string";
filename target "file_with_new_long_string";

data source;
    infile source recfm=f lrecl=32000 pad;
    input text $char32000.;
run;

data target;
    length x $ 32767;
    set source;
    x = prxchange('s/old/new/', -1, text);
run;

data _null_;
    length y varchar(5000000);

    do until(eof);
        set target end=eof;
        y = cats(y, x);
    end;

    file target lrecl = 5000000;
    put y;
run;