dataset creation

Reply
New User
Posts: 1

dataset creation

 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 ? 

Super User
Posts: 19,850

Re: dataset creation

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?
Frequent Contributor
Posts: 130

Re: dataset creation

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;
Super User
Posts: 19,850

Re: dataset creation

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;
Ask a Question
Discussion stats
  • 3 replies
  • 287 views
  • 1 like
  • 3 in conversation