BookmarkSubscribeRSS Feed
gurpreetkaur
Fluorite | Level 6

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

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
Astounding
PROC Star

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;

Ksharp
Super User
proc surveyselect data=sashelp.air out=want group=4;
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

Ksharp
Super User

ME TOO ! 

remember and forget something everyday .

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1171 views
  • 3 likes
  • 4 in conversation