Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
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 @li mf_nobs.sas
25
26 @param [in] 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 [in] tableuri= Metadata URI of the table to be created
29 @param [out] outds= (work.mm_createdataset) The dataset to create. The table
30 name needs to be 32 chars or less as per SAS naming rules.
31 @param [in] mdebug= (0) Set to 1 to enable DEBUG messages
32
33 @version 9.4
34 @author Allan Bowe
35
36 @sasjslint {"maxLineLength": 85}
37**/
38
39%macro mm_createdataset(libds=,tableuri=,outds=work.mm_createdataset,mDebug=0);
40%local dbg errorcheck tempds1 tempds2 tempds3;
41%if &mDebug=0 %then %let dbg=*;
42%let errorcheck=1;
43
44%if %index(&libds,.)>0 %then %do;
45 /* get lib uri */
46 data;run;%let tempds1=&syslast;
47 %mm_getlibs(outds=&tempds1)
48 data _null_;
49 set &tempds1;
50 if upcase(libraryref)="%upcase(%scan(&libds,1,.))";
51 call symputx('liburi',LibraryId,'l');
52 run;
53 /* get ds uri */
54 data;run;%let tempds2=&syslast;
55 %mm_gettables(uri=&liburi,outds=&tempds2)
56 data _null_;
57 set &tempds2;
58 where upcase(tablename)="%upcase(%scan(&libds,2,.))";
59 &dbg putlog tableuri=;
60 call symputx('tableuri',tableuri);
61 run;
62%end;
63
64data;run;
65%let tempds3=&syslast;
66%mm_getcols(tableuri=&tableuri,outds=&tempds3)
67
68%if %mf_nobs(&tempds3)=0 %then %do;
69 %put &libds (&tableuri) has no columns defined!!;
70 data &outds;
71 run;
72 %return;
73%end;
74
75data _null_;
76 set &tempds3 end=last;
77 if _n_=1 then call execute('data &outds;');
78 length attrib $32767;
79
80 if SAScolumntype='C' then type='$';
81 attrib='attrib '!!cats(colname)!!' length='!!cats(type,SASColumnLength,'.');
82
83 if not missing(sasformat) then fmt=' format='!!cats(sasformat);
84 if not missing(sasinformat) then infmt=' informat='!!cats(sasinformat);
85 if not missing(coldesc) then desc=' label='!!quote(cats(coldesc));
86
87 attrib=trim(attrib)!!fmt!!infmt!!desc!!';';
88
89 call execute(attrib);
90 if last then call execute('call missing(of _all_);stop;run;');
91run;
92
93%mend mm_createdataset;