Download presentation
Presentation is loading. Please wait.
Published byPhilippa Mitchell Modified over 8 years ago
1
Using the Macro Facility to Create HTML, XML and RTF output Rick Langston, SAS Institute Inc.
2
What we can use Macro to do Can generate any text, not just SAS code, such as... HTML and XML RTF for Word documents Examples for HTML and RTF shown herein Using the new experimental PROC STREAM
3
Macro is like the cormorant
4
Basic example filename new 'mytest.htm' recfm=v lrecl=32767; %macro make_table(nrows,ncols); %do i=1 %to &nrows; %do j=1 %to &ncols; &j %end; %end; %mend; proc stream outfile=new; begin %make_table(2,3) ;;;;
5
What is produced 1 2 3 1 2 3
6
What is produced 1 2 3 1 2 3
7
/* adding newlines */ %macro make_table(nrows,ncols); &streamdelim newline; %do i=1 %to &nrows; &streamdelim newline; %do j=1 %to &ncols; &streamdelim newline; &j %end; &streamdelim newline; %end; &streamdelim newline; %mend; proc stream outfile=new; begin %make_table(2,3) ;;;;
8
What is produced 1 2 3 1 2 3
9
Using the... tags filename prefmt temp; data _null_; file prefmt; input; put _infile_; cards4; this is some preformatted text that we would like to see before our table ;;;;
10
%macro make_table(nrows,ncols); &streamdelim newline; &streamdelim readfile prefmt; &streamdelim newline; %do i=1 %to &nrows; &streamdelim newline; %do j=1 %to &ncols; &streamdelim newline; &j %end; &streamdelim newline; %end; &streamdelim newline; %mend;
11
What is produced this is some preformatted text that we would like to see before our table 1 2 3 1 2 3
12
/* using & and % */ %macro make_table(nrows,ncols); Using row %nrstr(&) col of &nrows %nrstr(&) &ncols &streamdelim newline; Use this link to find out more. %do i=1 %to &nrows; %do j=1 %to &ncols; &j %end; %end; %mend;
13
/* using & and % */ %macro make_table(nrows,ncols); Using row %nrstr(&) col of &nrows %nrstr(&) &ncols &streamdelim newline; Use this link to find out more. %do i=1 %to &nrows; %do j=1 %to &ncols; &j %end; %end; %mend;
14
/* using & and % */ %macro make_table(nrows,ncols); Using row %nrstr(&) col of &nrows %nrstr(&) &ncols &streamdelim newline; Use this link to find out more. %do i=1 %to &nrows; %do j=1 %to &ncols; &j %end; %end; %mend;
15
A tip for handling input HTML File my.htm contains < 1 & 2 > We try the following: proc stream outfile=out; begin &streamdelim; %include 'my.htm'; ;;;; We get noise such as: WARNING: Apparent symbolic reference LT not resolved.
16
A tip for handling input HTML proc stream outfile=out; begin &streamdelim; %let amp=P %let lt=L %let gt=n %include 'my.htm'; ;;;;
17
Using/Creating RTF files These are script and text based files Alternate creation from Word Can’t do this with DOC/DOCX because it’s binary This example courtesy of Don Henderson
19
What’s in the RTF file?
20
One way to make our RTF file filename classrtf 'class.rtf' recfm=v lrecl=32767; filename new 'new.rtf' recfm=v lrecl=32767; proc stream outfile=new quoting=both asis; begin %let name=Alfred; %let height=69; %let weight=112.5; %let sex=male; %let age=14; &streamdelim; %include classrtf; ;;;;
22
Example macro for alternate method %macro each_student(obsnum); %global name height weight sex age; data _null_; set sashelp.class(firstobs=&obsnum obs=&obsnum); call symputx('name',name); call symputx('height',height); call symputx('weight',weight); call symputx('sex',put(sex,$sex.)); call symputx('age',age); run; filename new "&name..rtf" recfm=v lrecl=32767; filename classrtf 'class.rtf' recfm=v lrecl=32767; proc stream outfile=new quoting=both asis; begin &streamdelim; %include classrtf; ;;;; %mend;
23
Invoking the macro data _null_; set sashelp.class(obs=3); rc = dosubl( cats('%each_student(', _n_, ');' ) ); run; Use DOSUBL instead of CALL EXECUTE Creates Alfred.rtf, Alice.rtf, Barbara.rtf
24
Use of DOSUBL %macro getcount(data); %global numobs; Proc sql; select count(*) into:numobs from &data; quit; %mend; data temp; do x=1 to 10; output; end; run; Code in red is generated by the macro
25
Use of DOSUBL filename outfile temp; proc stream outfile=outfile; begin some text here %getcount(work.temp) obs count for work.temp is &numobs. %let x=%sysfunc(dosubl(%nrstr(%getcount(work.temp)))); obs count for work.temp is &numobs. text after the dosub call ;;;; data _null_; infile outfile; input; put _infile_; run;
26
Use of DOSUBL some text here Proc sql; select count(*) into:numobs from work.temp; quit; obs count for work.temp is. obs count for work.temp is 10. text after the dosub call
27
Summary Any scripting language is possible HTML, RTF given as examples Deal with & and % via %nrstr if needed PROC STREAM available as experimental in 9.3 readfile to read a file with no tokenizing (9.3m2)
28
Contact Info Rick Langston, SAS Institute Inc. Rick.Langston@sas.com Rick.Langston@sas.com Don Henderson’s blog entry http://www.sascommunity.org/wiki/SAS%C2%AE_Serve r_Pages:_Generating_Dynamic_Content http://www.sascommunity.org/wiki/SAS%C2%AE_Serve r_Pages:_Generating_Dynamic_Content
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.