
Applications Development Paper 26 Using The SAS System and Dynamic Data Exchange to Populate Microsoft Word Documents with Text, Tables, and Graphs Michael C. Harris, Amgen, Thousand Oaks, CA 6. The system shall have the ability to toggle the superscript attribute Abstract on and off. 7. The system shall have the ability to control typefaces and point Dynamic data exchange (DDE) is one of the means by which sizes. appropriately written Microsoft® Windows® applications can 8. The system shall have the ability to control navigation within a communicate and share data with one another. DDE is a client/server document. architecture. The application that controls another via DDE is the client, 9. The system shall have the ability to control text selection within a and the controlled application (which usually provides some kind of document. functionality not present in the client) is the server. The SAS Data Step 10. The system shall have the ability to control navigation with tables. Language can encapsulate WordBasic statements and enable SAS 11. The system shall have the ability to control text selection within a programs running on personal computers to be effective clients and table. control the actions of Microsoft Word for Windows (Word) in ways that 12. The system shall provide facilities for formatting tables. are useful for creating, populating, and formatting tables, and placing 13. The system shall provide facilities for creating tables of user- text strings and graphical elements at predetermined locations within specified dimensions. documents. Output Requirements Introduction 1. The system shall have the ability to save the current file. Integrating the results of analyses in text, tabular, and graphical formats 2. The system shall have the ability to save the current file to a new into documents created with word processing software is a common filename. activity. SAS software is capable of generating all three types of output 3. The system shall have the ability to save the current file in any mentioned above, but has limited intrinsic features for creating format supported by Word. publication quality text and tables, and none at all for directly populating existing word processor files or creating foreign file types Operational Requirements without significant programming effort. A library of SAS macros for 1. The system shall function with Windows NT 4.0 generating output via DDE can provide the desired flexibility and 2. The system shall function with Microsoft Word for Windows functionality while maximizing code reuse. Gilmore discussed the use of version 7.0 bookmarks within Microsoft Word to insert small amounts of text at predetermined locations within documents and briefly described how to control the actions of Word1. We will focus on techniques for complex Design Decisions data transfer and assume the reader has a working knowledge of Word There are two approaches to implementing a DDE system for table and the SAS Macro language. Extensive code samples are presented. generation. The first is to provide ease of use at the expense of flexibility. The second is to provide a high level of control with Requirements increased complexity from both the developer’s and the user’s Specifying what a software system must do is a necessary step in the life perspectives. The second approach almost automatically expands cycle of the system. Since anything that can be done via keyboard or functionality beyond table generation into total document control. Since mouse can be done via DDE with SAS, it makes sense to identify a there is a time and place for each, both approaches will be subset of Word’s functionality that specifically addresses the problem at demonstrated. hand. Note that your problem may not be exactly the same as someone else’s, and you may therefore have different requirements. Below is a Ease of Use minimal set of requirements that can serve a starting point for future The idea here is to wrap every piece of functionality in a DATA development. These requirements reflect the functionality used to _NULL_ step packaged in a macro so that each call stands alone. This produce the examples shown in this paper. makes it easy to understand what a program is doing while hiding the details. It is convenient to bundle related macros into one file which is Input Requirements itself a macro that can be invoked through the autocall facility. Invoking 1. The system shall accept SAS data sets as input. the macro sets up the environment required by the system. Below is 2. The system shall accept user-specified alphanumeric input. code for such a library. Space doesn’t permit a full description of each 3. The system shall have the ability to open existing Microsoft Word macro. documents. %macro ddemac; options noxwait; Processing Requirements filename cmds dde 'winword|system'; 1. The system shall have the ability to launch Microsoft Word without manual intervention. /* openword starts Winword and waits for 5 seconds */ 2. The system shall have the ability to close the current Microsoft %macro openword; Word document. data _null_; 3. The system shall have the ability to exit Microsoft Word without call system('start manual intervention. c:\progra~1\msoffice\winword\winword.exe'); 4. The system shall have the ability to toggle bold printing on and t=sleep(5); run; off. %mend; 5. The system shall have the ability to control justification attributes. /* opendoc opens the specified document */ %macro opendoc(doc=); Applications Development data _null_; %mend; cmd='[FileOpen.Name =' || '"' || "&doc" || /* ins_pict inserts a graphic at the insertion '"' || ']'; point */ file cmds; %macro ins_pict(name=, link=); put cmd; data _null_; run; file cmds; %mend; cmd='[InsertPicture .Name = "'|| "&name" || /* wordsave saves current Winword document */ '", .LinkToFile = "' || "&link" %macro wordsave; || '"]'; data _null_; put cmd; file cmds; run; put '[FileSave]'; %mend; run; %mend ddemac; %mend; /* wordexit closes Winword */ %macro wordexit; More Control data _null_; A high level of flexibility can be achieved by wrapping SAS code file cmds; fragments containing WordBasic statements in macros. The full power put '[FileExit]'; run; of the DATA step is then available to the programmer. The code below %mend; fulfills the requirements not met above and demonstrates the kinds of /* findtbl positions cursor in row 1 col 1 of things that can be done. specified table */ %macro findtbl(table); %macro ddemac2; %if &table= %then /* wordbold toggles bold printing on and off; %let table=1; valid values for action are 'on' and 'off' */ data _null_; %macro bold(action); length cmd $ 200; %let action=%upcase(&action); cmd='[EditGoTo .Destination = ' || '"' || 't' || "&table" || '"]'; %if &action=ON %then %do; file cmds; put '[Bold 1]'; put cmd; %end; run; %if &action=OFF %then %do; %mend; put '[Bold 0]'; /* table writes the input data set to the %end; current table beginning at the current cursor %mend; location. Use findtbl to locate row 1 col 1 of a /* center turns centering on */ particular table */ %macro center; %macro table(dsn=, rows=, cols=); put '[CenterPara]'; data _null_; %mend; %let cells=%eval(&rows*&cols); /* leftjust turns on left justification */ set &dsn; %macro leftjust; file cmds; put '[LeftPara]'; %mend; %do r=1 %to &rows; /* ritejust turns on right justification */ %do c=1 %to &cols; %macro ritejust; cmd= '[Insert "' || col&c || '"]'; put '[RightPara]'; put cmd; %mend; %if %eval(&r*&c) < &cells %then %do; /* para inserts a new paragraph */ put '[NextCell]';; %macro para; %end; put '[InsertPara]'; %end; %mend; return; /* page inserts a page break */ %end; %macro page; run; put ‘[InsertBreak .Type = 0]’; %mend; data _null_; /* fontsize sets font to specified size */ file cmds; %macro fontsize(points); put '[TableDeleteRow]'; put "[FontSize &points]"; run; %mend; %mend; /* sprscrpt sets the superscript text attribute; /* tblfmt uses the autoformat feature of Word to on can be 1 to turn on or 0 to turn off */ format a table */ %macro sprscrpt(on); %macro tblfmt(fmt); %let on=%upcase(&on); %if &fmt= %then %if &on=ON %then %do; %let fmt=1; put "[Superscript 1]"; %end; data _null_; %if &on=OFF %then %do; file cmds; put "[Superscript 0]"; put "[TableAutoFormat .Format = &fmt, %end; .Borders = 1, .Shading = 1, .Font = 1, .Color = %mend; 0, .AutoFit = 1, .HeadingRows = 1, .FirstColumn /* puttext writes text to the current cursor = 1, .LastRow = 0, .LastColumn = 0]"; location */ run; %macro puttext(text); %mend; put '[Insert ' '"' "&text" '"]'; /* go2bkmrk movse cursor to the specified %mend; bookmark */ /* putvar writes a data step variable to the %macro go2bkmrk(bm); current cursor location */ data _null_; %macro putvar(var); file cmds; cmd= '[Insert ' || '"' || &var || '"]'; put '[EditGoTo .Destination = "' "&bm" '"]'; put cmd; run; %mend; Applications Development /* nextcell moves to the next cell in a table Valid values for style are top, bottom, left, the number of times specified by n */ right, inside, outside and none. Bool can be 0 %macro nextcell(n); to turn off or 1 to turn on */ %if &n= %then %let n=1; %macro borders(style, bool); %do i=1 %to &n; %let style=%upcase(&style); put '[NextCell()]'; %if &style=TOP %then %do; %end; put "[BorderTop &bool]"; %mend; %end; %mend; %else %if &style=BOTTOM %then %do; put "[BorderBottom &bool]"; /* merge merges the specified number of cells %end; beginning at the insertion point */ %else %if &style=LEFT %then %do; %macro merge(count); put "[BorderLeft &bool]"; put "[CharRight &count,
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages5 Page
-
File Size-