Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Copyright © 2009, SAS Institute Inc. All rights reserved. Neat Code from Rick Rick Langston, SAS Institute Inc."— Presentation transcript:

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

2 Copyright © 2009, SAS Institute Inc. All rights reserved. SAS Examples to discuss Avoiding attachment problems Using Windows API calls easily Creating SAS data sets from HTML tables Informatting binary dates Getting combinations of letters

3 Copyright © 2009, SAS Institute Inc. All rights reserved. Avoiding 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!

4 Copyright © 2009, SAS Institute Inc. All rights reserved. Avoiding 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 " put " l=length(record);"; put " record:$hex72.;"; put " l2=l/2; record $varying36. l2;"; put " cards;"; run;

5 Copyright © 2009, SAS Institute Inc. All rights reserved. Avoiding 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;

6 Copyright © 2009, SAS Institute Inc. All rights reserved. Avoiding attachment problems *-----wrap up with a run-----; data _null_; file "sascode.txt" mod; put 'run;'; run;

7 Copyright © 2009, SAS Institute Inc. All rights reserved. Avoiding attachment problems data _null_; infile cards; file 'traj.zip' recfm=f lrecl=1; record:$char72.; l=length(record); record:$hex72.; l2=l/2; record $varying36. l2; cards; 504B BFC38EEDAEEA7A AD D F F 61646A D756C C E E48B18EEF4D C595DB6EDA401086AF E28CCD456F8891E B FD100000D6FF0B run;

8 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; ;;;;

9 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;

10 Copyright © 2009, SAS Institute Inc. All rights reserved. Using Windows API calls easily

11 Copyright © 2009, SAS Institute Inc. All rights reserved. Using Windows API calls easily Paper on this subject: Richard Devenezias information:

12 Copyright © 2009, SAS Institute Inc. All rights reserved. Creating SAS data sets from HTML tables My paper from SAS Global Forum 2009: %readhtml macro defined and discussed Only a few techniques highlighted here

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

14 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 ) 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; x $char1.; if x='<' then upcase=1; else if upcase and (x=' ' or x='>') then upcase=0; if upcase then x=upcase(x); x $char1.; if eof; call symputx('filesize',_n_); run;

15 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 ) 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; x $char1.; if x='<' then upcase=1; else if upcase and (x=' ' or x='>') then upcase=0; if upcase then x=upcase(x); x $char1.; if eof; call symputx('filesize',_n_); run;

16 Copyright © 2009, SAS Institute Inc. All rights reserved. Creating SAS data sets from HTML tables data 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='&filesize then leave; col=c; obscount+1; output; end; return;

17 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;

18 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= F fmtname=PDYMD type=I label=10959 label=02JAN1990 start= F fmtname=PDYMD type=I label=10960 label=03JAN1990 start= F fmtname=PDYMD type=I label=10961 label=04JAN1990 start= F fmtname=PDYMD type=I label=10962 label=05JAN1990 start= F

19 Copyright © 2009, SAS Institute Inc. All rights reserved. Informatting binary dates proc format cntlin=temp; data _null_; x=input(' f'x,pdymd4.); put x=date9. +1 '(should be 01JAN1990)'; x=input(' f'x,pdymd4.); put x=date9. +1 '(should be 31DEC1999)'; x=input(' f'x,pdymd4.); put x=date9. +1 '(should be 01JAN2000)'; x=input(' f'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)

20 Copyright © 2009, SAS Institute Inc. All rights reserved. Getting combinations of letters data 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=( ) then output; end; run;

21 Copyright © 2009, SAS Institute Inc. All rights reserved. Getting combinations of letters data _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;

22 Copyright © 2009, SAS Institute Inc. All rights reserved. Getting combinations of letters data _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;

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


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

Similar presentations


Ads by Google