Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mm_createdataset.sas
Go to the documentation of this file.
1 /**
2  @file mm_createdataset.sas
3  @brief Create a dataset from a metadata definition
4  @details This macro was built to support viewing empty tables in
5  https://datacontroller.io - a free evaluation copy is available by
6  contacting the author (Allan Bowe).
7 
8  The table can be retrieved using LIBRARY.DATASET reference, or directly
9  using the metadata URI.
10 
11  The dataset is written to the WORK library.
12 
13  usage:
14 
15  %mm_createdataset(libds=metlib.some_dataset)
16 
17  or
18 
19  %mm_createdataset(tableuri=G5X8AFW1.BE00015Y)
20 
21  <h4> Dependencies </h4>
22  @li mm_getlibs.sas
23  @li mm_gettables.sas
24  @li mm_getcols.sas
25 
26  @param libds= library.dataset metadata source. Note - table names in metadata
27  can be longer than 32 chars (just fyi, not an issue here)
28  @param tableuri= Metadata URI of the table to be created
29  @param outds= The dataset to create, default is `work.mm_createdataset`.
30  The table name needs to be 32 chars or less as per SAS naming rules.
31  @param mdebug= set DBG to 1 to disable DEBUG messages
32 
33  @version 9.4
34  @author Allan Bowe
35 
36 **/
37 
38 %macro mm_createdataset(libds=,tableuri=,outds=work.mm_createdataset,mDebug=0);
39 %local dbg errorcheck tempds1 tempds2 tempds3;
40 %if &mDebug=0 %then %let dbg=*;
41 %let errorcheck=1;
42 
43 %if %index(&libds,.)>0 %then %do;
44  /* get lib uri */
45  data;run;%let tempds1=&syslast;
46  %mm_getlibs(outds=&tempds1)
47  data _null_;
48  set &tempds1;
49  if upcase(libraryref)="%upcase(%scan(&libds,1,.))";
50  call symputx('liburi',LibraryId,'l');
51  run;
52  /* get ds uri */
53  data;run;%let tempds2=&syslast;
54  %mm_gettables(uri=&liburi,outds=&tempds2)
55  data _null_;
56  set &tempds2;
57  if upcase(tablename)="%upcase(%scan(&libds,2,.))";
58  call symputx('tableuri',tableuri);
59  run;
60 %end;
61 
62 data;run;%let tempds3=&syslast;
63 %mm_getcols(tableuri=&tableuri,outds=&tempds3)
64 
65 data _null_;
66  set &tempds3 end=last;
67  if _n_=1 then call execute('data &outds;');
68  length attrib $32767;
69 
70  if SAScolumntype='C' then type='$';
71  attrib='attrib '!!cats(colname)!!' length='!!cats(type,SASColumnLength,'.');
72 
73  if not missing(sasformat) then fmt=' format='!!cats(sasformat);
74  if not missing(sasinformat) then infmt=' informat='!!cats(sasinformat);
75  if not missing(coldesc) then desc=' label='!!quote(cats(coldesc));
76 
77  attrib=trim(attrib)!!fmt!!infmt!!desc!!';';
78 
79  call execute(attrib);
80  if last then call execute('call missing(of _all_);stop;run;');
81 run;
82 
83 %mend;