Pages

Tuesday, January 22, 2019

SAS Example : Split dataset into n equally sized datasets


Considering the cars dataset available in sashelp and splitting it to 10 equally sized datasets.


Step1:

Creating a row number using _n_ system variable option.
           
               data sample;
                   set sashelp.cars;
                        n=_n_;
               run;


Step2:

The below macro splits the data into n equally sized datasets. n is passed as a parameter to the macro.
  • Macro name : split
  • creating a cnt macro variable which holds the no. of observation count of the cars dataset. Cars dataset has 428 observations, therefore cnt = 428 
  • number macro variable holds the no. of observations to be kept in each split dataset. In the below macro, cars dataset is split into 10 datasets. therefore number = 428/10 ~ 43 observations 
  • DO loop to iterate 10 times to split the cars dataset into 10 equally sized datasets. The split datasets are named as sample_1 to sample_10.

%macro split(divide);

         proc sql;
               select max(n) into :cnt from sample;
         quit;

         data split;
               num=ceil(&cnt/&divide);
         run;

         proc sql;
               select num into :number from split;
         quit;

         %do i=1 %to ÷

                data sample_&i;
                     set sample;
                          if n>=((&i-1)*&number)+1 and n<=(&i*&number);
                run;

        %end;

%mend split;

%split(10);

Code:



Output:



No comments:

Post a Comment