Sunday, January 8, 2023

SAS Macro for Splitting Datasets into Multiple Parts

options symbolgen mprint mlogic mcompilenote=all;

%macro split (dsn=, parts=);
%if &parts =  %then %do;
   %put ERROR: 'parts' is not specified.;
   %put Use the 'parts=' option to specify the number of parts to split the dataset into.;
   %return;
%end;
%if &dsn = %then %do;
   %put ERROR: 'dsn' is not specified.;
   %put Use the 'dsn=' option to specify the dataset to split.;
   %return;
%end;
proc sql noprint;
select count(*) into :n trimmed from &dsn;quit;
%let num=%sysfunc(inputn(&n,8.));
%let fin=%sysfunc(round(&num/&parts));
%put &fin;
%let obsm=0;
%do i=1 %to (&parts);
%let fobs=%eval(&obsm+1);
%let obsm=%eval(&fobs-1+&fin);

data ds&i;
set &dsn (firstobs=&fobs obs=%sysfunc(min(&obsm,&num)));
run;
%end;
run;
%mend;

Example:

  1. %split (dsn=sashelp.cars, parts=4);
  2. %split (dsn=, parts=4);
  3. %split (dsn=sashelp.cars, parts=);



SAS Macro for Splitting Datasets into Multiple Parts

options symbolgen mprint mlogic mcompilenote=all; %macro split (dsn=, parts=); %if &parts =  %then %do;    %put ERROR: 'parts' i...