Help using Base SAS procedures

HOw can I create 25 datasets in data step

Reply
Contributor sss
Contributor
Posts: 65

HOw can I create 25 datasets in data step

ORG SITE LOC ASSET amount
===================================================
QUANTUM MCA MCA01 TRAN TRAN_HTT 310.00
QUANTUM MCA MCA01 TRAN TRAN_HTT 261.50
QUANTUM MCA MCA01 TRAN TRAN_HTT 273.80
QUANTUM MCA MCA01 TRAN TRAN_HTT 329.30
QUANTUM MCA MCA01 TRAN TRAN_HTT 290.80
QUANTUM MCA MCA01 TRAN TRAN_HTT 324.20
QUANTUM MCA MCA01 TRAN TRAN_HTT 346.30
IESI ATX ATX01 CNC_MIL_400 MIL_LIN_360 322.30
IESI ATX ATX01 CNC_MIL_400 MIL_LIN_360 320.30
IESI ATX ATX01 CNC_MIL_400 MIL_LIN_360 230.00
IESI ATX ATX01 CNC_MIL_400 MIL_LIN_360 367.00
IESI ATX ATX01 CNC_MIL_400 MIL_LIN_360 245.00
IESI ATX ATX01 CNC_MIL_400 MIL_LIN_360 225.00
IESI ATX ATX01 CNC_MIL_400 MIL_LIN_360 398.00
IESI ATX ATX01 CNC_MIL_400 MIL_LIN_360 401.00
IESI ATX ATX01 CNC_MIL_400 MIL_LIN_360 378.00
IESI ATX ATX01 CNC_MIL_400 MIL_LIN_360 265.00
IESI ATX ATX01 CNC_MIL_400 MIL_LIN_360 279.00
IESI ATX ATX01 CNC_MIL_400 MIL_LIN_360 376.00
IESI ATX ATX01 CNC_MIL_400 MIL_LIN_360 345.00
IESI ATX ATX01 CNC_MIL_400 MIL_LIN_360 256.00
IESI ATX ATX01 CNC_MIL_400 MIL_LIN_360 200.00
IESI ATX ATX01 CNC_MIL_400 MIL_LIN_360 278.00
IESI ATX ATX01 CNC_MIL_400 MIL_LIN_360 345.00
===============================================

i m having 25 obs i want to copy each obs in a new dataset.

Which means 25 obs should be split into 25 dataset . Each dataset consisting on 1 obs .

i can do in simple way like:
data one two three -------- twnyfive;
set datsetname;
run;

but my issue is that if there are 500 obs if i want 2 copy the values in 500 diffrent dataset i can write all 500 dataset name.

thnxzzzzz in advnce Smiley Happy
New Contributor
Posts: 4

Re: HOw can I create 25 datasets in data step

You could write a macro to do this:
First, get the number of obs into a macro var using a null data step or Proc SQL.
Then you could write a data step something like this:

data %do i=1 %to &N_obs; NewData&i %end;;
set OldData;
%do i=1 %to &N_obs;
if _N_=&i then output NewData&i;
%end;
run;
Super User
Posts: 19,772

Re: HOw can I create 25 datasets in data step

Here's a macro solution, but I do wonder why you want to do this.

proc sql noprint;
select count(*) into :num_records from sashelp.class ;
quit;

%put &num_records;


%macro output_separate();

%do i=1 %to &num_records;
data data_&i;
format obsnum 8.;
obsnum=&i.;
set sashelp.class point=obsnum;
output;
stop;
run;
%end;
%mend;

%output_separate
Contributor sss
Contributor
Posts: 65

Re: HOw can I create 25 datasets in data step

HI Reeza

thnxs a lot its working f9
Contributor sss
Contributor
Posts: 65

Re: HOw can I create 25 datasets in data step

hi polingjw

Code is working gr8 thnx uuu
Regular Contributor
Posts: 171

Re: HOw can I create 25 datasets in data step

You could also use a hash solution. Here is an example with sashelp.class.

[pre]

data _null_;
if _n_=1 then do;
declare hash h();
h.definekey('_n_');
h.definedata("Name", "Sex", "Age");
h.definedone();
end;
set sashelp.class;
h.clear();
h.add();
num = 'Obs'!!trim(left(put(_n_, 8.)));
h.output(dataset:num);
run;

[/pre]
Super Contributor
Super Contributor
Posts: 365

Re: HOw can I create 25 datasets in data step

Hello SSS,

This is a pure datastep solution:

[pre]data d1 d2 d3 d4;
set datsetname;
if _n_=1 then output d1;
else if _n_=2 then output d2;
else if _n_=3 then output d3;
else if _n_=4 then output d4;
run;
[/pre]
Sincerely,
SPR
Ask a Question
Discussion stats
  • 6 replies
  • 147 views
  • 0 likes
  • 5 in conversation