copyright © 2009, sas institute inc. all rights reserved. neat code from rick rick langston, sas...

23
Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc.

Upload: kathleen-colten

Post on 31-Mar-2015

229 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

Neat Code from RickRick Langston, SAS Institute Inc.

Page 2: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

SAS Examples to discuss

Avoiding email attachment problems

Using Windows API calls easily

Creating SAS data sets from HTML tables

Informatting binary dates

Getting combinations of letters

Page 3: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

Avoiding email attachment problems

Need to send binary data?

Especially need to send a zip file?

So many mail servers reject those

Send a text file using SAS!

Page 4: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

Avoiding email attachment problems:

%let filename=traj.zip;

*-----write out the initial DATA step code-----*;

data _null_; file "sascode.txt";

put "data _null_; infile cards;";

put " file '&filename.' recfm=f lrecl=1;";

put " input @1 record:$char72.@;

put " l=length(record);";

put " input @1 record:$hex72.;";

put " l2=l/2; put @1 record $varying36. l2;";

put " cards;";

run;

Page 5: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

Avoiding email attachment problems

*-----write out the contents of the file in hex, 72 hex digits at a time-----*;

data _null_; infile "&filename." recfm=f lrecl=36 length=l;

file "sascode.txt" mod;

input;

length hex $72;

hex=putc(_infile_,'$hex',l*2);

put hex;

run;

Page 6: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

Avoiding email attachment problems

*-----wrap up with a run-----;

data _null_; file "sascode.txt" mod;

put 'run;';

run;

Page 7: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

Avoiding email attachment problems

data _null_; infile cards;

file 'traj.zip' recfm=f lrecl=1;

input @1 record:$char72.;

l=length(record);

input @1 record:$hex72.;

l2=l/2; put @1 record $varying36. l2;

cards;

504B0304140000000800409BFC38EEDAEEA7A0020000AD0800001D001500632F7372632F

61646A7573744D756C74537461727456616C7565732E63555409000388558E48B18EEF4D

5578040087006400C595DB6EDA401086AF178977185125E28CCD456F8891E84115510815

....

4B05060000000036003600FD100000D6FF0B000000

run;

Page 8: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

Using Windows API calls easily

filename sascbtbl temp;

data _null_; file sascbtbl;

input; put _infile_;cards4;

routine MessageBoxA minarg=4 maxarg=4 returns=long

module=user32 stackpop=called;

arg 1 num input format=pib4. byvalue;

arg 2 char input format=$cstr200.;

arg 3 char input format=$cstr200.;

arg 4 num input format=pib4. byvalue;

;;;;

Page 9: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

Using Windows API calls easily

data _null_;

button = modulen('MessageBoxA',0,

'Instructions: Click OK for X, Otherwise Click Cancel',

'Sample MessageBox Title',

1);

if button=1 then do;

put 'OK was clicked';

end;

if button=2 then do;

put 'Cancel was clicked';

end;

run;

Page 10: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

Using Windows API calls easily

Page 11: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

Using Windows API calls easily

Paper on this subject:

http://www2.sas.com/proceedings/sugi26/p281-26.pdf

Richard Devenezia’s information:

http://www.devenezia.com/downloads/sas/sascbtbl/

Page 12: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

Creating SAS data sets from HTML tables

My paper from SAS Global Forum 2009:

http://support.sas.com/resources/papers/proceedings09/052-009.pdf

%readhtml macro defined and discussed

Only a few techniques highlighted here

Page 13: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

Page 14: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

Creating SAS data sets from HTML tables /* This step makes a local copy of the URL contents into a temporary file.

This makes random access of the file simpler, and also gives us an

opportunity to find the total size of the file to use that value in a

subsequent LRECL= option. Also, we can upcase all the tags (anything between

< and blank or >) so that we can accept any casing combination. */

filename myfile temp;

data _null_; infile urltext recfm=f lrecl=1 end=eof; file myfile recfm=f lrecl=1;

retain upcase 0;

input @1 x $char1.;

if x='<' then upcase=1;

else if upcase and (x=' ' or x='>') then upcase=0;

if upcase then x=upcase(x);

put @1 x $char1.;

