12-20-2016 04:26 AM
I have a dataset and I want one fourth of observation to go in one dataset and another one fouth to a different dataset and another one fourth to different dataset and so on .How can i do this
12-20-2016 04:33 AM
If there is no particular criterion to select which data goes where, the following program should do what you want.
data want1 want2 want3 want4; set sashelp.class; if mod(_N_,4)=0 then output want1; if mod(_N_,4)=1 then output want2; if mod(_N_,4)=2 then output want3; if mod(_N_,4)=3 then output want4; run;
12-20-2016 04:57 AM
I will go with a macro (sorry RW9)
%macro create_data; proc sql; SELECT ceil(count(*)/4) INTO :nbdatasets FROM sashelp.class; quit; %let datasets=; %do i=1 %to &nbdatasets.; %let datasets=&datasets want&i.; %end; data &datasets.; set sashelp.class; %do i=1 %to &nbdatasets.; if ceil(_N_/4)=&i. then output want&i.; %end; run; %mend; %create_data;
12-20-2016 05:11 AM - edited 12-20-2016 05:47 AM
Seeing as you invoked my macros aversions
data _null_; set sashelp.vtable (where=(libname="WORK" and memname="<yourds>")); do i=1 to nobs by 4; call execute(cats('data want',put(i,best.),'; set work.<yourds> point=',put(i,best.),' obs=4; run;')); end; run;
That being said however, I would really question the need to split your data up in the first place. The question is why? SAS is built around the concept of by group processing, each of the functions are optimised to do thing over groups. Read/write to dataset is one of the biggest resource hogs in most proceses. So why not in your one dataset assign a group value, e.g. a number which starts at 1 and then increment by one each 4 observations. You can then do:
proc means data=have; by group; ...
For example to get a means for each group. Its also far simpler code.
12-20-2016 06:47 AM
12-20-2016 08:13 AM
will your code wok ?
12-20-2016 06:36 AM
Sorry, hard to tell if thats a response to my code, so will add. If you only want four tables then @Patrick has a good solution, you just need to fiddle with the select part. However still better to assign groups in the one dataset, and then use by group processing.
12-20-2016 08:46 AM
12-20-2016 05:17 AM
A variation of @gamotte initial post which splits up the data as per your requirement.
data want1 want2 want3 want4; set sashelp.class; select ( mod(floor((_N_-1)/4),4) ); when (0) output want1; when (1) output want2; when (2) output want3; when (3) output want4; otherwise; end; run;
12-20-2016 05:37 AM
%let dsid=%sysfunc(open(sashelp.class)); %let nobs=%sysfunc(attrn(&dsid,nlobs)); %let n=%sysevalf(&nobs/4,i); %let dsid=%sysfunc(close(&dsid)); data want1 want2 want3 want4; set sashelp.class; if _n_ le &n then output want1; else if _n_ le 2*&n then output want2; else if _n_ le 3*&n then output want3; else output want4; run;
12-20-2016 05:45 AM
If you want randomly select obs ,try proc surveyselect.
proc surveyselect data=sashelp.class groups=4 out=temp; run; data want1 want2 want3 want4; set temp; if groupid=1 then output want1; else if groupid=2 then output want2; else if groupid=3 then output want3; else output want4; run;