2005 david woods

34
Slide 1 David Woods SUNZ Conference 14 th December 2005 Real-World Inputs and Outputs

Upload: shane-gibson

Post on 16-Apr-2017

687 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: 2005 David Woods

Slide 1

David WoodsSUNZ Conference

14th December 2005

Real-World Inputs and Outputs

Page 2: 2005 David Woods

Slide 2

Overview

Reading and writing to Excel Making controlled PDFs Submitting commands to Windows Sending email Inputs from the Web

Page 3: 2005 David Woods

Slide 3

Outputting to Excel

Typical Scenario:Need to design a report from SAS datasets to be read by regular analysts in Excel.

Typical Solution:SAS dataset is ‘Exported’ either as an Excel file, or as a comma delimited file.

Page 4: 2005 David Woods

Slide 4

Outputting to Excel

Problem:

PROC EXPORT DATA= SASHELP.Tourism OUTFILE= "C:\output.xls" DBMS=EXCEL REPLACE; SHEET="sheet1"; RUN;

UGLY!

Page 5: 2005 David Woods

Slide 5

Dynamic Data Exchange

We can use Dynamic Data Exchange (DDE) to read and write directly to Excel.

Uses X4ML to instruct Excel Can read/write to individual cells or

ranges Can apply formatting to cells Can use conditional formatting and output

Page 6: 2005 David Woods

Slide 6

Desired Excel Output

Page 7: 2005 David Woods

Slide 7

Some code for starting DDE

Open Excel for writing:options noxsync noxwait xmin;filename sas2xl dde 'excel|system';data _null_;

length fid rc start stop time 8;fid=fopen('sas2xl','s');if(fid le 0) then do; rc=system('start excel'); start=datetime(); stop=start+10; do while(fid le 0); fid=fopen('sas2xl','s'); time=datetime(); if(time ge stop) then fid=1; end;

end;rc=fclose(fid);

run;

Set up new workbook:/* Get rid of old workbook and *//* create new one. Save it. */%let path = C:\SUNZ;%let name = My workbook;data _null_;

file sas2xl;put '[file.close(false)]';put '[new(1)]';put '[error(false)]';put "[save.as(""&path\&name"")]";

run;

Page 8: 2005 David Woods

Slide 8

DDE – putting values in cells

filename recrange dde "excel|[My workbook.xls]Sheet1!r2c2:r3c6" notab;

%let t = '09'x;data _null_;

file recrange;put &t &t 'Holidays in Spain' &t 'UK real personal' &t &t 'The consumer price';output;put 'Year' &t &t 'by US residents' &t 'disposable income' &t 'The UK population' &t 'index in Spain';output;

run;

filename recrange clear;

Set a range

Write values toThe range

Clear the range

Page 9: 2005 David Woods

Slide 9

DDE – putting values in cells

filename recrange dde "excel|[My workbook.xls]Sheet1!r4c2:r13c7" notab;

data _null_;set sashelp.tourism(obs=10);file recrange;put year &t &t vsp &t pdi &t pop &t cpisp;

run;

filename recrange clear;

Set a range

Write values toThe range

Clear the range

Page 10: 2005 David Woods

Slide 10

DDE – formatting

Submit commands in data steps calling ‘sas2xl’

Zoom to 85%data _null_;

file sas2xl;put '[zoom(85)]';

run;

Page 11: 2005 David Woods

Slide 11

DDE - formatting

Make headings bold and purple:data _null_;

file sas2xl;put '[select("r2:r3")]';put '[format.font("Arial",10,true,false,false,

false,17,false,false)]';run;

Functions are X4ML functions

Complete help can be found in MACROFUN.hlp… go to:

http://www.microsoft.com/learning/And search for MACROFUN.EXE

Eg: FORMAT.FONT(name_text, size_num, bold, italic, underline, strike, color, outline, shadow)

Page 12: 2005 David Woods

Slide 12

DDE - formatting

data _null_;file sas2xl;

/* Set background colors */put "[select(""r2c2:r3c2,r2c4:r3c7"")]";put "[patterns(1,0,1)]";put "[select(""r4c2:r13c2"")]";put "[patterns(1,0,39)]";put "[select(""r4c4:r13c7"")]";put "[patterns(1,0,43)]";

/* Set number formats */put '[select("c4,c6")]';put '[format.number("0.00")]';put '[select("c7")]';put '[format.number("0.0000")]';

