# Export certain datasets as csv from macro

#### Morten

##### New Member
Hi

I have a functioning macro that creates a list of datasets from a master file that is unique for each day. The datasets I create in the SAS macro therefore have the data added to them so they are easy to categorize.

I would like to export some of the datasets as I create during the macro as csv files, as this will make analysis in R easier. I have written a Proc Export for each file I would like to export inside the macro itself, however even with the REPLACE function taken out, only the last file of the macro's run is saved to the file destination.

I use the below code for each dataset I would like to export while the macro is running.

Whould I write the proc export statements as individual macros instead?

Code:
PROC EXPORT DATA= dataset&year&mon2&day2

OUTFILE= "D:\Data"
DBMS=CSV LABEL;
PUTNAMES=YES;
RUN;
Thanks

#### Dason

It's been a while since I've worked with SAS macros but what is happening to you makes perfect sense to me since you're writing to the same outfile every time. You could dynamically create the outfile name each time using the macro variables though. Possibly something like

Code:
PROC EXPORT DATA= dataset&year&mon2&day2

OUTFILE= "D:\Data\dataset&year&mon2&day2..csv"
DBMS=CSV LABEL;
PUTNAMES=YES;
RUN;
but like I said I haven't used it in quite some time. I don't know if you need those two periods before the csv - but I recall needing two of them at some point.

#### Janus

##### New Member
It's been a while since I've worked with SAS macros but what is happening to you makes perfect sense to me since you're writing to the same outfile every time. You could dynamically create the outfile name each time using the macro variables though. Possibly something like

Code:
PROC EXPORT DATA= dataset&year&mon2&day2

OUTFILE= "D:\Data\dataset&year&mon2&day2..csv"
DBMS=CSV LABEL;
PUTNAMES=YES;
RUN;
but like I said I haven't used it in quite some time. I don't know if you need those two periods before the csv - but I recall needing two of them at some point.
You remember correctly (including the two periods, one to end the macro and one to specify the file extension).