BookmarkSubscribeRSS Feed
arpit
Calcite | Level 5
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
5 REPLIES 5
data_null__
Jade | Level 19
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]
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
Vasile01
Fluorite | Level 6
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;
deleted_user
Not applicable
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
Ksharp
Super User
[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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 1014 views
  • 0 likes
  • 6 in conversation