BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
current_thing
Obsidian | Level 7

Hello,

 

I have a SAS dataset that looks like this:

Col1Col2Color
a01Blue
a01Orange
a02Blue
b01Color
b01Blue
b02Orange

 

What I want to do is for each unique combination of value in Col1 and Col2 -> create a dataset and export it. So for example:

dataset1 would be:

Col1Col2Color
a01Blue
a01Orange

dataset2 would be:

a02Blue

dataset3 would be:

b01Color
b01Blue

and so on...

 

How can I achieve this programmatically? Thank you in advance for any help!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Much easier to create the CSV files directly without attempting to also create datasets.

Especially if you don't need the header line(s).

data _null_;
  set have;
  length filename $200;
  filename=cats('/mydir/myfolder/basename',col1,col2,'.csv');
  file csv filevar=filename dsd ;
  put (_all_) (+0);
run;

 If you do need the header line then it can also work if the data is already sorted (or at least grouped).

data _null_;
  set have;
  by col1 col2;
  length filename $200;
  filename=cats('/mydir/myfolder/basename',col1,col2,'.csv');
  file csv filevar=filename dsd ;
  if first.col2 then put 'col1,col2,color';
  put  col1 col2 color;
run;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Much easier to create the CSV files directly without attempting to also create datasets.

Especially if you don't need the header line(s).

data _null_;
  set have;
  length filename $200;
  filename=cats('/mydir/myfolder/basename',col1,col2,'.csv');
  file csv filevar=filename dsd ;
  put (_all_) (+0);
run;

 If you do need the header line then it can also work if the data is already sorted (or at least grouped).

data _null_;
  set have;
  by col1 col2;
  length filename $200;
  filename=cats('/mydir/myfolder/basename',col1,col2,'.csv');
  file csv filevar=filename dsd ;
  if first.col2 then put 'col1,col2,color';
  put  col1 col2 color;
run;
current_thing
Obsidian | Level 7

Tom, outstanding! Thank you

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 1168 views
  • 2 likes
  • 2 in conversation