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