Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_chop.sas File Reference

Splits a file of ANY SIZE by reference to a search string. More...

Go to the source code of this file.

Detailed Description

Provide a fileref and a search string to chop off part of a file.

Works by reading in the file byte by byte, then marking the beginning and end of each matched string, before finally doing the chop.

Choose whether to keep the FIRST or the LAST section of the file. Optionally, use an OFFSET to fix the precise chop point.

Usage:

%let src="%sysfunc(pathname(work))/file.txt";
%let str=Chop here!;
%let out1="%sysfunc(pathname(work))/file1.txt";
%let out2="%sysfunc(pathname(work))/file2.txt";
%let out3="%sysfunc(pathname(work))/file3.txt";
%let out4="%sysfunc(pathname(work))/file4.txt";

data _null_;
  file &src;
  put "startsection&str.endsection";
run;

%mp_chop(&src, matchvar=str, keep=FIRST, outfile=&out1)
%mp_chop(&src, matchvar=str, keep=LAST, outfile=&out2)
%mp_chop(&src, matchvar=str, keep=FIRST, matchpoint=END, outfile=&out3)
%mp_chop(&src, matchvar=str, keep=LAST, matchpoint=END, outfile=&out4)

filename results (&out1 &out2 &out3 &out4);
data _null_;
  infile results;
  input;
  list;
run;

Results:

  • startsection
  • Chop here!endsection
  • startsectionChop here!
  • endsection

For more examples, see mp_chop.test.sas

Parameters
[in]infileThe QUOTED path to the file on which to perform the chop
[in]matchvar=() Macro variable NAME containing the string to split by
[in]matchpoint=(START) Valid values:
  • START - chop at the beginning of the string in matchvar.
  • END - chop at the end of the string in matchvar.
[in]offset=(0) An adjustment to the precise chop location, by by reference to the matchpoint. Should be a positive or negative integer.
[in]keep=(FIRST) Valid values:
  • FIRST - keep the section of the file before the chop
  • LAST - keep the section of the file after the chop
[in]mdebug=(0) Set to 1 to provide macro debugging
[out]outfile=(0) Optional QUOTED path to the adjusted output file (avoids overwriting the first file).

SAS Macros

Related Macros

Version
9.4
Author
Allan Bowe

Definition in file mp_chop.sas.