Production Ready Macros for SAS Application Developers
mm_createdocument.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Create a Document object in a metadata folder
4  @details Document objects are useful for storing properties in metadata.
5  This macro is idempotent - it will not create an object with the same name
6  in the same location, twice.
7  Note - the filerefs are left open, to enable inspection after running the
8  macro (or importing into an xmlmap if needed).
9 
10  usage:
11 
12  %mm_createdocument(tree=/User Folders/sasdemo
13  ,name=MyNote)
14 
15  <h4> Dependencies </h4>
16  @li mf_abort.sas
17  @li mf_verifymacvars.sas
18 
19 
20  @param tree= The metadata folder uri, or the metadata path, in which to
21  create the document. This must exist.
22  @param name= Document object name. Avoid spaces.
23 
24  @param desc= Document description (optional)
25  @param textrole= TextRole property (optional)
26  @param frefin= fileref to use (enables change if there is a conflict)
27  @param frefout= fileref to use (enables change if there is a conflict)
28  @param mDebug= set to 1 to show debug messages in the log
29 
30  @author Allan Bowe
31 
32 **/
33 
34 %macro mm_createdocument(
35  tree=/User Folders/sasdemo
36  ,name=myNote
37  ,desc=Created by &sysmacroname
38  ,textrole=
39  ,frefin=mm_in
40  ,frefout=mm_out
41  ,mDebug=1
42  );
43 
44 %local mD;
45 %if &mDebug=1 %then %let mD=;
46 %else %let mD=%str(*);
47 %&mD.put Executing &sysmacroname..sas;
48 %&mD.put _local_;
49 
50 %mf_verifymacvars(tree name)
51 
52 /**
53  * check tree exists
54  */
55 
56 data _null_;
57  length type uri $256;
58  rc=metadata_pathobj("","&tree","Folder",type,uri);
59  call symputx('type',type,'l');
60  call symputx('treeuri',uri,'l');
61 run;
62 
63 %mf_abort(
64  iftrue= (&type ne Tree)
65  ,mac=mm_createdocument.sas
66  ,msg=Tree &tree does not exist!
67 )
68 
69 /**
70  * Check object does not exist already
71  */
72 data _null_;
73  length type uri $256;
74  rc=metadata_pathobj("","&tree/&name","Note",type,uri);
75  call symputx('type',type,'l');
76  call symputx('docuri',uri,'l');
77  putlog (_all_)(=);
78 run;
79 
80 %if &type = Document %then %do;
81  %put Document &name already exists in &tree!;
82  %return;
83 %end;
84 
85 /**
86  * Now we can create the document
87  */
88 filename &frefin temp;
89 
90 /* write header XML */
91 data _null_;
92  file &frefin;
93  name=quote("&name");
94  desc=quote("&desc");
95  textrole=quote("&textrole");
96  treeuri=quote("&treeuri");
97 
98  put "<AddMetadata><Reposid>$METAREPOSITORY</Reposid>"/
99  '<Metadata><Document IsHidden="0" PublicType="Note" UsageVersion="1000000"'/
100  " Name=" name " desc=" desc " TextRole=" textrole ">"/
101  "<Notes> "/
102  ' <TextStore IsHidden="0" Name=' name ' UsageVersion="0" '/
103  ' TextRole="SourceCode" StoredText="hello world" />' /
104  '</Notes>'/
105  /*URI="Document for public note" */
106  "<Trees><Tree ObjRef=" treeuri "/></Trees>"/
107  "</Document></Metadata><NS>SAS</NS>"/
108  "<Flags>268435456</Flags></AddMetadata>";
109 run;
110 
111 filename &frefout temp;
112 
113 proc metadata in= &frefin out=&frefout verbose;
114 run;
115 
116 %if &mdebug=1 %then %do;
117  /* write the response to the log for debugging */
118  data _null_;
119  infile &frefout lrecl=1048576;
120  input;
121  put _infile_;
122  run;
123 %end;
124 
125 %mend;