DATA Step, Macro, Functions and more

Split datasets into multiple small datasets

Reply
Contributor
Posts: 28

Split datasets into multiple small datasets

Hi all, 

 

I want to understand how can I split a dataset into multiple datasets in different percentages. 

 

Say I have a dataset Have and I want to split it into first  20% and rest equally into 3 parts.

 

Thanks in advance 

Valued Guide
Posts: 580

Re: Split datasets into multiple small datasets

Posted in reply to adityaa9z

I am sure that this question was asked many times before and almost always somebody answered: "why? Splitting data is a good idea in very, very few cases, only."

 

Some hints:

  • the set-statement has the options "nobs" - useful to know how many obs are in a dataset
  • the automatic variable _n_ is necessary to trigger the right output-statement
  • because the output-statement does not allow dynamic naming of the dataset to write to, you have to code one output-statement for each dataset to be created
  • each dataset has to be named in the data-statement
PROC Star
Posts: 1,286

Re: Split datasets into multiple small datasets

Posted in reply to adityaa9z

I agree with @andreas_lds, hardly ever a good idea.

 

However if you want to do it anyways, here is a nice example of how

 

Creating Quantile Groups

Super User
Super User
Posts: 9,599

Re: Split datasets into multiple small datasets

Posted in reply to adityaa9z

First off, its generally better to not do so, assigning groups within the one dataset creates easier programming down the line.

If you wish to continue then surveyselect should be able to do it:

https://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#surveyselect_toc.h...

 

Which was mentioned in:

https://communities.sas.com/t5/Base-SAS-Programming/Subsetting-Dataset-based-on-number-of-observatio...

Respected Advisor
Posts: 3,066

Re: Split datasets into multiple small datasets

I feel like joining the party!

 

Don't split your data sets!

 

 

--
Paige Miller
Super User
Posts: 10,787

Re: Split datasets into multiple small datasets

[ Edited ]
Posted in reply to adityaa9z
data x1 x2 x3 x4;
 set sashelp.air;
 call streaminit(123456789);
 _id_=rand('table',0.2,0.8/3,0.8/3);
 if _id_=1 then output x1;
  else if _id_=2 then output x2;
    else if _id_=3 then output x3;
      else if _id_=4 then output x4;
run;
Ask a Question
Discussion stats
  • 5 replies
  • 83 views
  • 0 likes
  • 6 in conversation