if eof;

call symputx('filesize',_n_);

run;

Page 15: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

Creating SAS data sets from HTML tables /* This step makes a local copy of the URL contents into a temporary file.

This makes random access of the file simpler, and also gives us an

opportunity to find the total size of the file to use that value in a

subsequent LRECL= option. Also, we can upcase all the tags (anything between

< and blank or >) so that we can accept any casing combination. */

filename myfile temp;

data _null_; infile urltext recfm=f lrecl=1 end=eof; file myfile recfm=f lrecl=1;

retain upcase 0;

input @1 x $char1.;

if x='<' then upcase=1;

else if upcase and (x=' ' or x='>') then upcase=0;

if upcase then x=upcase(x);

put @1 x $char1.;

if eof;

call symputx('filesize',_n_);

run;

Page 16: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

Creating SAS data sets from HTML tablesdata detail(keep=text col);

infile myfile recfm=f lrecl=&filesize. column=c missover;

array which{3} $8 _temporary_ ('TABLE','TR','TD');

length tag $8;

do i=1 to 3;

tag='<'||which{i}; link readfile;

tag='</'||which{i}; link readfile;

end;

return;

readfile:;

obscount=0;

text=tag;

input @1 @;

do while(1);

input @(trim(tag)) @;

if c>&filesize then leave;

col=c; obscount+1;

output;

end;

return;

Page 17: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

Informatting binary dates

data temp;

fmtname='PDYMD'; type='I';

do label='01jan1990'd to '31dec2010'd;

y=year(label)-1900; m=month(label); d=day(label);

string=put(y,z3.)||put(m,z2.)||put(d,z2.)||'F';

start=input(string,$hex8.);

output;

end;

keep fmtname type start label;

run;

Page 18: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

Informatting binary dates

27 data _null_; set temp(obs=5);

28 put fmtname= type= label= label=date9. start=$hex8.;

29 run;

fmtname=PDYMD type=I label=10958 label=01JAN1990 start=0900101F

fmtname=PDYMD type=I label=10959 label=02JAN1990 start=0900102F

fmtname=PDYMD type=I label=10960 label=03JAN1990 start=0900103F

fmtname=PDYMD type=I label=10961 label=04JAN1990 start=0900104F

fmtname=PDYMD type=I label=10962 label=05JAN1990 start=0900105F

Page 19: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

Informatting binary datesproc format cntlin=temp;

data _null_;

x=input('0900101f'x,pdymd4.); put x=date9. +1 '(should be 01JAN1990)';

x=input('0991231f'x,pdymd4.); put x=date9. +1 '(should be 31DEC1999)';

x=input('1000101f'x,pdymd4.); put x=date9. +1 '(should be 01JAN2000)';

x=input('1101231f'x,pdymd4.); put x=date9. +1 '(should be 31DEC2010)';

run;

x=01JAN1990 (should be 01JAN1990)

x=31DEC1999 (should be 31DEC1999)

x=01JAN2000 (should be 01JAN2000)

x=31DEC2010 (should be 31DEC2010)

Page 20: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

Getting combinations of lettersdata temp;

do a=1 to 6;

do b=1 to 6;

do c=1 to 6;

do d=1 to 6;

do e=1 to 6;

do f=1 to 6;

if 2**a+2**b+2**c+2**d+2**e+2**f=(2+4+8+16+32+64)

then output;

end;

end;

end;

end;

end;

end;

run;

Page 21: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

Getting combinations of lettersdata _null_; set temp;

value='festal';

comb=substr(value,a,1)||

substr(value,b,1)||

substr(value,c,1)||

substr(value,d,1)||

substr(value,e,1)||

substr(value,f,1);

put comb=;

run;

Page 22: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.

Getting combinations of lettersdata _null_; set temp;

value='festal';

comb=substr(value,a,1)||

substr(value,b,1)||

substr(value,c,1)||

substr(value,d,1)||

substr(value,e,1)||

substr(value,f,1);

put comb=;

run;

Page 23: Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc

Copyright © 2009, SAS Institute Inc. All rights reserved.Copyright © 2009, SAS Institute Inc. All rights reserved.