BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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.
2 REPLIES 2
Patrick
Opal | Level 21
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
DanielSantos
Barite | Level 11
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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1065 views
  • 0 likes
  • 3 in conversation