![]() Chapter Contents |
![]() Previous |
![]() Next |
SAS Companion for the Microsoft Windows Environment |
Before you run these examples, you must first invoke Microsoft Excel and Microsoft Word, and open the spreadsheet or document used in the example.
Note: DDE examples are included in the host-specific
sample programs that you access from the Help menu.
Using the X Command to Open a DDE Server |
options noxwait noxsync; x 'excel'; /* you might need to specify */ /* the complete pathname */
Using DDE to Write Data To Microsoft Excel |
/* The DDE link is established using */ /* Microsoft Excel SHEET1, rows 1 */ /* through 100 and columns 1 through 3 */ filename random dde 'excel|sheet1!r1c1:r100c3'; data random; file random; do i=1 to 100; x=ranuni(i); y=10+x; z=x-10; put x y z; end; run;
Using DDE to Write Data To Microsoft Word |
filename testit dde `winword|"c:\temp\testing.doc" !MARK' notab; data _null_; file testit; put ` This is a test.'; run;
Note: If you are writing to Microsoft Word97, use Visual Basic commands such as FileOpen.Name, FileSave, FileClose, and Insert. If the PUT statement contains a macro that Word97 does not understand, you will see this message:
Ambiguous name detected: TmpDDE
Using DDE to Read Data from Microsoft Excel |
/* The DDE link is established using */ /* Microsoft Excel SHEET1, rows 1 */ /* through 10 and columns 1 through 3 */ filename monthly dde 'excel|sheet1!r1c1:r10c3'; data monthly; infile monthly; input var1 var2 var3; run; proc print; run;
Using DDE to Read Data from Microsoft Word |
This example reads data from a Microsoft Word document at a given bookmark.
filename testit dde 'winword|"c:\temp\testing.doc" !MARK' notab; libname workdir `c:\temp'; /* Get ready to read the first bookmark. */ data workdir.worddata; length wordnum $5; infile testit; input wordnum $; run;
Using DDE and the SYSTEM Topic to Invoke Commands in an Application Using Excel |
/* This code assumes that Excel */ /* is installed on the current */ /* drive in a directory called EXCEL. */ options noxwait noxsync; x 'excel'; /* you might need to specify */ /* the entire pathname */ /* Sleep for 60 seconds to give */ /* Excel time to come up. */ data _null_; x=sleep(60); run; /* The DDE link is established using */ /* Microsoft Excel SHEET1, rows 1 */ /* through 20 and columns 1 through 3 */ filename data dde 'excel|sheet1!r1c1:r20c3'; data one; file data; do i=1 to 20; x=ranuni(i); y=x+10; z=x/2; put x y z; end; run; /* Microsoft defines the DDE topic */ /* SYSTEM to enable commands to be */ /* invoked within Excel. */ filename cmds dde `excel|system'; /* These PUT statements are */ /* executing Excel macro commands */ data _null_; file cmds; put '[SELECT("R1C1:R20C3")]'; put '[SORT(1,"R1C1",1)]'; put '[SAVE()]'; put '[QUIT()]'; run;
Using the NOTAB Option with DDE |
The NOTAB option also can be used to store full character strings, including embedded blanks, in a single spreadsheet cell. For example, if a link is established between the SAS System and the Excel application, and a SAS variable contains a character string with embedded blanks, each word of the character string is normally stored in a single cell. To store the entire string, including embedded blanks in a single cell, use the NOTAB option as in the following example:
/* Without the NOTAB option, column1 */ /* contains 'test' and column2 */ /* contains 'one'. */ filename test dde 'excel|sheet1!r1c1:r1c2'; data string; file test; a='test one'; b='test two'; put a $15. b $15.; run; /* You can use the NOTAB option to store */ /* each variable in a separate cell. To */ /* do this, you must force a tab */ /* ('09'x) between each variable, as in */ /* the PUT statement. */ /* After performing this DATA step, column1*/ /* contains 'test one' and column2 */ /* contains 'test two'. */ filename test dde 'excel|sheet1!r2c1:r2c2' notab; data string; file test; a='test one'; b='test two'; put a $15. '09'x b $15.; run;
Using the DDE HOTLINK |
/* Enter data into Excel SHEET1 in */ /* row 1 column 1. When you */ /* are through entering data, place */ /* any character in row 5 */ /* column 1, and the DDE link is */ /* terminated. */ filename daily dde 'excel|sheet1!r1c1' hotlink; filename status dde 'excel|sheet1!r5c1' hotlink; data daily; infile status length=flag; input @; if flag ne 0 then stop; infile daily length=b; input @; /* If data have changed, then the */ /* incoming record length */ /* is not equal to 0. */ if b ne 0 then do; input total $; put total=; output; end; run;
It is possible to establish multiple DDE sessions. The previous example uses two separate DDE links. When the HOTLINK option is used and there are multiple cells referenced in the item specification, if any one of the cells changes, then all cells are transmitted.
Unless the HOTLINK option is specified, DDE is performed as a single one-time data transfer. That is, the values currently stored in the spreadsheet cells at the time that the DDE is processed are values that are transferred.
Using the !DDE_FLUSH String to Transfer Data Dynamically |
DDE also
enables you to program when the DDE buffer is dumped
during a DDE link. Normally, the data in the DDE buffer are transmitted when
the DDE link is closed at the end of the DATA step. However, the special
string
'!DDE_FLUSH'
issued in a PUT statement instructs the SAS System to
dump the contents of the DDE buffer. This function allows you considerable
flexibility in the way DDE is used, including the capacity to transfer data
dynamically through the DATA step, as in the following example:
/* A DATA step window is displayed. */ /* Enter data as prompted. */ /* When you are finished, enter STOP */ /* on the command line. */ filename entry dde 'excel|sheet1!r1c1:r1c3'; dm 'pmenu off'; data entry; if _n_=1 then do; window ENTRY color=black #3 'This is data for Row 1 Column 1' c=cyan +2 var1 $10. c=orange #5 'This is data for Row 1 Column 2' c=cyan +2 var2 $10. c=orange #7 'This is data for Row 1 Column 3' c=cyan +2 var3 $10. c=orange; end; flsh='!DDE_FLUSH'; file entry; do while (upcase(_cmd_) ne 'STOP'); display entry; put var1 var2 var3 flsh; output; VAR1=''; VAR2=''; VAR3=''; end; stop; run; dm 'pmenu on';
Reading Missing Data |
... 10 John Raleigh Cardinals 11 Jose North Bend Orioles 12 Kurt Yelm Red Sox 13 Brent Dodgers ...
Here's the code that can read these data correctly into a SAS data set:
filename mydata dde 'excel|sheet1!r10c1:r20c3'; data in; infile mydata dlm='09'x notab dsd missover; informat name $10. town $char20. team $char20.; input name town team; run; proc print data=in; run;
In this example, the NOTAB option tells the SAS System
not to convert tabs that are sent from the Excel application into blanks.
Therefore, the tab character can be used as the delimiter between data values.
The DLM= option specifies the delimiter character, and
'09'x
is the
hexadecimal representation of the tab character. The DSD option specifies
that two consecutive delimiters represent a missing value. The default delimiter
is a comma. For more information about the DSD option, see SAS Language Reference: Dictionary. The
MISSOVER option prevents a SAS program from going to a new input line if it
does not find values in the current line for all the INPUT statement variables.
With the MISSOVER option, when an INPUT statement reaches the end of the
current record, values that are expected but not found are set to missing.
The INFORMAT statement forces the DATA step to use modified list input, which is crucial to this example. If you do not use modified list input, you receive incorrect results. The necessity of using modified list input is not DDE specific. You would need it even if you were using data in a CARDS statement, whether your data were blank- or comma-delimited.
![]() Chapter Contents |
![]() Previous |
![]() Next |
![]() Top of Page |
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.