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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 2 replies
  • 629 views
  • 0 likes
  • 3 in conversation