Reputation: 87
I have a SAS dataset that I need to split into smaller datasets of 200 (or less). The dataset will vary in size but the rule of maximum rows of 200 will always apply. For example, the dataset is currently 637 rows, I want to split this dataset into 3 sets of 200 and then the remaining 37 rows go into their own dataset, creating 4 datasets.
Can anyone think of a way to do this?
The dataset has only 3 columns: ID, email and date.
Upvotes: 0
Views: 2029
Reputation: 12909
One way is through a macro. This code will split all datasets up into evenly-spaced data. The arguments of the macro are:
Code:
%macro split(data=, out=out, max=);
/* Get the number of observations */
%let dsid = %sysfunc(open(&data.) );
%let nobs = %sysfunc(attrn(&dsid., nlobs) );
%let rc = %sysfunc(close(&dsid.) );
/* Calculate the total number of datasets needed */
%let n_datasets = %sysevalf(&nobs./&max., ceil);
%do i = 1 %to &n_datasets.;
/* Calculate start/end observations */
%let start = %sysevalf(&max*&i. - &max. + 1);
%let end = %sysevalf(&max.*&i.);
data &out.&i.;
set &data.(firstobs=&start. obs=&end.);
run;
%end;
%mend;
Example:
%split(data=sashelp.cars, out=cars, max=200);
Upvotes: 1