Macros for SAS Application Developers
https://github.com/sasjs/core
mp_dsmeta.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Export dataset metadata to a single output table
4  @details Exports the dataset attributes and enginehost information, then
5  converts the datasets into a single output table in the following format:
6 
7 |ODS_TABLE:$10.|NAME:$100.|VALUE:$1000.|
8 |---|---|---|
9 |`ATTRIBUTES `|`Data Set Name `|`SASHELP.CLASS `|
10 |`ATTRIBUTES `|`Observations `|`19 `|
11 |`ATTRIBUTES `|`Member Type `|`DATA `|
12 |`ATTRIBUTES `|`Variables `|`5 `|
13 |`ATTRIBUTES `|`Engine `|`V9 `|
14 |`ATTRIBUTES `|`Indexes `|`0 `|
15 |`ATTRIBUTES `|`Created `|`06/08/2020 00:59:14 `|
16 |`ATTRIBUTES `|`Observation Length `|`40 `|
17 |`ATTRIBUTES `|`Last Modified `|`06/08/2020 00:59:14 `|
18 |`ATTRIBUTES `|`Deleted Observations `|`0 `|
19 |`ATTRIBUTES `|`Protection `|`. `|
20 |`ATTRIBUTES `|`Compressed `|`NO `|
21 |`ATTRIBUTES `|`Data Set Type `|`. `|
22 |`ATTRIBUTES `|`Sorted `|`NO `|
23 |`ATTRIBUTES `|`Label `|`Student Data `|
24 |`ATTRIBUTES `|`Data Representation `|`SOLARIS_X86_64, LINUX_X86_64, ALPHA_TRU64, LINUX_IA64 `|
25 |`ATTRIBUTES `|`Encoding `|`us-ascii ASCII (ANSI) `|
26 |`ENGINEHOST `|`Data Set Page Size `|`65536 `|
27 |`ENGINEHOST `|`Number of Data Set Pages `|`1 `|
28 |`ENGINEHOST `|`First Data Page `|`1 `|
29 |`ENGINEHOST `|`Max Obs per Page `|`1632 `|
30 |`ENGINEHOST `|`Obs in First Data Page `|`19 `|
31 |`ENGINEHOST `|`Number of Data Set Repairs `|`0 `|
32 |`ENGINEHOST `|`Filename `|`/opt/sas/sas9/SASHome/SASFoundation/9.4/sashelp/class.sas7bdat `|
33 |`ENGINEHOST `|`Release Created `|`9.0401M7 `|
34 |`ENGINEHOST `|`Host Created `|`Linux `|
35 |`ENGINEHOST `|`Inode Number `|`28314616 `|
36 |`ENGINEHOST `|`Access Permission `|`rw-r--r-- `|
37 |`ENGINEHOST `|`Owner Name `|`sas `|
38 |`ENGINEHOST `|`File Size `|`128KB `|
39 |`ENGINEHOST `|`File Size (bytes) `|`131072 `|
40 
41  Example usage:
42 
43  %mp_dsmeta(sashelp.class,outds=work.mymeta)
44  proc print data=work.mymeta;
45  run;
46 
47  For more details on creating datasets from PROC CONTENTS check out this
48  excellent [paper](
49  https://support.sas.com/resources/papers/proceedings14/1549-2014.pdf) by
50  [Louise Hadden](https://www.linkedin.com/in/louisehadden/).
51 
52  @param [in] libds The library.dataset to export the metadata for
53  @param [out] outds= (work.dsmeta) The output table to contain the metadata
54 
55  <h4> Related Files </h4>
56  @li mp_dsmeta.test.sas
57  @li mp_getcols.sas
58  @li mp_getdbml.sas
59  @li mp_getddl.sas
60  @li mp_getformats.sas
61  @li mp_getpk.sas
62  @li mp_guesspk.sas
63 
64 **/
65 
66 %macro mp_dsmeta(libds,outds=work.dsmeta);
67 
68 %local ds1 ds2;
69 data;run; %let ds1=&syslast;
70 data;run; %let ds2=&syslast;
71 
72 /* setup the ODS capture */
73 ods output attributes=&ds1 enginehost=&ds2;
74 
75 /* export the metadata */
76 proc contents data=&libds;
77 run;
78 
79 /* load it into a single table */
80 data &outds (keep=ods_table name value);
81  length ods_table $10 name label2 label1 label $100
82  value cvalue cvalue1 cvalue2 $1000
83  nvalue nvalue1 nvalue2 8;
84  if _n_=1 then call missing (of _all_);
85  * putlog (_all_)(=);
86  set &ds1 (in=atrs) &ds2 (in=eng);
87  if atrs then do;
88  ods_table='ATTRIBUTES';
89  name=coalescec(label1,label);
90  value=coalescec(cvalue1,cvalue,put(coalesce(nvalue1,nvalue),best.));
91  output;
92  if label2 ne '' then do;
93  name=label2;
94  value=coalescec(cvalue2,put(nvalue2,best.));
95  output;
96  end;
97  end;
98  else if eng then do;
99  ods_table='ENGINEHOST';
100  name=coalescec(label1,label);
101  value=coalescec(cvalue1,cvalue,put(coalesce(nvalue1,nvalue),best.));
102  output;
103  end;
104 run;
105 
106 proc sql;
107 drop table &ds1, &ds2;
108 
109 %mend mp_dsmeta;
110