/* Center all cells */put '[select("c2:c7")]';put '[alignment(3)]';

run;

data _null_;file sas2xl;

/* Set column widths */put '[column.width(7.14,"c2",false)]';put '[column.width(2,"c1,c3",false)]';put '[column.width(18.2,"c4:c7",false)]';

/* Set outlines borders */put '[select("r2c2:r13c2,r2c4:r13c7")]';put '[border(2)]';

/* Set internal borders */put '[select("r2c4:r13c6")]';put '[border(,,1)]';

/* Save and close the file */put '[error(false)]';put '[save]';put '[file.close(false)]';put '[quit()]';

run;

Page 13: 2005 David Woods

Slide 13

Finished output

Page 14: 2005 David Woods

Slide 14

DDE – reading Excel sheets

DDE can also be used to read specified ranges from Excel sheets.

data _null_; file sas2xl; put '[file.close(false)]'; put '[open("C:\My workbook")]'; put '[workbook.activate("Sheet1")]';run;

filename recrange dde "excel|[My workbook.xls]Sheet1!r6c4:r8c5" notab;data workbook; infile recrange delimiter='09'x lrecl=1000; input VAR1 $ VAR2 $;run;filename recrange clear;

First open Excel as earlier, and close it again at the end.

Page 15: 2005 David Woods

Slide 15

Making PDFs

Often, it’s necessary to make a document in PDF format that looks exactly like you want it to.

Example: The invoice for the conference

Page 16: 2005 David Woods

Slide 16

Making PDFs – proc ganno

Create a dataset with one observation for each item on the PDF.Text entries: function='label'; x=5; y=94.25; style = '"Arial"'; position = '6'; size=3; text='SUNZ'; output;

Page 17: 2005 David Woods

Slide 17

Making PDFs – proc ganno

Drawing lines: function='move'; x=1; y=87; output; function='draw'; x=95; y=87; color = 'black'; line = 1; size=8; output;

Move cursor to start of line

Draw line to finishing position

Page 18: 2005 David Woods

Slide 18

Making PDFs – proc ganno

Finally, invoke the ODS PDF output destination. ods listing close; ods pdf bookmarklist=none file="C:\invoice.pdf";

goptions hpos=100 vpos=100; proc ganno annotate=annodata; run; quit; ods pdf close;

• PROC GANNO very powerful, used for assembling graphs one element at a time, or customising graphs

• Requires SAS/GRAPH

Page 19: 2005 David Woods

Slide 19

Making PDFs – proc report

No SAS/GRAPH… need to use PROC REPORTHarder!

Approach: • Activate ODS PDF output destination• Call PROC REPORT to place elements on

pageProblems:• PROC REPORT is hard to customise the

appearance of• SAS help is almost useless

Page 20: 2005 David Woods

Slide 20

Making PDFs – proc report

•Biggest resource is the paper by Brian T. Schellenberger at SAS

•This paper is written entirely using PROC REPORT and ODS

•Found at http://support.sas.com/rnd/base/topics/odsprinter/qual.pdf

Page 21: 2005 David Woods

Slide 21

Making PDFs – proc report

options topmargin=1.25 bottommargin=0.3 leftmargin=0.75 rightmargin=0.75 papersize=a4;

ods path work.templat(update) sasusr.templat(update) sashelp.tmplmst(read);

proc template;define Style styles.format; parent = styles.Printer;replace fonts /

"docFont" = ("Arial", 10pt) "headingFont" = ("Arial", 10pt,bold) "headingEmphasisFont" = ("Arial", 10pt,bold italic) "FixedFont" = ("Courier New, Courier",8pt) "FixedHeadingFont" = ("Courier New, Courier",8pt,bold) "FixedStrongFont" = ("Courier New, Courier",8pt,bold) "FixedEmphasisFont" = ("Courier New, Courier",8pt,italic) "EmphasisFont" = ("Arial", 10pt) "StrongFont" = ("Arial", 8pt, bold) "TitleFont" = ("Arial", 8pt) "TitleFont2" = ("Arial", 8pt)

;end;

run;

First steps: Set some options and define a new style template

Page 22: 2005 David Woods

Slide 22

Making PDFs – proc report

Activate the ODS PDF destination: ods listing close; ods printer style=format columns=1 pdf notoc

file="C:\sample.pdf";

