DATA Step, Macro, Functions and more

creat 3 dataset from 1 dataset

Reply
Contributor
Posts: 45

creat 3 dataset from 1 dataset

I have a data set having below mention colum and data.

month pay bonus
jan 20000 1000
jan 10000 1100
jan 5000 2000
feb 4000 3000
feb 5000 4000
mar 4800 600
mar 5000 800

I want to create 3 different data set jan feb mar like below

jan
jan 20000 1000
jan 10000 1100
jan 5000 2000
feb
feb 4000 3000
feb 5000 4000
mar
mar 4800 600
mar 5000 800

please help me
Respected Advisor
Posts: 3,799

Re: creat 3 dataset from 1 dataset

With data grouped as in your example.

[pre]
data all;
input month$ pay bonus;
cards;
jan 20000 1000
jan 10000 1100
jan 5000 2000
feb 4000 3000
feb 5000 4000
mar 4800 600
mar 5000 800
;;;;
run;
data _null_;
set all;
by month notsorted;
if _n_ eq 1 then do;
declare hash d(ordered:'a');
d.definekey('_n_');
d.definedata('month','pay','bonus');
d.definedone();
end;
if first.month then d.clear();
d.add();
if last.month then d.output(dataset:month);
run;
[/pre]
Super Contributor
Super Contributor
Posts: 3,174

Re: creat 3 dataset from 1 dataset

Posted in reply to data_null__
If you need to generate a SAS file with unique/distinct values for a variable, consider using PROC SORT with NODUPKEY and a specific BY statement variable list.

Scott Barry
SBBWorks, Inc.
Occasional Contributor
Posts: 14

Re: creat 3 dataset from 1 dataset

Hi,

You could read about conditionally writing observations in more than one dataset at:
http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/a001343763.htm
Warm regards,
Vasile


data database;
input month $3. pay bonus;
cards;
jan 20000 1000
jan 10000 1100
jan 5000 2000
feb 4000 3000
feb 5000 4000
mar 4800 600
mar 5000 800
;


data jan feb mar;
set database;
select (month);
when ('jan') output jan;
when ('feb') output feb;
when ('mar') output mar;
otherwise;
end;
run;
N/A
Posts: 0

Re: creat 3 dataset from 1 dataset

Hello,

The powerful call execute routine can solve this issue:

[pre]

data _null_;
set in end=last;

call symput ('mon',month);

if _n_ eq 1 then call execute ('data jan feb mar;');

call execute (cats('set in (obs=',_N_,' firstobs=',_N_,');' ));

call execute ('output &mon;');

if last then call execute('run;');

run;
[/pre]

Marius
Super User
Posts: 10,046

Re: creat 3 dataset from 1 dataset

[pre]
data temp;
input month $ pay bonus;
cards;
jan 20000 1000
jan 10000 1100
jan 5000 2000
feb 4000 3000
feb 5000 4000
mar 4800 600
mar 5000 800
;
run;

data _null_;
declare hash hh(ordered: 'a');
declare hiter ff('hh');
hh.definekey('month');
hh.definedata('month','_hh');
hh.definedone();

declare hash _hh(ordered: 'a');
do until(last);
count+1;
set temp end=last;
rc=hh.find();
if rc ne 0 then do;
_hh= _new_ hash(ordered: 'a');
_hh.definekey('count');
_hh.definedata('month','pay','bonus');
_hh.definedone();
hh.replace();
end;
_hh.replace();
end;

rc=ff.first();
do while(rc =0);
_hh.output(dataset: month);
rc=ff.next();
end;
run;
[/pre]



Ksharp
Ask a Question
Discussion stats
  • 5 replies
  • 198 views
  • 0 likes
  • 6 in conversation