Macros for SAS Application Developers
https://github.com/sasjs/core
mp_ds2md.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_ds2md(sashelp.class)
33 
34  @param [in] libds the library / dataset to create or read from.
35  @param [out] outref= (mdtable) Fileref to contain the markdown
36  @param [out] showlog= (YES) Set to NO to avoid printing markdown to the log
37 
38  <h4> SAS Macros </h4>
39  @li mf_getvarlist.sas
40  @li mf_getvarformat.sas
41 
42  @version 9.3
43  @author Allan Bowe
44 **/
45 
46 %macro mp_ds2md(
47  libds,
48  outref=mdtable,
49  showlog=YES
50 )/*/STORE SOURCE*/;
51 
52 /* check fileref is assigned */
53 %if %sysfunc(fileref(&outref)) > 0 %then %do;
54  filename &outref temp;
55 %end;
56 
57 %local vars;
58 %let vars=%upcase(%mf_getvarlist(&libds));
59 
60 %if %trim(X&vars)=X %then %do;
61  %put &sysmacroname: Table &libds has no columns!!;
62  %return;
63 %end;
64 
65 /* create the header row */
66 data _null_;
67  file &outref;
68  length line $32767;
69  call missing(line);
70  put '|'
71 %local i var fmt;
72 %do i=1 %to %sysfunc(countw(&vars));
73  %let var=%scan(&vars,&i);
74  %let fmt=%lowcase(%mf_getvarformat(&libds,&var,force=1));
75  "&var:&fmt|"
76 %end;
77  ;
78  put '|'
79 %do i=1 %to %sysfunc(countw(&vars));
80  "---|"
81 %end;
82  ;
83 run;
84 
85 /* write out the data */
86 data _null_;
87  file &outref mod dlm='|' lrecl=32767;
88  set &libds ;
89  length line $32767;
90  line='|`'!!cats(%mf_getvarlist(&libds,dlm=%str(%)!!' `|`'!!cats%()))!!' `|';
91  put line;
92 run;
93 
94 %if %upcase(&showlog)=YES %then %do;
95  options ps=max lrecl=max;
96  data _null_;
97  infile &outref;
98  if _n_=1 then putlog "# &libds" /;
99  input;
100  putlog _infile_;
101  run;
102 %end;
103 
104 %mend mp_ds2md;