Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mm_gettableid.sas
Go to the documentation of this file.
1 /**
2  @file mm_gettableid.sas
3  @brief Get the metadata id for a particular table
4  @details Provide a libref and table name to return the corresponding metadata id
5  in an output datataset.
6 
7  Usage:
8 
9  - get a table id
10  %mm_gettableid(libref=METALIB,ds=SOMETABLE,outds=iwant)
11 
12  @param libref= The libref to search
13  @param ds= The input dataset to check
14  @param outds= the dataset to create that contains the `tableuri`
15  @param mDebug= set to 1 to show debug messages in the log
16 
17  @returns outds dataset containing `tableuri` and `tablename`
18 
19  @version 9.3
20  @author Allan Bowe
21 
22 **/
23 
24 %macro mm_gettableid(
25  libref=
26  ,ds=
27  ,outds=work.mm_gettableid
28  ,mDebug=0
29 )/*/STORE SOURCE*/;
30 
31 %local mD;
32 %if &mDebug=1 %then %let mD=;
33 %else %let mD=%str(*);
34 %&mD.put Executing &sysmacroname..sas;
35 %&mD.put _local_;
36 
37 data &outds;
38  length uri usingpkguri id type tableuri tablename tmpuri $256;
39  call missing(of _all_);
40  keep tableuri tablename;
41  n=1;
42  rc=0;
43  if metadata_getnobj("omsobj:SASLibrary?@Libref='&libref'",n,uri)<1 then do;
44  put "Library &libref not found";
45  stop;
46  end;
47  &mD.putlog "uri is " uri;
48  if metadata_getnasn(uri, "UsingPackages", 1, usingpkguri)>0 then do;
49  rc=metadata_resolve(usingpkguri,type,id);
50  &mD.putlog "Type is " type;
51  end;
52 
53  if type='DatabaseSchema' then tmpuri=usingpkguri;
54  else tmpuri=uri;
55 
56  t=1;
57  do while(metadata_getnasn(tmpuri, "Tables", t, tableuri)>0);
58  t+1;
59  rc= metadata_getattr(tableuri, "Name", tablename);
60  &mD.putlog "Table is " tablename;
61  if upcase(tablename)="%upcase(&ds)" then do;
62  output;
63  end;
64  end;
65 run;
66 
67 %mend;