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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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