DATA Step, Macro, Functions and more

converting multiple sas file to text file

Accepted Solution Solved
Reply
Contributor
Posts: 25
Accepted Solution

converting multiple sas file to text file

Hi,

I'm trying to convert SAS files into text files and I have 120 of them. Can anyone help me figure out what wrong did I do. Appreciate your help

Thanks,

Esita

libname rna "C:\data";

%macro export_data (file=,textfile=);

proc export data=&file.

outfile="&textfile."

dbms= tab replace;

run;

%mend export_data;

%export_data (file= C:\data\cell_one.sas7bdat, textfile= rna.cell_one.txt);

%export_data (file= C:\data\cell_two.sas7bdat, textfile= rna.cell_two.txt);


Accepted Solutions
Solution
‎09-30-2014 04:49 PM
Super User
Posts: 11,343

Re: converting multiple sas file to text file

If you are naming the output based on the input and sending it all to the same folder you can simplify a bit:

libname rna "C:\data";

%macro export_data (file=);

proc export data=ma.&file.

outfile="C:\folder\subfolder\&file..txt"

dbms= tab replace;

run;

%mend export_data;

Better practice though would be to provide  library and path as parameters to make it more flexible. But if this is a one time use that may not be critical.

View solution in original post


All Replies
Super Contributor
Posts: 418

Re: converting multiple sas file to text file

I think there might be a few things wrong here. Off the top of my head, your textfile doesn't have an output path. Therefore your code for the first run will look like follows.

proc export data=C:\data\cell_one.sas7bdat   outfile="rna.cell_one.txt" dbms=tab replace;

run;

Note how the outfie doesn't have a specific output path location to output your file?  Try fixing that first.

Secondly, it looks like you are trying to export a sas Dataset found on your C:\data drive.  The problem is I don't think proc export allows you to use a file reference for a sas dataset. you actually need to use a named sas Dataset in your code, which you do not have... Don't quote me on this as I've actually never tried it and don't have sas in front of me right now.... However do this.

libname mylib 'c:\data';

proc export data=mylib.Cell_one outfile="c:\your output path\rna.cell_one.txt" dbms=tab replace;

run;

Super User
Posts: 19,869

Re: converting multiple sas file to text file

Posted in reply to Anotherdream

You actually can reference a sas dataset with it's path...but I'd recommend your solution overall. 

I'd bet if you searched through the forums you'd find several solutions to your problem, in fact, they would automatically call all files in that particular library.

Solution
‎09-30-2014 04:49 PM
Super User
Posts: 11,343

Re: converting multiple sas file to text file

If you are naming the output based on the input and sending it all to the same folder you can simplify a bit:

libname rna "C:\data";

%macro export_data (file=);

proc export data=ma.&file.

outfile="C:\folder\subfolder\&file..txt"

dbms= tab replace;

run;

%mend export_data;

Better practice though would be to provide  library and path as parameters to make it more flexible. But if this is a one time use that may not be critical.

Super User
Super User
Posts: 7,076

Re: converting multiple sas file to text file

If you turn on MPRINT option and just run one of the macro calls you should see the problem for yourself very quickly.

MPRINT(EXPORT_DATA):   proc export data=C:\data\cell_one.sas7bdat outfile="rna.cell_one.txt" dbms= tab replace;

NOTE: The SAS System stopped processing this step because of errors.

MPRINT(EXPORT_DATA):   run;

NOTE: Line generated by the macro variable "FILE".

1    C:\data\cell_one.sas7bdat

      -

      22

      200

ERROR 22-322: Syntax error, expecting one of the following: ;, (, DATA, DBLABEL, DBMS, DEBUG, FILE, LABEL, OUTFILE, OUTTABLE,

              REPLACE, TABLE, _DEBUG_.

ERROR 200-322: The symbol is not recognized and will be ignored.

You are passing in the SAS dataset by filename rather than using the libref MA that you defined in the first line of your program. So either change the first value passed to the macro to be a SAS dataset reference like MA.CELL_ONE or remove the LIBREF and put quotes around the physical filename "C:\data\cell_one.sas7bdat" and SAS will make a temporary libref for your automatically.

You also might want to include a fully qualified path for the output file, otherwise it will be created in whatever directory is current when the code is run. c:\data\rna.cell_one.txt

Contributor
Posts: 25

Re: converting multiple sas file to text file

Thank you all for your suggestions. This is what I tried. And I got error msg "ERROR: Export unsuccessful.  See SAS Log for details."

libname rna "C:\data";

%macro export_data (file=);

proc export data=rna.&file.

outfile="rna.&file.txt"

dbms= tab replace;

run;

%mend export_data;

%export_data (file= cell_one);

%export_data (file= cell_two);

Contributor
Posts: 25

Re: converting multiple sas file to text file

And I tried this as well. both give me the same error

libname rna "C:\data";

%macro export_data (file=);

proc export data=rna.&file.

outfile="C:\data.&file.txt"

dbms= tab replace;

run;

%mend export_data;

%export_data (file= cell_one);

%export_data (file= cell_two);

Super User
Posts: 11,343

Re: converting multiple sas file to text file

outfile="C:\data.&file.txt"

should be

outfile="C:\data\&file..txt"

outfile="C:\data.&file.txt" would resolve to outfile="C:\data<file>txt" and would be 1) just a folder and 2) likely one you don't have.

The first period after the &file says to append the text to the macro variable value, the second one is to comply with windows naming conventions of .extension.

You should post the error when you get one but I'm pretty sure it was a file or folder not found.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 1014 views
  • 1 like
  • 5 in conversation