DATA Step, Macro, Functions and more

Subsetting Dataset based on number of observations

Reply
Occasional Contributor
Posts: 19

Subsetting Dataset based on number of observations

Hi Experts,

 

I need to create an automation system for creating subsets based on number of observations present in master dataset. For example: if my master dataset has 100 observations, I need to create 4 subsets each having 25 observations. I am unable to think of a logic. If my master dataset has 200 observations then I need to create 4 datasets of 50 observations each. Please help me in writing this logic.

 

Thank you,

Gurpreet

Super User
Super User
Posts: 9,599

Re: Subsetting Dataset based on number of observations

Posted in reply to gurpreetkaur

Get number of observations, then run code, e.g:

proc sql noprint;
  select nobs/4
  into     :cnt
  from    dictionary.tables
  where  libname="<yourlib>"
     and   memname="<yourdataset>";
quit;

data want1 want2 want3 want4;
  set <yourlib>.<yourdataset>;
  if _n_ < (1 * &cnt.) then output want1;
  else if _n_ < (2 *&cnt.) then output want2;
  ...;
run;
Super User
Posts: 6,781

Re: Subsetting Dataset based on number of observations

Posted in reply to gurpreetkaur

If your data source is a SAS data set, you can pull the number of observations and divide them up in the same step:

 

data subset1 subset2 subset3 subset4;

set have nobs=_nobs_;

if _n_ <= _nobs_ / 4 then output subset1;

else if _n_ <= _nobs_ / 2 then output subset2;

else if _n_ <= _nobs_ * 3 / 4 then output subset3;

else output subset4;

run;

Super User
Posts: 10,784

Re: Subsetting Dataset based on number of observations

Posted in reply to gurpreetkaur
proc surveyselect data=sashelp.air out=want group=4;
run;
Super User
Super User
Posts: 9,599

Re: Subsetting Dataset based on number of observations

Nice, I always forget the surveyselect function - even though I posted on it some time back myself Smiley Surprised)

Super User
Posts: 10,784

Re: Subsetting Dataset based on number of observations

[ Edited ]

ME TOO ! 

remember and forget something everyday .

Ask a Question
Discussion stats
  • 5 replies
  • 63 views
  • 3 likes
  • 4 in conversation