Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mv_castabsave.sas
Go to the documentation of this file.
1/**
2 @file mv_castabsave.sas
3 @brief Saves an in-memory CAS table back to persistent storage
4 @details Runs in SPRE against an active CAS session. Accepts a
5 SAS libref, derives the CAS caslib and session UUID from
6 sashelp.vlibnam, locates the owning CAS server via the
7 casManagement REST API, then queries the table endpoint to
8 discover the original source file and saves back to that path.
9 CASUTIL infers the file type from the output file extension.
10
11 A CAS session must already be established by the caller, eg:
12
13 cas mysess;
14 libname mylib cas caslib=Public;
15 %mv_castabsave(lib=mylib, table=BASEBALL)
16
17 @param [in] lib= SAS libref for the CAS caslib
18 @param [in] table= Name of the in-memory CAS table to save
19 @param [in] mdebug= (0) Set to 1 to enable verbose logging:
20 - echoes resolved parameters
21 - prints HTTP response body
22 - enables mprint/notes during PROC calls
23
24 <h4> SAS Macros </h4>
25 @li mf_getplatform.sas
26 @li mf_getuniquefileref.sas
27 @li mf_getuniquelibref.sas
28 @li mp_abort.sas
29
30**/
31
32%macro mv_castabsave(
33 lib=
34 ,table=
35 ,mdebug=0
36);
37
38%local _sysopts base_uri caslib uuid server
39 srcfile srccaslib fname1 libref1 ftmp i _svcount;
40%let _sysopts=%sysfunc(getoption(mprint)) %sysfunc(getoption(notes));
41
42/* ---- input validation -------------------------------------------------- */
43%mp_abort(
44 iftrue=("&lib"="" or "&table"=""),
45 msg=%str(lib= and table= are required)
46)
47
48%if &mdebug=1 %then %do;
49 %put &=lib;
50 %put &=table;
51 options mprint notes;
52%end;
53
54/* ---- derive caslib and session UUID from sashelp.vlibnam --------------- */
55data _null_;
56 set sashelp.vlibnam(
57 where=(libname="%upcase(&lib)"
58 and sysname in ("Caslib","Session UUID"))
59 );
60 if sysname="Caslib" then call symputx('caslib',sysvalue,'L');
61 else call symputx('uuid',sysvalue,'L');
62run;
63
64%mp_abort(
65 iftrue=("&caslib"=""),
66 msg=%str(&lib is not an assigned CAS libref)
67)
68
69%mp_abort(
70 iftrue=("&uuid"=""),
71 msg=%str(No session UUID found for libref &lib)
72)
73
74%if &mdebug=1 %then %do;
75 %put &=caslib;
76 %put &=uuid;
77%end;
78
79%let base_uri=%mf_getplatform(VIYARESTAPI);
80
81/* ---- get list of CAS servers ------------------------------------------- */
82%let fname1=%mf_getuniquefileref();
83%let libref1=%mf_getuniquelibref();
84
85proc http method='GET' out=&fname1 oauth_bearer=sas_services
86 url="&base_uri/casManagement/servers";
87run;
88
89%mp_abort(
90 iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 200),
91 msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
92)
93
94libname &libref1 JSON fileref=&fname1;
95
96data _null_;
97 set &libref1..items;
98 call symputx(cats('_sv_', _n_), name, 'L');
99 call symputx('_svcount', _n_, 'L');
100run;
101
102libname &libref1 clear;
103filename &fname1 clear;
104
105/* ---- find which server owns this session ------------------------------- */
106%do i=1 %to &_svcount;
107 %if "&server"="" %then %do;
108 %if &mdebug=1 %then %put checking server: &&_sv_&i;
109 %let ftmp=%mf_getuniquefileref();
110 proc http method='GET' out=&ftmp oauth_bearer=sas_services
111 url="&base_uri/casManagement/servers/&&_sv_&i/sessions/&uuid";
112 run;
113 %if &SYS_PROCHTTP_STATUS_CODE=200
114 %then %let server=&&_sv_&i;
115 filename &ftmp clear;
116 %end;
117%end;
118
119%mp_abort(
120 iftrue=("&server"=""),
121 msg=%str(Could not find owning server for CAS session &uuid)
122)
123
124%if &mdebug=1 %then %put &=server;
125
126/* ---- discover srcfile from REST endpoint ------------------------------- */
127%let fname1=%mf_getuniquefileref();
128%let libref1=%mf_getuniquelibref();
129
130proc http method='GET' out=&fname1 oauth_bearer=sas_services
131 url="&base_uri/casManagement/servers/&server/caslibs/&caslib/tables/&table";
132run;
133
134%if &mdebug=1 %then %do;
135 %put &=SYS_PROCHTTP_STATUS_CODE &=SYS_PROCHTTP_STATUS_PHRASE;
136 data _null_;
137 infile &fname1;
138 input;
139 putlog _infile_;
140 run;
141%end;
142
143%mp_abort(
144 iftrue=(&SYS_PROCHTTP_STATUS_CODE=404),
145 msg=%str(&caslib..&table not found - is it loaded in memory?)
146)
147%mp_abort(
148 iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 200),
149 msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
150)
151
152libname &libref1 JSON fileref=&fname1;
153
154data _null_;
155 set &libref1..tablereference;
156 call symputx('srcfile', sourceTableName, 'L');
157 call symputx('srccaslib', sourceCaslibName, 'L');
158 stop;
159run;
160
161libname &libref1 clear;
162filename &fname1 clear;
163
164%mp_abort(
165 iftrue=("&srcfile"="" or "&srccaslib"=""),
166 msg=%str(No sourceTableName/sourceCaslibName for &caslib..&table)
167)
168
169%if &mdebug=1 %then %put &=srcfile;
170
171/* ---- save to disk ------------------------------------------------------- */
172proc casutil;
173 save casdata="&table"
174 incaslib="&caslib"
175 casout="&srcfile"
176 outcaslib="&srccaslib"
177 replace;
178quit;
179
180%mp_abort(
181 iftrue=(&syscc ne 0),
182 msg=%str(Save failed for &caslib..&table)
183)
184
185%put NOTE: Table &caslib..&table saved to &srcfile;
186
187/* ---- restore options --------------------------------------------------- */
188%if &mdebug=1 %then %do;
189 options &_sysopts;
190%end;
191
192%mend mv_castabsave;