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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 688 views
  • 2 likes
  • 2 in conversation