Skip to content

Instantly share code, notes, and snippets.

@statgeek
Created August 27, 2017 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save statgeek/f4eaec3e5ec9666865e663c9e225f09d to your computer and use it in GitHub Desktop.
Save statgeek/f4eaec3e5ec9666865e663c9e225f09d to your computer and use it in GitHub Desktop.
SAS - split dataset into subsets based on number of records
*create a sample data set;
data have;
do i=1 to 7000;
x=rand('bernoulli', 0.4);
output;
end;
run;
*Set group size;
%let group_size = 1000;
*Creates a data set, but not required can be a data _null_ data step;
data want;
set have end=_eof nobs=_nobs;
length list_names $32000.;
retain group 0 list_names;
prev_group=group;
if mod(_n_, &group_size)=0 then group+1;
if group ne prev_group or _eof then do;
if _eof then list_names = catx(", ", list_names, put(x, 8.2));
call symputx("list"||put(group, z2.), list_names, 'g');
call missing(list_names);
end;
list_names = catx(", ", list_names, put(x, 8.2));
run;
%put &list01;
%put &list04;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment