I am having n observation in a dataset.i want equal observation into 4 datasets?
suppose i have 20 obs in a dataset?
i want 1 to 5 obs in one dataset and 6 to 10 in one dataset and 11 to 15 in one dataset and 16 to 20 obs in one dataset?
please only code required?
Here is a code I found and seems to work fine :
%macro split2(num,tab_in);
data _null_;
if 0 then set &tab_in nobs=count;
call symput('numobs', put(count, 8.));
run;
%let n=%sysevalf(&numobs/&num, ceil);
data
%do J=1 %to #
SAMPLE_&J %end;
;
set &tab_in;
%do I=1 %to #
if %eval(&n*(&i-1)) <_n_ <=%eval(&n*&I) then output SAMPLE_&I;
%end;
run;
%mend split2;
/*Sample test*/
data have;
do i=1 to 15;
output;
end;
run;
%split2(4,have);
Here is a code I found and seems to work fine :
%macro split2(num,tab_in);
data _null_;
if 0 then set &tab_in nobs=count;
call symput('numobs', put(count, 8.));
run;
%let n=%sysevalf(&numobs/&num, ceil);
data
%do J=1 %to #
SAMPLE_&J %end;
;
set &tab_in;
%do I=1 %to #
if %eval(&n*(&i-1)) <_n_ <=%eval(&n*&I) then output SAMPLE_&I;
%end;
run;
%mend split2;
/*Sample test*/
data have;
do i=1 to 15;
output;
end;
run;
%split2(4,have);
You can see that there are simple versions and there are complex (but more flexible) versions. If you use a macro-based approach, here are a couple of changes to simplify and speed up the program.
The macro contains:
%do I=1 %to #
if %eval(&n*(&i-1)) <_n_ <=%eval(&n*&I) then output SAMPLE_&I;
%end;
This would work equally well:
if _n_ <= &n then output SAMPLE_1;
%do I=2 %to #
else if _n_ <= %eval(&n*&I) then output SAMPLE_&I;
%end;
This code will work.
data want1 want2 want3 want4;
set have nobs=nrecs;
select (ceil(_n_/(nrecs/4)));
when (1) output want1;
when (2) output want2;
when (3) output want3;
otherwise output want4;
end;
run;
It uses the SET statement option NOBS=, which stores the number of obs in HAVE in variable nrecs. And it also uses the automatic variable _N_ which is the "iteration" number in the data step - equivalent to the obs number in this program.
Now say nrecs=100, then nrecs/4=25 and _n_/(nrecs/4) ranges from .004, .008,... ,1, 1.004, 1.008, … 4.. And the CEIL functions rounds up to 1,2,3, or 4..
Extra observations are written to WANT4, then WANT3, then WANT2.
Maybe something like this
data have;
do i=1 to 20;
output;
end;
run;
data want1 want2 want3 want4;
set have;
if _N_<=5 then output want1;
else if 5<_N_<=10 then output want2;
else if 10<_N_<=15 then output want3;
else if 15<_N_<=20 then output want4;
run;
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 16. 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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: