DATA Step, Macro, Functions and more

creating SAS dataset

Reply
Contributor
Posts: 35

creating SAS dataset

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 

Regular Contributor
Posts: 194

Re: creating SAS dataset

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;
Contributor
Posts: 35

Re: creating SAS dataset

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

Regular Contributor
Posts: 194

Re: creating SAS dataset

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

Re: creating SAS dataset

[ Edited ]

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.

Regular Contributor
Posts: 194

Re: creating SAS dataset

Didn't you mean dictionary.columns rather than sashelp.class ?
Super User
Super User
Posts: 7,420

Re: creating SAS dataset

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

Contributor
Posts: 35

Re: creating SAS dataset

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 ?
Super User
Posts: 6,966

Re: creating SAS dataset


Rohit12 wrote:


will your code wok ?

TRY.IT.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 35

Re: creating SAS dataset

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
Super User
Super User
Posts: 7,420

Re: creating SAS dataset

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.

Regular Contributor
Posts: 194

Re: creating SAS dataset

@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.
Respected Advisor
Posts: 3,902

Re: creating SAS dataset

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;
Super User
Posts: 9,687

Re: creating SAS dataset

%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;
Super User
Posts: 9,687

Re: creating SAS dataset

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;
Ask a Question
Discussion stats
  • 17 replies
  • 314 views
  • 5 likes
  • 9 in conversation