DATA Step, Macro, Functions and more

How can I make a new dataset for each set of rows

Reply
N/A
Posts: 0

How can I make a new dataset for each set of rows

I have to make a new dataset for each set of rows in a dataset. like a dataset


Data Set: Trip
PersonID TripID StrtTime EndTime
0001 1 0830 0900
0001 2 0915 1015
0001 2 1700 1730
0002 1 0700 0800
0002 2 0730 0830
0002 2 1630 1730

here I need to create a new dataset for each combination of PersonID And TripID.for eg. for 0001 & 1,there should be differnt dataset and for 0001 & 2, there should be differnt dataset and so on.
List of combination is dynamic and will be stored in a macro as a comma separated list.and combination will also be variant, i.e. it can have any number of columns.
Respected Advisor
Posts: 3,908

Re: How can I make a new dataset for each set of rows

What you ask for can be done but needs quite a bit of macro coding. At least I can't think of an easy way to do it.

I can't help but thinking that there must be another approach for your problem. It's normally not a good idea to create a lot of datasets.

Can you describe what the problem is you want to solve?

Regards, Patrick
Super Contributor
Posts: 474

Re: How can I make a new dataset for each set of rows

I agree with Patrick, generating many datasets are not a good thing to do (it should be "neater" if you could process the dataset at once without splitting it), but here's an "ugly" (call execute) but simple way of doing it without the complexity of macro coding.

Assuming that Trip is ordered by PersonID and TripID:

data _null_;
set Trip;
retain _FIRSTOBS;
by PersonID TripID; /* assume that Trip is ordered */
if first.TripID then do; /* first pair */
_FIRSTOBS=_N_; /* hold the obs num */
_COMBNUM+1; /* count combination number */
end;
if last.TripID then do; /* last pair */
call execute('data COMB'!!cats(put(_COMBNUM,best.))!!
'; set x (firstobs='!!cats(put(_FIRSTOBS,best.))!!' obs = '!!cats(put(_N_,best.))!!'); run;'); /* slice the dataset with call execute call */
end;
run;

Caution, at the end you'll have many COMBx datasets (one per combination).

Greetings from Portugal.

Daniel Santos at www.cgd.pt
Ask a Question
Discussion stats
  • 2 replies
  • 106 views
  • 0 likes
  • 3 in conversation