BookmarkSubscribeRSS Feed
Rohit12
Obsidian | Level 7

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 

17 REPLIES 17
gamotte
Rhodochrosite | Level 12

Hi,

 

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;
Rohit12
Obsidian | Level 7

 what i want first four observation should go to want1 dataset  and then next four to want2 and so on 

gamotte
Rhodochrosite | Level 12

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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Seeing as you invoked my macros aversions Smiley Happy

 

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.

gamotte
Rhodochrosite | Level 12
Didn't you mean dictionary.columns rather than sashelp.class ?
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Nah, I meant sashelp.vtable (corrected now) or you could use dictionary.tables.  

Rohit12
Obsidian | Level 7
I have a dataset which has 1000 rows and I want 4 different datasets to be created with each having 250 rows for ex want1 should have rows from 1 to 250 and want 2 to have from 251 to 500

will your code wok ?
Rohit12
Obsidian | Level 7
this code will work for sashelp.class but what If my dataset is having 1000 rows then it will create 250 datasets .But what I want is 4 datasets only
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

gamotte
Rhodochrosite | Level 12
@Ksharp's solution seems to be what you want.

In order to get correct answers, you should put a little more effort in explaining exactly what is the desired output because :
"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"
and
" what i want first four observation should go to want1 dataset and then next four to want2 and so on"
seem to be two completely different demands.

"want1 should have rows from 1 to 250 and want 2 to have from 251 to 500" is a precision you should have made in your first post.

You'll get better answers quicker and it will avoid unnecessary work to contributors.
Patrick
Opal | Level 21

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;
Ksharp
Super User
%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;
Ksharp
Super User

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;

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
  • 17 replies
  • 1193 views
  • 5 likes
  • 9 in conversation