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
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!
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.
Ready to level-up your skills? Choose your own adventure.