Presentation is loading. Please wait.

Presentation is loading. Please wait.

SAS ® Global Forum 2014 March 23-26 Washington, DC Arthur Tabachneck Thornhill, ON Canada Tom Abernathy New York, NY Matt Kastin Penn Valley, PA.

Similar presentations


Presentation on theme: "SAS ® Global Forum 2014 March 23-26 Washington, DC Arthur Tabachneck Thornhill, ON Canada Tom Abernathy New York, NY Matt Kastin Penn Valley, PA."— Presentation transcript:

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 or limitations in exporting 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 Formats?export 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 window or programmatically in either base SAS or EG?export by simply clicking on a file in the SAS Explorer window or programmatically in either base SAS or EG? include or exclude a row that contains either variable names or variable labels?include or exclude a row that contains either variable names or variable labels? export a table to a specific range?export a table to a specific range? add a table to an existing worksheet?add a table to an existing worksheet? add a worksheet to an existing workbook?add a worksheet to an existing workbook? copy a file to your system's clipboard?copy 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 that can be used to drive more than 60 different actions dependent upon how it is called gsubmit '%exportxl(data=%8b.%32b, type=P method=E)'; will run proc export on the dataset Note: ONLY type=P Requires SAS/Access for PC File Formats gsubmit '%exportxl(data=%8b.%32b,metho &Export to Excel our paper presents a SAS macro: %exportxl for example

16 16 gsubmit '%exportxl(data=%8b.%32b, type=N, method=E)'; will use vbs to create a new Excel Workbook gsubmit '%exportxl(data=%8b.%32b, type=C, method=E)'; will copy the dataset to your system's clipboard gsubmit '%exportxl(data=%8b.%32b, type=A, method=E)'; will use vbs to create a new WorkSheet gsubmit '%exportxl(data=%8b.%32b, type=M, range=F3, method=E)'; will use vbs to modify an existing WorkSheet, starting the added table at cell F3 gsubmit '%exportxl(data=%8b.%32b, type=N, usenames=L, method=E)'; will use vbs to create a new Workbook with the first row containing the variables' labels more examples:

17 17 data is the dataset you want to export outfile is the Excel Workbook you want to create or modify type can have one of five values: P to run PROC EXPORT N to use VBS to create a new Workbook M to use VBS to modify an existing Worksheet A to use VBS to add a new Worksheet C to copy a file to your system's clipboard usenames can have one of three values: Y to include a row of variable names L to include a row of variable labels N to not include a variable name or label header record the macro's named parameters

18 18 range can either be YES if running from SAS Explorer to indicate that you want to specify a range NO if running from SAS Explorer to indicate that you don't want to specify a range A1 or any valid Excel cell where you want a table to start and you're not running from SAS Explorer sheet is the Excel Worksheet you want to create or modify replace can have one of two values: Y to replace a Workbook if it already exists N to ask, first, if an existing Workbook is found method can have one of two values: E to indicate that the macro was run from SAS Explorer I to indicate that the macro was run programmatically the macro's named parameters (continued)

19 19 click on OK to exit the Add Action screen gsubmit '%exportxl(%8b,%32b,P,YES)'; &Export to Excel after you have entered an action command

20 20 click on OK to exit the Table Options screen then, after you've entered as many actions as you care to create

21 21 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

22 22 if you use one of the Actions with range=YES* and method=E 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

23 23 and, if you use type=N, M or A, without Replace i.e., the program will ask you whether it should, or shouldn't, replace the existing file a screen like the following will appear

24 24 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

25 25 %macro exportxl(data=, outfile=, sheet=, type=N, usenames=Y, range=A1, replace=Y, method=I); %if %sysfunc(countw(&data.)) eq 2 %then %do; %let libnm=%scan(&data.,1); %let filenm=%scan(&data.,2); %end; %else %do; %let libnm=work; %let filenm=&data.; %end; %if %length(&outfile.) lt 1 %then %do; %let outfile=%sysfunc(pathname(&libnm.))\&filenm..xlsx; %let sheet=&filenm.; %end; how the macro works

26 26 %macro exportxl(data=, outfile=, sheet=new, type=N, usenames=Y, range=A1, replace=Y, method=I); %if %sysfunc(countw(&data.)) eq 2 %then %do; %let libnm=%scan(&data.,1); %let filenm=%scan(&data.,2); %end; %else %do; %let libnm=work; %let filenm=&data.; %end; %if %length(&outfile.) lt 1 %then %do; %let outfile=%sysfunc(pathname(&libnm.))\&filenm..xlsx; %let sheet=&filenm.; %end; how the macro works

27 27 %macro exportxl(data=, outfile=, sheet=new, type=N, usenames=Y, range=A1, replace=Y, method=I); %if %sysfunc(countw(&data.)) eq 2 %then %do; %let libnm=%scan(&data.,1); %let filenm=%scan(&data.,2); %end; %else %do; %let libnm=work; %let filenm=&data.; %end; %if %length(&outfile.) lt 1 %then %do; %let outfile=%sysfunc(pathname(&libnm.))\&filenm..xlsx; %let sheet=&filenm.; %end; how the macro works

28 28 %macro exportxl(data=, outfile=, sheet=new, type=N, usenames=Y, range=A1, replace=Y, method=I); %if %sysfunc(countw(&data.)) eq 2 %then %do; %let libnm=%scan(&data.,1); %let filenm=%scan(&data.,2); %end; %else %do; %let libnm=work; %let filenm=&data.; %end; %if %length(&outfile.) lt 1 %then %do; %let outfile=%sysfunc(pathname(&libnm.))\&filenm..xlsx; %let sheet=&filenm.; %end; how the macro works

29 29 %if %upcase(&type.) eq P %then %do; proc export data=&libnm..&filenm. outfile= "&outfile." dbms=xlsx %if %upcase(&replace.) eq Y %then replace; ; /*Note: the following line can be included if DBMS=Excel is used*/ /*%if &sheet ne "" %then sheet="&sheet";;*/ run; %end; how the macro works

30 30 %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

31 31 %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

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 Open the SAS dataset so that we can read it

33 33 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 upcase(usenm) in ('L','Y') then do; if i gt 1 then rc=fput(fid,'09'x); if upcase(usenm) eq 'Y' then rc=fput(fid,varname(dsid,i)); else do; if missing(varlabel(dsid,i)) then rc=fput(fid,varname(dsid,i)); else rc=fput(fid,varlabel(dsid,i)); end; if upcase(usenm) in ('L','Y') then rc=fwrite(fid); how the macro works Get number of variables and declare array

34 34 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 upcase(usenm) in ('L','Y') then do; if i gt 1 then rc=fput(fid,'09'x); if upcase(usenm) eq 'Y' then rc=fput(fid,varname(dsid,i)); else do; if missing(varlabel(dsid,i)) then rc=fput(fid,varname(dsid,i)); else rc=fput(fid,varlabel(dsid,i)); end; if upcase(usenm) in ('L','Y') then rc=fwrite(fid); how the macro works Assign values to array based on variable type

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 upcase(usenm) in ('L','Y') then do; if i gt 1 then rc=fput(fid,'09'x); if upcase(usenm) eq 'Y' then rc=fput(fid,varname(dsid,i)); else do; if missing(varlabel(dsid,i)) then rc=fput(fid,varname(dsid,i)); else rc=fput(fid,varlabel(dsid,i)); end; if upcase(usenm) in ('L','Y') then rc=fwrite(fid); how the macro works Assign variable names or labels

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 upcase(usenm) in ('L','Y') then do; if i gt 1 then rc=fput(fid,'09'x); if upcase(usenm) eq 'Y' then rc=fput(fid,varname(dsid,i)); else do; if missing(varlabel(dsid,i)) then rc=fput(fid,varname(dsid,i)); else rc=fput(fid,varlabel(dsid,i)); end; if upcase(usenm) in ('L','Y') then rc=fwrite(fid); how the macro works Write variable names/ labels

37 37 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; rc=fclose(fid); rc=close(dsid); rc=filename('clippy'); return(0); endsub; quit; how the macro works Get data and formats and write tab-delimited records

38 38 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; rc=fclose(fid); rc=close(dsid); rc=filename('clippy'); return(0); endsub; quit; how the macro works Cleanup

39 39 how the macro works %local cmplib; %let cmplib=%sysfunc(getoption(cmplib)); options cmplib=(work.func); %put %sysfunc(c2cb(&libnm,&filenm,&usenames)) ; options cmplib=(&cmplib); Get current cmplib option setting Replace cmplib option Run Function Reset Options

40 40 %if %upcase(&type.) eq N or %upcase(&type.) eq M or %upcase(&type.) eq A %then %do; %if %upcase(&method.) eq E %then %do; %if %upcase(&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 $41. required=yes attr=underline color=yellow; DISPLAY range blank; call symputx('range',range); stop; run; %end; %else %let range=A1;; %end; how the macro works Display window to let users identify range

41 41 %if %upcase(&type.) eq N or %upcase(&type.) eq M or %upcase(&type.) eq A %then %do; %if %upcase(&method.) eq E %then %do; %if %upcase(&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 $41. required=yes attr=underline color=yellow; DISPLAY range blank; call symputx('range',range); stop; run; %end; %else %let range=A1;; %end; how the macro works Otherwise, set default 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

43 43 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'; %if %upcase(&type.) eq N or %upcase(&type.) eq A %then %do; %if %upcase(&type.) eq N %then put 'Dim NewSheet';; put 'Dim inSheetCount'; %if %upcase(&type.) eq A %then put 'Dim strFile';; %end; put 'Set objExcel = CreateObject("Excel.Application")'; %if %upcase(&type.) eq N %then %do; put 'Set Newbook = objExcel.Workbooks.Add()'; put 'objExcel.Visible = True'; put 'intSheetCount = Newbook.Application.Worksheets.Count'; how the macro works Dim objExcel Dim Newbook Dim NewSheet Dim inSheetCount Set objExcel = CreateObject("Excel.Application") Set Newbook = objExcel.Workbooks.Add() objExcel.Visible = True intSheetCount = Newbook.Application.Worksheets.Count set NewSheet = Newbook.Sheets.Add +(,objExcel.WorkSheets(intSheetCount)) Newbook.Sheets(intSheetCount + 1).Name="class1" Newbook.Sheets("class1").Select Newbook.Sheets("class1").Range("A1").Activate Newbook.Sheets("class1").Paste objExcel.DisplayAlerts = False NewSheet.SaveAs("c:\art\test1.xlsx") objExcel.Workbooks.Close objExcel.DisplayAlerts = True objExcel.Quit If type eq N, M or A create a VBS script called PasteIt.vbs

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, or call the macro directly from base SAS or EG, to accomplish things 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

48 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


Download ppt "SAS ® Global Forum 2014 March 23-26 Washington, DC Arthur Tabachneck Thornhill, ON Canada Tom Abernathy New York, NY Matt Kastin Penn Valley, PA."

Similar presentations


Ads by Google