BookmarkSubscribeRSS Feed
nayab_shaik
Calcite | Level 5

Hi Team,

how can you export 50 data sets into single excel workbook with different sheets.

 

 

 

Advance Thanks

 

11 REPLIES 11
PeterClemmensen
Tourmaline | Level 20

Are the data sets in the same library? And how do you determine the names of the data sets?

Reeza
Super User
You can use ODS EXCEL.
If you need more details, please provide more details about your problem. Vague questions will get vague answers.
koyelghosh
Lapis Lazuli | Level 10

You can try the below. Just change the OUTFILE= with the full path of the file (in which you want all the results to go). Please note this macro may not work if you have more than 255 sheets to be exported. LIBNAME is the library/folder where all your sas datasets are there.

 

%MACRO ExportAllDatasetsToExcel(LIBNAME, Extension="XLS");

	PROC SQL NOPRINT;
	CREATE TABLE columns as
	SELECT DISTINCT(memname) AS table_name FORMAT=$30.
	FROM dictionary.columns
	WHERE libname = &LIBNAME
	;
	quit;
	
	PROC SQL NOPRINT;
		SELECT COUNT(*) AS NumObs INTO :NumObs FROM columns;
	RUN;

	%DO I=1 %TO &NumObs;
		DATA _NULL_ ;
			SET Columns (FIRSTOBS=&I OBS=&I);
			CALL SYMPUT('TableName',TRIM(table_name));
		RUN;
		%PUT "&TableName";
		PROC EXPORT DATA=&LIBNAME..&TableName DBMS=XLSX OUTFILE="FullPathOfTheFileHere.xlsx";
		SHEET="&TableName";
		RUN;
	%END;
%MEND;

Please let me know if this worked for you. I am just curious.

nayab_shaik
Calcite | Level 5

Thanku you

ChrisNZ
Tourmaline | Level 20

Shouldn't this work?

 

libname XL xlsx "%sysfunc(pathname(WORK))\T.xlsx";

 

proc copy inlib=WORK outlib=XL mt=data; run;

 

andreas_lds
Jade | Level 19

@ChrisNZ wrote:

Shouldn't this work?

 

libname XL xlsx "%sysfunc(pathname(WORK))\T.xlsx";

 

proc copy inlib=WORK outlib=XL mt=data; run;

 


 

Works perfect, with one minor glitch:

WARNING: Engine XLSX does not support SORTEDBY operations.  SORTEDBY information cannot be copied.
ChrisNZ
Tourmaline | Level 20
That makes sense of course. No metadata in Excel.
koyelghosh
Lapis Lazuli | Level 10

@ChrisNZ Works very nicely. Your code is simple and elegant.

 

I read earlier but I forgot @Kurt_Bremser advice (his Maxim 11 of 52 ... that a Macro is not needed) and the result is a negative control of an experiment (wherein the positive control being someone pursuing reuse of existing code and elegance).

 

Lesson: Use BASE SAS first. So much is already there. Perhaps many times, one can spare reinventing the wheel.

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
  • 11 replies
  • 5270 views
  • 3 likes
  • 7 in conversation