Working Smarter, Not Harder With DDE (2)

  • Upload
    kauti

  • View
    229

  • Download
    0

Embed Size (px)

DESCRIPTION

Working Smarter, Not Harder With DDE (2)

Citation preview

  • Working Smarter, Not Harder with DDE:Did I Really Spend All That Time Just Formatting My Report?Bobby KiddVice PresidentSenior Statistical Portfolio AnalystFirst Horizon National Corp

  • Hey, Whats With the Acronyms! DDE stands for Dynamic Data ExchangeDDE in SAS gives you the ability to read from, write to, and issue program commands to many Microsoft Applications such as Excel and WordSince most reporting is done with Excel we will limit ourselves to that program

  • But I Can Already Read From and Write To ExcelProc Import and Proc Export both require the data to start at the top right corner, Cell A1 of the first sheet.DDE, however, can read from and write to any cell or group of cells in any sheet in the file.It can also create new rows, columns, and worksheets.

  • But, thats not all!In addition to the freedom of defining where you want the dataYou can change the format and the fontCopy and Paste from other cells, worksheets and even other files.Create new new formulasAdd Worksheets, Columns and RowsIn short, just about everything you can do in Excel, you can program into DDE

  • OK, But why would I want to?The less man(ual) handling of the data, the betterDont have to worry about copying over dataDont have to remember to format those 1 or 2 cells down in the corner of the sheet.How many times have you had to go back and fix typos and formatting errorsData provided from someone else's formatted reportsDo you really enjoy copying, pasting and formatting the same report every day, week, month when you can make SAS do it all for you in seconds.

  • The BasicsVery similar to working with flat filesStarts with filename statementDDE triplet: program|filepath[filename]sheet!locationFilename sample DDE excel|c:\foo\[bar.xls]yourdatahere!R4c5;Read data with input statementWrite data with put statement

  • MildRead DataBe mindful of informatsfilename readme dde 'excel|C:\...\SAS Pres\[samples.xls]Read Me!R5C3:R14C6';

    data DDERead;infile readme;informat Name $8. Sex $1. Purchase_Amount dollar8.2 State$2.;input Name$ Sex$ Purchase_Amount State$;run;

  • Mild

  • MildWrite DataJust like writing to flat filefilename writeme1 dde 'excel|C:\...\SAS Pres\[samples.xls]Write Me!R5C2:R8C3';filename writeme2 dde 'excel|C:\...\SAS Pres\[samples.xls]Write Me!R12C2:R13C3';data _null_;set state;file writeme1;put State Total;run;data _null_;set sex;file writeme2;put Sex Total;run;

  • MediumIncrease ControlUse notab to gain control over delimitersWhen reading use dlm=09x for the tab keyFlat file default is space delimited so Abel C would be two fieldsfilename readme dde 'excel|C:\...\SAS Pres\[samples.xls]Read Me!R5C3:R14C6' notab;

    data readdde;infile readme dlm = '09'x;informat Name $8. Sex $1. Purchase_Amount dollar8.2 State$2.;input Name Sex Purchase_Amount State;run;

  • MediumWithout dlm=09x and notab

    With dlm=09x and notab

  • MediumControl Write delimitersUse notab to prevent SAS from changing spaces into tabsUse 09x to place tab delimiters where you want them.filename MinP dde 'excel|C:\...\SAS Pres\[samples.xls]Write Me!R16C2:R16C3' notab;filename MaxP dde 'excel|C:\...\SAS Pres\[samples.xls]Write Me!R19C2:R19C3' notab;data _null_;set min;file MinP;Put Name '09'x Purchase_Amount;run;data _null_;set max;file MaxP;Put Name '09'x Purchase_Amount;run;

  • MediumWithout notab, with 09x: spaces become tabs

    With notab, without 09x: everything is one field

  • HotBeyond Data: Formatting, formulas, filesIts not a virus: Macrofun.exeAvailable from MicrosoftMacro4 helpfileContains most if not all Excel macro4 functionsFilename cmds dde 'excel|system';Allows SAS to issue system commandsOpen, save, and close files, etc.

  • HotMacro FunctionsExample Format.font

    Function Name and syntax given in detail.Parameter descriptions and values are documentedOnly need to specify parameters as far as you needUse with Put statement: put '[format.font("script",,1)]';

  • HotExcel must be opened firstManualEasy but tediousX commandIssues DOS commandsNoxwait, noxsync allow SAS to run concurrently with ExcelDoesnt like spacesUse cd to reach folders with spaces in names

  • HotStart ExcelOptions noxwait noxsync;x 'cd c:\Program files\microsoft office';x 'Office10\excel.exe';Open fileUse sleep function to ensure excel has loaded before issuing commands.Commands still issued with put statementsfilename cmds dde 'excel|system'; data _null_; file cmds; put '[file-open("C:\...\SAS Pres\sample.xls")]'; run;

  • HotImplementationfilename cmds dde 'excel|system';data _null_; data _null_;file cmds; file cmds;put '[select("r5c2:r8c2")]'; put [select(r5c3:r8c3)];put '[format.font(times new roman",,1)]'; put [format.number($#,###.00)];run; run;

  • HotSave, close and QuitSave.as() to save fileFile-close() to close the wookbookQuit() to end excelCommands still issued with put statementsdata _null_;file cmds; put '[save.as("C:\...\Sas Pres\sample.xls")]'; put '[file-close()]'; put '[quit()]';run;

  • As Hot As You Can TakeWrite formulas:put [formula("=sum(R[-19]C:R[-14]C)/R38C20")];put [formula("=sum(R12C20:R17C20)/R38C20")];Combine with ODS html output into .xlsUse activate(filename) and workbook.activate(sheet) to choose between file sheetUse select(cellrange), copy(), paste()Combine with Macro VariablesAutomatically change rows, columns with monthDefine columns as function of monthMacros only resolve inside double quotesput "[formula(""=sum(R[-19]C:R[-14]C)/R38C&col."")]";

  • Complete AutomationRead, Write and Manipulate Excel all from within SASWith use of SAS wakeup function can schedule reports to be ready when you show up to workHit F8 and walk away

  • Questions