Macros for SAS Application Developers
https://github.com/sasjs/core
ms_getgroups.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Fetches the list of groups from SASjs Server
4  @details Fetches the list of groups from SASjs Server and writes them to an
5  output dataset. Provide a username to filter for the groups for a particular
6  user.
7 
8  Example:
9 
10  %ms_getgroups(outds=userlist)
11 
12  With filter on username:
13 
14  %ms_getgroups(outds=userlist, user=James)
15 
16  With filter on userid:
17 
18  %ms_getgroups(outds=userlist, uid=1)
19 
20  @param [in] mdebug= (0) Set to 1 to enable DEBUG messages
21  @param [in] user= (0) Provide the username on which to filter
22  @param [in] uid= (0) Provide the userid on which to filter
23  @param [out] outds= (work.ms_getgroups) This output dataset will contain the
24  list of groups. Format:
25 |NAME:$32.|DESCRIPTION:$256.|GROUPID:best.|
26 |---|---|---|
27 |`SomeGroup `|`A group `|`1`|
28 |`Another Group`|`this is a different group`|`2`|
29 |`admin`|`Administrators `|`3`|
30 
31 
32  <h4> SAS Macros </h4>
33  @li mf_getuniquefileref.sas
34  @li mf_getuniquelibref.sas
35  @li mp_abort.sas
36 
37  <h4> Related Files </h4>
38  @li ms_creategroup.sas
39  @li ms_getgroups.test.sas
40 
41 **/
42 
43 %macro ms_getgroups(
44  user=0,
45  uid=0,
46  outds=work.ms_getgroups,
47  mdebug=0
48 );
49 
50 %mp_abort(
51  iftrue=(&syscc ne 0)
52  ,mac=ms_getgroups.sas
53  ,msg=%str(syscc=&syscc on macro entry)
54 )
55 
56 %local fref0 fref1 libref optval rc msg url;
57 
58 %if %sysget(MODE)=desktop %then %do;
59  /* groups api does not exist in desktop mode */
60  data &outds;
61  length NAME $32 DESCRIPTION $256. GROUPID 8;
62  name="&sysuserid";
63  description="&sysuserid (group - desktop mode)";
64  groupid=1;
65  output;
66  stop;
67  run;
68  %return;
69 %end;
70 
71 %let fref0=%mf_getuniquefileref();
72 %let fref1=%mf_getuniquefileref();
73 %let libref=%mf_getuniquelibref();
74 
75 /* avoid sending bom marker to API */
76 %let optval=%sysfunc(getoption(bomfile));
77 options nobomfile;
78 
79 data _null_;
80  file &fref0 lrecl=1000;
81  infile "&_sasjs_tokenfile" lrecl=1000;
82  input;
83  if _n_=1 then put "accept: application/json";
84  put _infile_;
85 run;
86 
87 %if &mdebug=1 %then %do;
88  data _null_;
89  infile &fref0;
90  input;
91  put _infile_;
92  run;
93 %end;
94 
95 %if "&user" ne "0" %then %let url=/SASjsApi/user/by/username/&user;
96 %else %if "&uid" ne "0" %then %let url=/SASjsApi/user/&uid;
97 %else %let url=/SASjsApi/group;
98 
99 
100 proc http method='GET' headerin=&fref0 out=&fref1
101  url="&_sasjs_apiserverurl.&url";
102 %if &mdebug=1 %then %do;
103  debug level=1;
104 %end;
105 run;
106 
107 %mp_abort(
108  iftrue=(&syscc ne 0)
109  ,mac=ms_getgroups.sas
110  ,msg=%str(Issue submitting GET query to SASjsApi)
111 )
112 
113 libname &libref JSON fileref=&fref1;
114 
115 %if "&user"="0" and "&uid"="0" %then %do;
116  data &outds;
117  length NAME $32 DESCRIPTION $256. GROUPID 8;
118  if _n_=1 then call missing(of _all_);
119  set &libref..root;
120  drop ordinal_root;
121  run;
122 %end;
123 %else %do;
124  data &outds;
125  length NAME $32 DESCRIPTION $256. GROUPID 8;
126  if _n_=1 then call missing(of _all_);
127  set &libref..groups;
128  drop ordinal_:;
129  run;
130 %end;
131 
132 %mp_abort(
133  iftrue=(&syscc ne 0)
134  ,mac=ms_getgroups.sas
135  ,msg=%str(Issue reading response JSON)
136 )
137 
138 /* reset options */
139 options &optval;
140 
141 %if &mdebug=1 %then %do;
142  filename &fref0 clear;
143  filename &fref1 clear;
144  libname &libref clear;
145 %end;
146 
147 %mend ms_getgroups;