BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
yuhuanp
Calcite | Level 5

i have 50 xlsx excel workbooks, in a folder, i want to import the 2nd sheet of each excel and convert to sas datasets.

each of excel has different sheet name, but i know for each excel, i only need the 2nd sheet, is there a way to use sheet= "   "   to accomplish this?

thanks,

 

Ann

 

1 ACCEPTED SOLUTION

Accepted Solutions
AhmedAl_Attar
Rhodochrosite | Level 12

Hi Ann,

Here is a potential solution 

%let myPath=<Path>;  *<--- Change this to point to your directory;
options validvarname=any;

%macro import_sheet2(p_xlsxWorkbook=, p_outDsName=);
	libname xl XLSX "&p_xlsxWorkbook";
	/* discover member (DATA) names */
	ods output members=work.members;
	proc datasets lib=xl;
	quit;
	
	/* Get the second sheet name */
	data _null_;
		set work.members(where=(num=2));
		call symput('l_sheetName',strip(name));
	run;
	/* %put &=l_sheetName; */
/* Generate the output data set from the second sheet */ data &p_outDsName; SET xl.&l_sheetName; run; /* Clean-up */ proc delete data=work.members; run; libname xl CLEAR; %mend import_sheet2; /* Find all xlsx workbooks in specified directory */ data _NULL_; length fref $8 filename $100 macroStmt $200; rc=filename(fref, "&myPath"); if rc=0 then do; did=dopen(fref); rc=filename(fref); end; else do; length msg $200.; msg=sysmsg(); put msg=; did=.; end; if did <=0 then putlog 'ERR' 'OR: Unable to open directory.'; dnum=dnum(did); do i=1 to dnum; filename=dread(did, i); /* If this entry is a file, then process */ fid=mopen(did, filename); if fid > 0 then do; /* Look for xlsx workbooks */ if (scan(filename,2,'.') = 'xlsx') then do; macroStmt= '%import_sheet2(p_xlsxWorkbook=%str(&myPath/'||strip(filename)||'), p_outDsName='||scan(strip(filename),1,'.')||');'; call execute (macroStmt); end; end; end; rc=dclose(did); run;

I didn't use Proc Import, instead I used the XLSX Library Engine that was added in SAS 9.4 M2

 

Hope this helps

View solution in original post

5 REPLIES 5
Seadrago
Obsidian | Level 7

yes the sheet="" option in proc import is meant to bring in a specific sheet for excel.

I think a macro would be your best bet if you have 50 workbooks.

I am assuming you are using xlsx format in the below example. I am also assuming the the name of your second sheet in your excel workbook is "Sheet 2". It could have a different name if you named it differently but the default is "Sheet 1", "Sheet 2", etc.

 

%maco get_ds(xlin=,dsout=);

proc import datafile="&xlin."

out=&dsout.

dbms=xlsx 

replace;

sheet="Sheet 2"

run;

%mend get_ds;

%get_ds(xlin=excel_workbook1.xlsx, dsout=outdata1);

%get_ds(xlin=excel_workbook2.xlsx, dsout=outdata2);

etc.

 

yuhuanp
Calcite | Level 5

all 50 excel workbook the sheet2 have different names, the sheet name is not sheet2, it named by products, but i need to read the 2nd sheet from all 50 excels, 

 

AhmedAl_Attar
Rhodochrosite | Level 12

Hi Ann,

Here is a potential solution 

%let myPath=<Path>;  *<--- Change this to point to your directory;
options validvarname=any;

%macro import_sheet2(p_xlsxWorkbook=, p_outDsName=);
	libname xl XLSX "&p_xlsxWorkbook";
	/* discover member (DATA) names */
	ods output members=work.members;
	proc datasets lib=xl;
	quit;
	
	/* Get the second sheet name */
	data _null_;
		set work.members(where=(num=2));
		call symput('l_sheetName',strip(name));
	run;
	/* %put &=l_sheetName; */
/* Generate the output data set from the second sheet */ data &p_outDsName; SET xl.&l_sheetName; run; /* Clean-up */ proc delete data=work.members; run; libname xl CLEAR; %mend import_sheet2; /* Find all xlsx workbooks in specified directory */ data _NULL_; length fref $8 filename $100 macroStmt $200; rc=filename(fref, "&myPath"); if rc=0 then do; did=dopen(fref); rc=filename(fref); end; else do; length msg $200.; msg=sysmsg(); put msg=; did=.; end; if did <=0 then putlog 'ERR' 'OR: Unable to open directory.'; dnum=dnum(did); do i=1 to dnum; filename=dread(did, i); /* If this entry is a file, then process */ fid=mopen(did, filename); if fid > 0 then do; /* Look for xlsx workbooks */ if (scan(filename,2,'.') = 'xlsx') then do; macroStmt= '%import_sheet2(p_xlsxWorkbook=%str(&myPath/'||strip(filename)||'), p_outDsName='||scan(strip(filename),1,'.')||');'; call execute (macroStmt); end; end; end; rc=dclose(did); run;

I didn't use Proc Import, instead I used the XLSX Library Engine that was added in SAS 9.4 M2

 

Hope this helps

yuhuanp
Calcite | Level 5

thank you, the code works. 

Ann

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
  • 5 replies
  • 512 views
  • 1 like
  • 4 in conversation