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**/
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 where upcase(tablename)="%upcase(%scan(&libds,2,.))";
58 &dbg putlog tableuri=;
59 call symputx('tableuri',tableuri);
60 run;
61%end;
62
63data;run;
64%let tempds3=&syslast;
65%mm_getcols(tableuri=&tableuri,outds=&tempds3)
66
67%if %mf_nobs(&tempds3)=0 %then %do;
68 %put &libds (&tableuri) has no columns defined!!;
69 data &outds;
70 run;
71 %return;
72%end;
73
74data _null_;
75 set &tempds3 end=last;
76 if _n_=1 then call execute('data &outds;');
77 length attrib $32767;
78
79 if SAScolumntype='C' then type='$';
80 attrib='attrib '!!cats(colname)!!' length='!!cats(type,SASColumnLength,'.');
81
82 if not missing(sasformat) then fmt=' format='!!cats(sasformat);
83 if not missing(sasinformat) then infmt=' informat='!!cats(sasinformat);
84 if not missing(coldesc) then desc=' label='!!quote(cats(coldesc));
85
86 attrib=trim(attrib)!!fmt!!infmt!!desc!!';';
87
88 call execute(attrib);
89 if last then call execute('call missing(of _all_);stop;run;');
90run;
91
92%mend mm_createdataset;