Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mp_mdtablewrite.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Create a Markdown Table from a dataset
4 @details A markdown table is a simple table representation for use in
5 documents written in markdown format.
6
7 An online generator is available here:
8 https://www.tablesgenerator.com/markdown_tables
9
10 This structure is also used by the Macro Core library for documenting input/
11 output datasets, as well as the sasjs/cli tool for documenting inputs/outputs
12 for web services.
13
14 We take the standard definition one step further by embedding the informat
15 in the table header row, like so:
16
17 |var1:$32|var2:best.|var3:date9.|
18 |---|---|---|
19 |some text|42|01JAN1960|
20 |blah|1|31DEC1999|
21
22 Which resolves to:
23
24 |var1:$32|var2:best.|var3:date9.|
25 |---|---|---|
26 |some text|42|01JAN1960|
27 |blah|1|31DEC1999|
28
29
30 Usage:
31
32 %mp_mdtablewrite(libds=sashelp.class,showlog=YES)
33
34
35 <h4> SAS Macros </h4>
36 @li mf_getvarlist.sas
37 @li mf_getvarformat.sas
38
39 @param [in] libds= the library / dataset to create or read from.
40 @param [out] fref= Fileref to contain the markdown. Default=mdtable.
41 @param [out] showlog= set to YES to show the markdown in the log. Default=NO.
42
43 @version 9.3
44 @author Allan Bowe
45**/
46
47%macro mp_mdtablewrite(
48 libds=,
49 fref=mdtable,
50 showlog=NO
51)/*/STORE SOURCE*/;
52
53/* check fileref is assigned */
54%if %sysfunc(fileref(&fref)) > 0 %then %do;
55 filename &fref temp;
56%end;
57
58%local vars;
59%let vars=%mf_getvarlist(&libds);
60
61/* create the header row */
62data _null_;
63 file &fref;
64 length line $32767;
65 put '|'
66%local i var fmt;
67%do i=1 %to %sysfunc(countw(&vars));
68 %let var=%scan(&vars,&i);
69 %let fmt=%mf_getvarformat(&libds,&var,force=1);
70 "&var:&fmt|"
71%end;
72 ;
73 put '|'
74%do i=1 %to %sysfunc(countw(&vars));
75 "---|"
76%end;
77 ;
78run;
79
80/* write out the data */
81data _null_;
82 file &fref mod dlm='|' lrecl=32767;
83 set &libds ;
84 length line $32767;
85 line=cats('|',%mf_getvarlist(&libds,dlm=%str(,'|',)),'|');
86 put line;
87run;
88
89%if %upcase(&showlog)=YES %then %do;
90 options ps=max;
91 data _null_;
92 infile &fref;
93 input;
94 putlog _infile_;
95 run;
96%end;
97
98%mend mp_mdtablewrite;