hi team,
Here im having 3lakhs 30 k records in dataset i want to divide them equally into n number of datasets.
Please looking for code many thanks
@MSK4 wrote:
hi team,
Here im having 3lakhs 30 k records in dataset i want to divide them equally into n number of datasets.
Please looking for code many thanks
You haven't said anything about the data except its size. If the data are already in random order and there is no "group" identifier then this is the simplest way, assuming you want 6 datasets:
data data1 data2 data3 data4 data5 data6;
set have;
select (mod(_n_-1,6)+1);
when (1) output data1;
when (2) output data2;
when (3) output data3;
when (4) output data4;
when (5) output data5;
when (6) output data6;
end;
run;
If you want to make the number of datasets a parameter, then you could:
%let ndatasets=6;
filename tmp temp ;
data _null_;
file tmp;
length statement $200;
statement='DATA ';
do i=1 to &ndatasets;
statement=catx(' ',statement,cats('data',i));
end;
put statement ';';
put 'set have;';
put "select (mod(_n_-1,&ndatasets)+1);";
do i=1 to &ndatasets;
put "when(" i ") output data" i ";" ;
end;
put "end;" / "run;" ;
run;
%include tmp / source2;
The program writes sas code to a temporary file (sas will delete it at the end fo the session). Then the %include statement calls it for execution. The "/source2" option tells sas to print the included statements in the sas log.
@MSK4 wrote:
hi team,
Here im having 3lakhs 30 k records in dataset i want to divide them equally into n number of datasets.
Please looking for code many thanks
Not sure what a "lakhs" is, but assuming it is just some unit of counting this is a common question.
Here is link to a previous question about splitting datasets by the number of observations that has an answer:
@MSK4 wrote:
hi team,
Here im having 3lakhs 30 k records in dataset i want to divide them equally into n number of datasets.
Please looking for code many thanks
You haven't said anything about the data except its size. If the data are already in random order and there is no "group" identifier then this is the simplest way, assuming you want 6 datasets:
data data1 data2 data3 data4 data5 data6;
set have;
select (mod(_n_-1,6)+1);
when (1) output data1;
when (2) output data2;
when (3) output data3;
when (4) output data4;
when (5) output data5;
when (6) output data6;
end;
run;
If you want to make the number of datasets a parameter, then you could:
%let ndatasets=6;
filename tmp temp ;
data _null_;
file tmp;
length statement $200;
statement='DATA ';
do i=1 to &ndatasets;
statement=catx(' ',statement,cats('data',i));
end;
put statement ';';
put 'set have;';
put "select (mod(_n_-1,&ndatasets)+1);";
do i=1 to &ndatasets;
put "when(" i ") output data" i ";" ;
end;
put "end;" / "run;" ;
run;
%include tmp / source2;
The program writes sas code to a temporary file (sas will delete it at the end fo the session). Then the %include statement calls it for execution. The "/source2" option tells sas to print the included statements in the sas log.
Why do you want to divide a rather small dataset at all?
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.