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  @param [in] pgm The _program value from which to extract the appLoc
35 
36  @author Allan Bowe
37 **/
38 
39 %macro mf_getapploc(pgm);
40 %if "&pgm"="" %then %do;
41  %if %symexist(_program) %then %let pgm=&_program;
42  %else %do;
43  %put &sysmacroname: No value provided and no _program variable available;
44  %return;
45  %end;
46 %end;
47 %local root;
48 
49 /**
50  * First check we are not in the tests/macros folder (which has no subfolders)
51  * or specifically in the testsetup or testteardown services
52  */
53 %if %index(&pgm,/tests/macros/)
54 or %index(&pgm,/tests/testsetup)
55 or %index(&pgm,/tests/testteardown)
56 %then %do;
57  %let root=%substr(&pgm,1,%index(&pgm,/tests)-1);
58  &root
59  %return;
60 %end;
61 
62 /**
63  * Next, move up two levels to avoid matches on subfolder or service name
64  */
65 %let root=%substr(&pgm,1,%length(&pgm)-%length(%scan(&pgm,-1,/))-1);
66 %let root=%substr(&root,1,%length(&root)-%length(%scan(&root,-1,/))-1);
67 
68 %if %index(&root,/tests/) %then %do;
69  %let root=%substr(&root,1,%index(&root,/tests/)-1);
70 %end;
71 %else %if %index(&root,/services) %then %do;
72  %let root=%substr(&root,1,%index(&root,/services)-1);
73 %end;
74 %else %if %index(&root,/jobs) %then %do;
75  %let root=%substr(&root,1,%index(&root,/jobs)-1);
76 %end;
77 %else %put &sysmacroname: Could not find an app location from &pgm;
78  &root
79 %mend mf_getapploc ;