Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mm_updatedocument.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Update the TextStore in a Document with the same name
4  @details Enables arbitrary content to be stored in a document object
5 
6  Usage:
7 
8  %mm_updatedocument(path=/my/metadata/path
9  ,name=docname
10  ,text="/file/system/some.txt")
11 
12 
13  @param path= the BIP Tree folder path
14  @param name=Document Name
15  @param text=a source file containing the text to be added
16 
17  @param frefin= change default inref if it clashes with an existing one
18  @param frefout= change default outref if it clashes with an existing one
19  @param mDebug= set to 1 to show debug messages in the log
20 
21  @version 9.3
22  @author Allan Bowe
23 
24 **/
25 
26 %macro mm_updatedocument(path=
27  ,name=
28  ,text=
29  ,frefin=inmeta
30  ,frefout=outmeta
31  ,mdebug=0
32 );
33 /* first, check if STP exists */
34 %local tsuri;
35 %let tsuri=stopifempty ;
36 
37 data _null_;
38  format type uri tsuri value $200.;
39  call missing (of _all_);
40  path="&path/&name(Note)";
41  /* first, find the STP ID */
42  if metadata_pathobj("",path,"Note",type,uri)>0 then do;
43  /* get sourcetext */
44  cnt=1;
45  do while (metadata_getnasn(uri,"Notes",cnt,tsuri)>0);
46  rc=metadata_getattr(tsuri,"Name",value);
47  put tsuri= value=;
48  if value="&name" then do;
49  /* found it! */
50  rc=metadata_getattr(tsuri,"Id",value);
51  call symputx('tsuri',value,'l');
52  stop;
53  end;
54  cnt+1;
55  end;
56  end;
57  else put (_all_)(=);
58 run;
59 
60 %if &tsuri=stopifempty %then %do;
61  %put WARNING: &path/&name.(Document) not found!;
62  %return;
63 %end;
64 
65 %if %length(&text)<2 %then %do;
66  %put WARNING: No text supplied!!;
67  %return;
68 %end;
69 
70 filename &frefin temp recfm=n;
71 
72 /* escape code so it can be stored as XML */
73 /* input file may be over 32k wide, so deal with one char at a time */
74 data _null_;
75  file &frefin recfm=n;
76  infile &text recfm=n;
77  input instr $CHAR1. ;
78  if _n_=1 then put "<UpdateMetadata><Reposid>$METAREPOSITORY</Reposid>
79  <Metadata><TextStore id='&tsuri' StoredText='" @@;
80  select (instr);
81  when ('&') put '&amp;';
82  when ('<') put '&lt;';
83  when ('>') put '&gt;';
84  when ("'") put '&apos;';
85  when ('"') put '&quot;';
86  when ('0A'x) put '&#x0a;';
87  when ('0D'x) put '&#x0d;';
88  when ('$') put '&#36;';
89  otherwise put instr $CHAR1.;
90  end;
91 run;
92 
93 data _null_;
94  file &frefin mod;
95  put "'></TextStore></Metadata><NS>SAS</NS><Flags>268435456</Flags>
96  </UpdateMetadata>";
97 run;
98 
99 
100 filename &frefout temp;
101 
102 proc metadata in= &frefin
103  %if &mdebug=1 %then out=&frefout verbose;
104 ;
105 run;
106 
107 %if &mdebug=1 %then %do;
108  /* write the response to the log for debugging */
109  data _null_;
110  infile &frefout lrecl=1048576;
111  input;
112  put _infile_;
113  run;
114 %end;
115 
116 %mend;