options cardimage nodate nonumber; ods escapechar = '\'; ods printer startpage=no;

title; footnote;

Page 23: 2005 David Woods

Slide 23

Making PDFs – proc report

Construct a dataset that contains the information you wish to output. Then call PROC REPORT. data text; text = 'This is the line of text we wish to display.'; run;

proc report data=text nowindows noheader style={rules=none frame=void just=l cellspacing=0 cellpadding=0}; define text / style=data; run; quit;

This will output a normal looking line of text

Page 24: 2005 David Woods

Slide 24

Making PDFs – proc report

Make macros! %macro standardtext(text);

data text; length text $9999; text = resolve(&text); run;

proc report data=text nowindows noheader style={rules=none frame=void just=l cellspacing=0 cellpadding=0}; define text / style=data; run; quit;

%mend standardtext;

%standardtext("This is the line of text we wish to display.");

Page 25: 2005 David Woods

Slide 25

Making PDFs – proc report

Changing appearance of output:• Use escape character ‘\’• To get This is italic

Use %standardtext("This is \S={font_style=italic}italic\S={}");• Some style modifiers are: font_face,

font_size, font_style, font_weight• Can also use shading and borders in the

report table

Page 26: 2005 David Woods

Slide 26

Note on manipulating files

• Any commands you can execute from the command prompt you can execute with SAS:

• Delete a filesystask command "del C:\sample.pdf" taskname=del;waitfor del;

• Rename a filesystask command "rename C:\sample.pdf letter.pdf" taskname=ren;waitfor ren;

• Run a Word macrosystask command " start /wait /min winword.exe ""C:\mydoc.doc"" /mymacro " taskname=runmacro;waitfor runmacro;

Page 27: 2005 David Woods

Slide 27

Sending Email using SAS

Add the following lines to your SAS config file (default - C:\Program Files\SAS\SAS9.1\nls\en\SASV9.CFG) -emailsys SMTP -emailhost 'smtp.offlode.com' -emailid '[email protected]'

Then:data _null_; file myemail; put 'Dear David'; put ' '; put 'Thank you for your patience ' @; put 'in sitting through these talks.'; put ' '; put 'Please find attached a PDF'; put ' '; put 'David Woods';run;

filename myemail email to = ("[email protected]") from = "David Woods

<[email protected]>" subject = "Have a nice day" attach = "C:\sample.pdf";

Page 28: 2005 David Woods

Slide 28

Getting stuff from the web

• Define a webpage as a fileref• Load the HTML source code into a dataset• Search through the dataset to find the

required info

filename webpage url "http://www.nzx.com/";

data nzx; infile webpage length=len lrecl=4000; input htmlline $varying4000. len;

run;

Page 29: 2005 David Woods

Slide 29

NZX share prices

Examine the source code to find some string which characterizes your target information, then extract the desired info into a new variable.

Page 30: 2005 David Woods

Slide 30

Google celebrity search

%let firnam = David;%let surnam = Woods;

filename webpage url "http://www.google.co.nz/search?hl=en&q=%22&firnam+&surnam%22&btnG=Google+Search&meta=";

data _null_;infile webpage length=len lrecl=4000;input htmlline $varying4000. len;if index(htmlline,'table width')>0 AND index(htmlline,'</b> of about <b>')>0 then do; call symput('number',substr(substr(htmlline,index(htmlline,

'</b> of about <b>')+17),1,index(substr(htmlline,index(htmlline, '</b> of about <b>')+17),'<')-1)); stop;end;

run;

Page 31: 2005 David Woods

Slide 31

Google celebrity search

Page 32: 2005 David Woods

Slide 32

Google celebrity search

Can rank people by celebrity status:

Application for detection of false names, if count is above a certain threshold they might be fake.

Page 33: 2005 David Woods

Slide 33

Useful References

• SAS help• SUGI papers• DDE:

– http://www.sas-consultant.com/professional/SUGI27-HOW-DDE.pdf

– http://www.sas-consultant.com/professional/SUGI26-Against-All-ODS-1.pdf

– http://www.nesug.org/html/Proceedings/nesug04/ap/ap06.pdf• ODS PDF output

– http://support.sas.com/rnd/base/topics/odsprinter/qual.pdf– http://support.sas.com/rnd/base/topics/odsprinter/usepaper.pdf

Page 34: 2005 David Woods

Slide 34