Download presentation
Presentation is loading. Please wait.
Published byRoberta Walsh Modified over 8 years ago
1
SAS ® Global Forum 2014 March 23-26 Washington, DC Arthur Tabachneck Thornhill, ON Canada Tom Abernathy New York, NY Matt Kastin Penn Valley, PA
2
2 Have you ever run into problems trying to export SAS datasets to Excel? Question
3
3 Regardless of whether you said yes or no: Would you like to be able to: export without running into 32/64 bit clashes or needing SAS/Access for PC File Formatsexport without running into 32/64 bit clashes or needing SAS/Access for PC File Formats export by simply clicking on a file in the SAS Explorer windowexport by simply clicking on a file in the SAS Explorer window include or exclude the row that contains variable namesinclude or exclude the row that contains variable names export a table to a specific rangeexport a table to a specific range add a table to an existing worksheetadd a table to an existing worksheet copy a file to your system's clipboardcopy a file to your system's clipboard
4
4 what the solution looks like Right click on a dataset name in the SAS Explorer window
5
5 Then select the desired Action (i.e., press hot key (E) or point and left-click)
6
6 The Workbook will automagically be created
7
7 Left-click anywhere in the SAS Explorer Window how to get those capabilities
8
8 Left-click on Tools, move your mouse to Options→Explorer how to get those capabilities
9
9 Left-click on Members how to get those capabilities
10
10 Then double left-click on Table how to get those capabilities
11
11 left-click on Add an Add Action screen will appear and an Add Action screen will appear how to get those capabilities
12
12 Type the text you want to add to the menu &Export to Excel Action: &Export to Excel It will appear on the menu as: Export to Excel Note: You can make one character a hotkey by preceding it with an ampersand how to get those capabilities
13
13 Left click in the Action Command box &Export to Excel how to get those capabilities
14
14 Action Commands only accept up to 255 characters The 255 character limitation can be circumvented by submitting a macro When using gsubmit in an Action Command, everything between the two single quotes will be submitted some key points about the Action Command % has a special meaning in Action Commands thus, if you need to use a % (e.g., to call a macro), use two %s When a SAS dataset is selected %8b and %32b can be used to refer to the libname and filename, respectively
15
15 which can produce 9 different actions dependent upon how it is called Action #1: gsubmit '%exportxl(%8b, %32b, P, YES, NO)'; will run proc export on the dataset Note: Requires SAS/Access for PC File Formats gsubmit '%exportxl(%8b,%32b,P,YES,NO)'; &Export to Excel our paper presents a SAS macro: %exportxl libnm filenmtype usenamesrange
16
16 which can produce 9 different actions dependent upon how it is called Action #2: gsubmit '%exportxl(%8b, %32b, S, YES, NO)'; creates an Excel workbook with variable names in 1 st row Note: Actions 2-9 only require base SAS gsubmit '%exportxl(%8b,%32b,S,YES,NO)'; our paper presents a SAS macro: %exportxl libnm filenmtype usenamesrange Export to Excel with Variable &Names
17
17 which can produce 9 different actions dependent upon how it is called Action #3: gsubmit '%exportxl(%8b, %32b, S, YES, YES)'; creates an Excel workbook with variable names in 1 st row and lets you indicate the upper left cell where range begins Note: Actions 2-9 only require base SAS gsubmit '%exportxl(%8b,%32b,S,YES,YES)'; our paper presents a SAS macro: %exportxl libnm filenmtype usenamesrange Export to Excel Range with Variable &Names
18
18 which can produce 9 different actions dependent upon how it is called Action #4: gsubmit '%exportxl(%8b, %32b, S, NO, NO)'; creates an Excel workbook without variable names in 1 st row Note: Actions 2-9 only require base SAS gsubmit '%exportxl(%8b,%32b,S,NO,NO)'; our paper presents a SAS macro: %exportxl libnm filenmtype usenamesrange Export to Excel w/&o Variable Names
19
19 which can produce 9 different actions dependent upon how it is called Action #5: gsubmit '%exportxl(%8b, %32b, S, NO, YES)'; creates an Excel workbook without variable names in 1 st row and lets you indicate the upper left cell where range begins Note: Actions 2-9 only require base SAS gsubmit '%exportxl(%8b,%32b,S,NO,YES)'; our paper presents a SAS macro: %exportxl libnm filenmtype usenamesrange Export to Excel Range w/o Variable Names
20
20 which can produce 9 different actions dependent upon how it is called Action #6: gsubmit '%exportxl(%8b, %32b, A, YES, YES)'; adds to an existing Excel workbook with variable names in top row and you indicate upper left cell where range begins Note: Actions 2-9 only require base SAS gsubmit '%exportxl(%8b,%32b,A,YES,YES)'; our paper presents a SAS macro: %exportxl libnm filenmtype usenamesrange &Add to Excel Range with Variable Names
21
21 which can produce 9 different actions dependent upon how it is called Action #7: gsubmit '%exportxl(%8b, %32b, A, NO, YES)'; adds to existing Excel workbook w/o variable names in top row and you indicate the upper left cell where range begins Note: Actions 2-9 only require base SAS gsubmit '%exportxl(%8b,%32b,A,NO,YES)'; our paper presents a SAS macro: %exportxl libnm filenmtype usenamesrange Export to Excel Range w/o Variable Names
22
22 which can produce 9 different actions dependent upon how it is called Action #8: gsubmit '%exportxl(%8b, %32b, C, YES, NO)'; copies dataset to clipboard with variable names in 1 st row Note: Actions 2-9 only require base SAS gsubmit '%exportxl(%8b,%32b,C,YES,NO)'; our paper presents a SAS macro: %exportxl libnm filenmtype usenamesrange Export to Clipboard with Variable &Names
23
23 which can produce 9 different actions dependent upon how it is called Action #9: gsubmit '%exportxl(%8b, %32b, C, NO, NO)'; copies dataset to clipboard w/o variable names in 1 st row Note: Actions 2-9 only require base SAS gsubmit '%exportxl(%8b,%32b,C,NO,NO)'; our paper presents a SAS macro: %exportxl libnm filenmtype usenamesrange Export to Clipboard w/o Variable &Names
24
24 click on OK to exit the Add Action screen gsubmit '%exportxl(%8b,%32b,P,YES)'; &Export to Excel after you have entered the action command
25
25 click on OK to exit the Table Options screen then,
26
26 Once you have completed those steps whenever you right click on a file in the SAS Explorer window, and select an action, the action will be taken
27
27 if you use one of the Actions with range=YES* enter the upper left cell where the range should begin and then press your key *Note: not applicable to Action #1 as proc export doesn't support outputting to a range the following screen will appear when you select the action
28
28 and, if you use Actions 2-7 i.e., the program will ask you whether it should, or shouldn't, replace the existing file if the workbook already exists, a screen like the following will appear
29
29 Save the file as exportxl.sas in a directory that exists in your SASAUTOS* path * see: http://analytics.ncsu.edu/sesug/2008/SBC-126.pdfhttp://analytics.ncsu.edu/sesug/2008/SBC-126.pdf where to get the macro Copy the SAS program from: http://www.sascommunity.org/wiki/Presentations User: Art297
30
30 %macro exportxl(libnm,filenm,type,usenames,range); %let filepath=%sysfunc(pathname(&libnm.)); %if &type. eq P %then %do; proc export data=&libnm..&filenm. outfile= "&filepath.\&filenm..xlsx" dbms=xlsx replace; run; %end; how the macro works libnm filenmtype usenames %8b %32b P for Proc Export S for vbs script A(dd) to existing workbook C for Clipboard include row with variable names YES NO %let filepath=%sysfunc(pathname(&libnm.)); whether table should be written to a range YES NO range
31
31 %macro exportxl(libnm,filenm,type,usenames,range); %let filepath=%sysfunc(pathname(&libnm.)); %if &type. eq P %then %do; proc export data=&libnm..&filenm. outfile= "&filepath.\&filenm..xlsx" dbms=xlsx replace; run; %end; how the macro works
32
32 %else %do; proc fcmp outlib=work.func.util; function c2cb(lib $,mem $, usenm $); rc=filename('clippy',' ','clipbrd'); if rc ne 0 then return(1); fid=fopen('clippy','o',0,'v'); if fid eq 0 then do; rc = filename( 'clippy' ); return(2); end; dsid=open(catx('.',lib,mem)); if dsid eq 0 then do; rc=fclose(fid); rc=filename('clippy'); return(3); end; how the macro works
33
33 %else %do; proc fcmp outlib=work.func.util; function c2cb(lib $,mem $, usenm $); rc=filename('clippy',' ','clipbrd'); if rc ne 0 then return(1); fid=fopen('clippy','o',0,'v'); if fid eq 0 then do; rc = filename( 'clippy' ); return(2); end; dsid=open(catx('.',lib,mem)); if dsid eq 0 then do; rc=fclose(fid); rc=filename('clippy'); return(3); end; how the macro works Open the clipbrd so that we can write to it
34
34 %else %do; proc fcmp outlib=work.func.util; function c2cb(lib $,mem $, usenm $); rc=filename('clippy',' ','clipbrd'); if rc ne 0 then return(1); fid=fopen('clippy','o',0,'v'); if fid eq 0 then do; rc = filename( 'clippy' ); return(2); end; dsid=open(catx('.',lib,mem)); if dsid eq 0 then do; rc=fclose(fid); rc=filename('clippy'); return(3); end; how the macro works Open the SAS dataset so that we can read it
35
35 nvar=attrn(dsid,'nvar'); array v[1] /nosymbols; call dynamic_array(v,nvar); do i = 1 to nvar; v[i]=ifn( vartype(dsid,i)='C',1,2); if usenm eq 'YES' then do; if i gt 1 then rc=fput(fid,'09'x); rc=fput(fid,varname(dsid,i)); end; if usenm eq 'YES' then rc=fwrite(fid); do i=1 to attrn(dsid,'nlobs'); rc=fetchobs(dsid,i); do j=1 to nvar; if j gt 1 then rc=fput(fid,'09'x); if (v[j] eq 1) then rc=fput(fid, getvarc(dsid,j)); else do; fmt=varfmt(dsid,j) ; if missing(fmt) then fmt='best12.'; rc=fput(fid,putc(putn(getvarn(dsid,j),fmt),'$char12.')); end; rc=fwrite(fid); end; how the macro works Get number of variables and declare array
36
36 nvar=attrn(dsid,'nvar'); array v[1] /nosymbols; call dynamic_array(v,nvar); do i = 1 to nvar; v[i]=ifn( vartype(dsid,i)='C',1,2); if usenm eq 'YES' then do; if i gt 1 then rc=fput(fid,'09'x); rc=fput(fid,varname(dsid,i)); end; if usenm eq 'YES' then rc=fwrite(fid); do i=1 to attrn(dsid,'nlobs'); rc=fetchobs(dsid,i); do j=1 to nvar; if j gt 1 then rc=fput(fid,'09'x); if (v[j] eq 1) then rc=fput(fid, getvarc(dsid,j)); else do; fmt=varfmt(dsid,j) ; if missing(fmt) then fmt='best12.'; rc=fput(fid,putc(putn(getvarn(dsid,j),fmt),'$char12.')); end; rc=fwrite(fid); end; how the macro works Assign values to array based on variable type Assign variable names
37
37 nvar=attrn(dsid,'nvar'); array v[1] /nosymbols; call dynamic_array(v,nvar); do i = 1 to nvar; v[i]=ifn( vartype(dsid,i)='C',1,2); if usenm eq 'YES' then do; if i gt 1 then rc=fput(fid,'09'x); rc=fput(fid,varname(dsid,i)); end; if usenm eq 'YES' then rc=fwrite(fid); do i=1 to attrn(dsid,'nlobs'); rc=fetchobs(dsid,i); do j=1 to nvar; if j gt 1 then rc=fput(fid,'09'x); if (v[j] eq 1) then rc=fput(fid, getvarc(dsid,j)); else do; fmt=varfmt(dsid,j) ; if missing(fmt) then fmt='best12.'; rc=fput(fid,putc(putn(getvarn(dsid,j),fmt),'$char12.')); end; rc=fwrite(fid); end; how the macro works Write variable names
38
38 nvar=attrn(dsid,'nvar'); array v[1] /nosymbols; call dynamic_array(v,nvar); do i = 1 to nvar; v[i]=ifn( vartype(dsid,i)='C',1,2); if usenm eq 'YES' then do; if i gt 1 then rc=fput(fid,'09'x); rc=fput(fid,varname(dsid,i)); end; if usenm eq 'YES' then rc=fwrite(fid); do i=1 to attrn(dsid,'nlobs'); rc=fetchobs(dsid,i); do j=1 to nvar; if j gt 1 then rc=fput(fid,'09'x); if (v[j] eq 1) then rc=fput(fid, getvarc(dsid,j)); else do; fmt=varfmt(dsid,j) ; if missing(fmt) then fmt='best12.'; rc=fput(fid,putc(putn(getvarn(dsid,j),fmt),'$char12.')); end; rc=fwrite(fid); end; how the macro works Get data and formats and write tab-delimited records
39
39 rc=fclose(fid); rc=close(dsid); rc=filename('clippy'); return(0); endsub; quit; %local cmplib; %let cmplib=%sysfunc(getoption(cmplib)); options cmplib=(work.func); %put %sysfunc(c2cb(&libnm,&filenm,&usenames)) ; options cmplib=(&cmplib); how the macro works Cleanup
40
40 how the macro works rc=fclose(fid); rc=close(dsid); rc=filename('clippy'); return(0); endsub; quit; %local cmplib; %let cmplib=%sysfunc(getoption(cmplib)); options cmplib=(work.func); %put %sysfunc(c2cb(&libnm,&filenm,&usenames)) ; options cmplib=(&cmplib); Get options Run Function Reset Options
41
41 %if &range. eq YES %then %do; data _null_; window range rows=8 columns=80 irow=1 icolumn=2 color=black #2 @3 'Enter the upper left cell where range should begin (e.g. D5): ' color=gray range $8. required=yes attr=underline color=yellow; DISPLAY range blank; call symputx('range',range); stop; run; %end; %else %do; %let range=A1; %end; how the macro works Display window to let users identify range
42
42 %if &range. eq YES %then %do; data _null_; window range rows=8 columns=80 irow=1 icolumn=2 color=black #2 @3 'Enter the upper left cell where range should begin (e.g. D5): ' color=gray range $8. required=yes attr=underline color=yellow; DISPLAY range blank; call symputx('range',range); stop; run; %end; %else %do; %let range=A1; %end; how the macro works Otherwise, set default range
43
43 %if &type. eq A or &type. eq S %then %do; data _null_; length script filevar $256; script = catx('\',pathname('WORK'),'PasteIt.vbs'); filevar = script; script="'"||'cscript "'||trim(script)||'"'||"'"; call symput('script',script); file dummy1 filevar=filevar recfm=v lrecl=512; put 'Dim objExcel'; put 'Dim Newbook'; put 'Set objExcel = CreateObject("Excel.Application")'; %if &type. eq S %then %do; put 'Set Newbook = objExcel.Workbooks.Add()'; put 'Newbook.Sheets("Sheet1").Select'; script=catt('Newbook.Sheets("Sheet1").Name = "',"&filenm.",'"'); put script; %end; %else %do; script=catt('Set Newbook = objExcel.Workbooks.Open("',"&filepath.\&filenm..xlsx",'")'); put script; script=catt('Newbook.Sheets("',"&filenm.",'").Select'); put script; %end; put 'objExcel.Visible = True'; script=catt('Newbook.Sheets("',"&filenm.",'").Range("',"&Range.",'").Activate'); put script; script=catt('Newbook.Sheets("',"&filenm.",'").Paste'); put script; script=catt('Newbook.SaveAs("',"&filepath.\&filenm..xlsx",'")'); put script; put "Newbook.Close"; put 'objExcel = Nothing'; put 'Newbook = Nothing'; run; how the macro works If type eq A or S create a VBS script called PasteIt.vbs Dim objExcel Dim Newbook Set objExcel = CreateObject("Excel.Application") Set Newbook =objExcel.Workbooks.Open("c:\art\Class.xlsx") Newbook.Sheets("Class").Select objExcel.Visible = True Newbook.Sheets("Class").Range("f3").Activate Newbook.Sheets("Class").Paste Newbook.SaveAs("c:\art\Class.xlsx") Newbook.Close objExcel = Nothing Newbook = Nothing
44
44 data _null_; call system(&script.); run; %end; %mend exportxl; how the macro works Run the VBS script where &script. = cscript WorkDirectoryPath\PasteIt.vbs
45
45 What the ExportXL macro is and where you can download it How the macro works How you can set up Action Commands to call the macro to accomplish things that you can't do with proc export summary of what I just presented: How you can create Action Commands in the SAS Explorer window
46
46 Export to Excel Copy Variable Names to Clipboard Run Proc Contents Get descriptive statistics Show all correlations Run Proc Means Print bar charts Run Proc Freq and, if you want, you can use the same method to add all of your repeated tasks to the SAS Explorer menu
47
47 stand alone version (exportxlsx.sas) also available on the paper's web page uses named parameters extra functionality: choice of variable labels or names, specify separate output path and indicate whether file should be replaced callable from base SAS or EG ability to write multiple tables to same worksheet uses PROC EXPORT-like parameter names (e.g., data, outfile, sheet and replace)
49
Your comments and questions are valued and encouraged Contact the Authors Arthur Tabachneck, Ph.D. myQNA, Inc. Thornhill, ON art297@rogers.com Tom Abernathy Pfizer, Inc. New York, NY tom.abernathy@pfizer.com Matt Kastin I-Behavior, Inc. Penn Valley, PA matthew.kastin@gmail.com
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.