BookmarkSubscribeRSS Feed
aditri
Calcite | Level 5

 i have dataset of 6 variabes and 100 observations,if i want to create 5 datasets with minimum 26 observations from the existing dataset...how i can generate ? 

3 REPLIES 3
Reeza
Super User
Generally not considered a good idea to split your dataset...You could use proc surveyselect so that you get random selections but that generates a single dataset that you'll have to split. Do you have any other rules besides a minimum of 26 variables?
dcruik
Lapis Lazuli | Level 10

I would agree with @Reeza if you are trying to gather sample data sets, I would suggest utilizing the surveyselect procedure, as it will gather random samples of data.  You could do something like the following to ensure you don't have the same observations in each of the six data sets:

proc surveyselect data=have out=want1
method=srs n=26;
run;

proc sql;
create table remain1 as
select A.*
from have A left join want1 B
on (A.ID=B.ID)
Where B.ID="";
quit;

proc surveyselect data=remain1 out=want2
method=srs n=26;
run;

proc sql;
create table remain2 as
select A.*
from remain1 A left join want2 B
on (A.ID=B.ID)
Where B.ID="";
quit;
.
.
.

However, if you want to just create six different data sets with 26 observations in each (sequentially) the following code would do it:

data want1;
set have nobs=26;
run;

data want2;
set have (firstobs=27 obs=26);
run;

data want3;
set have (firstobs=54 obs=26);
run;

data want4;
set have (firstobs=81 obs=26);
run;

data want5;
set have (firstobs=108 obs=26);
run;

data want6;
set have (firstobs=135 obs=26);
run;
Reeza
Super User
That last step is a bit ugly, using OUTPUT with multiple data sets is a bit cleaner. I still stand by not using it.

data out1 out2 out3 out4 out5 out6;
set sashelp.heart;
if ceil(_n_/26) =1 then output out1;
else if ceil(_n_/26) =2 then output out2;
else if ceil(_n_/26) =3 then output out3;
else if ceil(_n_/26) =4 then output out4;
else if ceil(_n_/26) =5 then output out5;
else if ceil(_n_/26) =6 then output out6;
run;

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
  • 3 replies
  • 632 views
  • 1 like
  • 3 in conversation