Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mv_castabload.sas
Go to the documentation of this file.
1/**
2 @file mv_castabload.sas
3 @brief Checks if a CAS table is loaded; if not, loads and promotes it
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, then checks whether the table is already
7 in-memory. If not, locates the owning CAS server via the
8 casManagement REST API, queries the table endpoint to discover
9 the source file and caslib, then loads and promotes the table.
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_castabload(lib=mylib, table=BASEBALL)
16
17 @param [in] lib= SAS libref for the CAS caslib
18 @param [in] table= Name of the CAS table to load
19 @param [in] mdebug= (0) Set to 1 to enable verbose logging:
20 - echoes resolved parameters
21 - prints tableExists result
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_castabload(
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 _exists;
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');
62 %if &mdebug=1 %then %do;
63 putlog sysname sysvalue;
64 %end;
65run;
66
67%mp_abort(
68 iftrue=("&caslib"=""),
69 msg=%str(&lib is not an assigned CAS libref)
70)
71
72%mp_abort(
73 iftrue=("&uuid"=""),
74 msg=%str(No session UUID found for libref &lib)
75)
76
77/* ---- existence check --------------------------------------------------- */
78proc cas;
79 table.tableExists result=r /
80 caslib="&caslib"
81 name="&table";
82 %if &mdebug=1 %then %do;
83 print r;
84 %end;
85 if r.exists > 0 then call symputx('_exists', '1', 'L');
86 else call symputx('_exists', '0', 'L');
87quit;
88
89/* ---- already loaded: skip ---------------------------------------------- */
90%if &_exists=1 %then %do;
91 %put NOTE: Table &caslib..&table already loaded - skipping;
92 %return;
93%end;
94
95/* ---- get list of CAS servers ----------------------------------------- */
96%let base_uri=%mf_getplatform(VIYARESTAPI);
97%let fname1=%mf_getuniquefileref();
98%let libref1=%mf_getuniquelibref();
99
100proc http method='GET' out=&fname1 oauth_bearer=sas_services
101 url="&base_uri/casManagement/servers";
102run;
103
104%mp_abort(
105 iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 200),
106 msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
107)
108
109libname &libref1 JSON fileref=&fname1;
110
111data _null_;
112 set &libref1..items;
113 call symputx(cats('_sv_', _n_), name, 'L');
114 call symputx('_svcount', _n_, 'L');
115run;
116
117libname &libref1 clear;
118filename &fname1 clear;
119
120/* ---- find which server owns this session ------------------------------ */
121%do i=1 %to &_svcount;
122 %if "&server"="" %then %do;
123 %if &mdebug=1 %then %put checking server: &&_sv_&i;
124 %let ftmp=%mf_getuniquefileref();
125 proc http method='GET' out=&ftmp oauth_bearer=sas_services
126 url="&base_uri/casManagement/servers/&&_sv_&i/sessions/&uuid";
127 run;
128 %if &SYS_PROCHTTP_STATUS_CODE=200
129 %then %let server=&&_sv_&i;
130 filename &ftmp clear;
131 %end;
132%end;
133
134%mp_abort(
135 iftrue=("&server"=""),
136 msg=%str(Could not find owning server for CAS session &uuid)
137)
138
139%if &mdebug=1 %then %put &=server;
140
141/* ---- discover source file from REST endpoint -------------------------- */
142%let fname1=%mf_getuniquefileref();
143%let libref1=%mf_getuniquelibref();
144
145proc http method='GET' out=&fname1 oauth_bearer=sas_services
146 url="&base_uri/casManagement/servers/&server/caslibs/&caslib/tables/&table";
147run;
148
149%if &mdebug=1 %then %do;
150 %put &=SYS_PROCHTTP_STATUS_CODE &=SYS_PROCHTTP_STATUS_PHRASE;
151 data _null_;
152 infile &fname1;
153 input;
154 putlog _infile_;
155 run;
156%end;
157
158%mp_abort(
159 iftrue=(&SYS_PROCHTTP_STATUS_CODE=404),
160 msg=%str(&caslib..&table not found - is a source file registered?)
161)
162%mp_abort(
163 iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 200),
164 msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
165)
166
167libname &libref1 JSON fileref=&fname1;
168
169data _null_;
170 set &libref1..tablereference;
171 call symputx('srcfile', sourceTableName, 'L');
172 call symputx('srccaslib', sourceCaslibName, 'L');
173 stop;
174run;
175
176libname &libref1 clear;
177filename &fname1 clear;
178
179%mp_abort(
180 iftrue=("&srcfile"="" or "&srccaslib"=""),
181 msg=%str(No sourceTableName/sourceCaslibName for &caslib..&table)
182)
183
184%if &mdebug=1 %then %put &=srcfile &=srccaslib;
185
186/* ---- load from discovered source -------------------------------------- */
187proc casutil;
188 load casdata="&srcfile"
189 incaslib="&srccaslib"
190 casout="&table"
191 outcaslib="&caslib"
192 promote;
193quit;
194
195%mp_abort(
196 iftrue=(&syscc ne 0),
197 msg=%str(Load failed for &caslib..&table)
198)
199
200%put NOTE: Table &caslib..&table loaded and promoted from &srcfile;
201
202/* ---- restore options --------------------------------------------------- */
203%if &mdebug=1 %then %do;
204 options &_sysopts;
205%end;
206
207%mend mv_castabload;