Hello,
I have a SAS dataset that looks like this:
Col1 | Col2 | Color |
a | 01 | Blue |
a | 01 | Orange |
a | 02 | Blue |
b | 01 | Color |
b | 01 | Blue |
b | 02 | Orange |
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:
Col1 | Col2 | Color |
a | 01 | Blue |
a | 01 | Orange |
dataset2 would be:
a | 02 | Blue |
dataset3 would be:
b | 01 | Color |
b | 01 | Blue |
and so on...
How can I achieve this programmatically? Thank you in advance for any help!
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;
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;
Tom, outstanding! Thank you
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.