Macros for SAS Application Developers
https://github.com/sasjs/core
mf_getapploc.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Returns the appLoc from the _program variable
4 @details When working with SASjs apps, web services / tests / jobs are always
5 deployed to a root (app) location in the SAS logical folder tree.
6
7 When building apps for use in other environments, you do not necessarily know
8 where the backend services will be deployed. Therefore a function like this
9 is handy in order to dynamically figure out the appLoc, and enable other
10 services to be connected by a relative reference.
11
12 SASjs apps always have the same immediate substructure (one or more of the
13 following):
14
15 @li /data
16 @li /jobs
17 @li /services
18 @li /tests
19 @li /tests/jobs
20 @li /tests/services
21 @li /tests/macros
22
23 This function works by testing for the existence of any of the above in the
24 automatic _program variable, and returning the part to the left of it.
25
26 Usage:
27
28 %put %mf_getapploc(&_program)
29
30 %put %mf_getapploc(/some/location/services/admin/myservice);
31 %put %mf_getapploc(/some/location/jobs/extract/somejob/);
32 %put %mf_getapploc(/some/location/tests/jobs/somejob/);
33
34
35 @author Allan Bowe
36**/
37
38%macro mf_getapploc(pgm);
39%if "&pgm"="" %then %do;
40 %if %symexist(_program) %then %let pgm=&_program;
41 %else %do;
42 %put &sysmacroname: No value provided and no _program variable available;
43 %return;
44 %end;
45%end;
46%local root;
47
48/**
49 * First check we are not in the tests/macros folder (which has no subfolders)
50 * or specifically in the testsetup or testteardown services
51 */
52%if %index(&pgm,/tests/macros/)
53or %index(&pgm,/tests/testsetup)
54or %index(&pgm,/tests/testteardown)
55%then %do;
56 %let root=%substr(&pgm,1,%index(&pgm,/tests)-1);
57 &root
58 %return;
59%end;
60
61/**
62 * Next, move up two levels to avoid matches on subfolder or service name
63 */
64%let root=%substr(&pgm,1,%length(&pgm)-%length(%scan(&pgm,-1,/))-1);
65%let root=%substr(&root,1,%length(&root)-%length(%scan(&root,-1,/))-1);
66
67%if %index(&root,/tests/) %then %do;
68 %let root=%substr(&root,1,%index(&root,/tests/)-1);
69%end;
70%else %if %index(&root,/services) %then %do;
71 %let root=%substr(&root,1,%index(&root,/services)-1);
72%end;
73%else %if %index(&root,/jobs) %then %do;
74 %let root=%substr(&root,1,%index(&root,/jobs)-1);
75%end;
76%else %put &sysmacroname: Could not find an app location from &pgm;
77 &root
78%mend mf_getapploc ;