Attention : This code requires administrator privileges.
The script begins by calling the 'reports' service via PROC HTTP to obtain a filtered list of reports (here, by name). The JSON response is read with a LIBNAME JSON. Then, for each report found, a macro (%save_VA_Report_Path) is executed. This macro uses another API call ('folders/ancestors') to find the parent folder path of the report. The path is reconstructed and stored in the final dataset. Finally, the script displays the list of reports with their path, ID, and creation/modification metadata.
Data Analysis
Type : EXTERNAL
Data is dynamically generated by calls to the SAS Viya REST API. The first call retrieves report metadata, and subsequent calls retrieve the folder structure for each report.
1 Code Block
PROC HTTP Data
Explanation : This block executes a GET request to the SAS Viya reports API to retrieve a collection of reports filtered by their name ('Report 2'). The JSON response is stored in the temporary fileref 'rptFile'.
Copied!
%let BASE_URI=%sysfunc(getoption(SERVICESBASEURL));
%let fullpath=/SAS Content/;
FILENAME rptFile TEMP ENCODING='UTF-8';
PROC HTTP METHOD = "GET" oauth_bearer=sas_services OUT = rptFile
/* get a list of reports, say created by sbjciw, or report name is 'Report 2' */
/* URL = "&BASE_URI/reports/reports?filter=eq(createdBy,'sbjciw')" */
URL = "&BASE_URI/reports/reports?filter=eq(name,'Report 2')";
HEADERS "Accept" = "application/vnd.sas.collection+json"
"Accept-Item" = "application/vnd.sas.summary+json";
RUN;
Explanation : Assigns a 'rptFile' library to read the received JSON. Then, a DATA step reads the items from the report collection to create the 'ds_rpts' table, keeping and renaming relevant variables like ID, name, and timestamps.
Copied!
LIBNAME rptFile json;
data ds_rpts (keep=rptID id name createdBy creationTimeStamp modifiedTimeStamp
rename=(modifiedTimeStamp=LastModified creationTimeStamp=CreatedAt));
length rptID $ 60 rptPath $ 100;
set rptFile.items;
rptID = '/reports/reports/'||id;
run;
1
LIBNAME rptFile json;
2
3
DATA ds_rpts (keep=rptID id name createdBy creationTimeStamp modifiedTimeStamp
Explanation : Defines a macro that takes a report URI as input. The macro performs a PROC HTTP call to find the ancestor folders of the report, uses PROC SQL to reconstruct the full path, then updates the 'ds_rpts' dataset with this path for the corresponding report. Note: Updating the 'ds_rpts' dataset is inefficient as it recreates the table with each call.
Copied!
%MACRO save_VA_Report_Path(reportURI);
FILENAME fldFile TEMP ENCODING='UTF-8';
%let locURI = &reportURI;
PROC HTTP METHOD = "GET" oauth_bearer=sas_services OUT = fldFile
/* get the folders in which the reportURI is in */
URL = "&BASE_URI/folders/ancestors?childUri=/reports/reports/&reportURI";
HEADERS "Accept" = "application/vnd.sas.content.folder.ancestor+json";
RUN;
LIBNAME fldFile json;
/* generate the path from the returned folders above */
proc sql noprint;
select name into :fldname separated by '/'
from fldFile.ancestors
order by ordinal_ancestors desc;
quit;
data tmpsave;
length cc $ 36;
set ds_rpts;
cc = "&locURI";
if trim(id) = trim(cc) then
rptPath=resolve('&fullpath.&fldname.');
drop cc;
run;
data ds_rpts;
set tmpsave;
run;
%MEND save_VA_Report_Path;
1
%MACRO save_VA_Report_Path(reportURI);
2
FILENAME fldFile TEMP ENCODING='UTF-8';
3
%let locURI = &reportURI;
4
5
PROC HTTP METHOD = "GET" oauth_bearer=sas_services OUT = fldFile
6
/* get the folders in which the reportURI is in */
/* generate the path from the returned folders above */
13
PROC SQL noprint;
14
select name into :fldname separated BY'/'
15
from fldFile.ancestors
16
order BY ordinal_ancestors desc;
17
QUIT;
18
19
DATA tmpsave;
20
LENGTH cc $ 36;
21
SET ds_rpts;
22
cc = "&locURI";
23
IF trim(id) = trim(cc) THEN
24
rptPath=resolve('&fullpath.&fldname.');
25
drop cc;
26
RUN;
27
28
DATA ds_rpts;
29
SET tmpsave;
30
RUN;
31
32
%MEND save_VA_Report_Path;
4 Code Block
DATA STEP Data
Explanation : This DATA _NULL_ step iterates through the 'ds_rpts' table and uses 'CALL EXECUTE' to dynamically call the '%save_VA_Report_Path' macro for each report (each 'id') found, in order to populate the report's path.
Copied!
DATA _null_;
set ds_rpts;
call execute('%save_VA_Report_Path('||id||')');
RUN;
1
DATA _null_;
2
SET ds_rpts;
3
call execute('%save_VA_Report_Path('||id||')');
4
RUN;
5 Code Block
PROC PRINT
Explanation : Prints the final content of the 'ds_rpts' dataset to display the path, name, ID, creator, and timestamps for each report.
Copied!
PROC PRINT data=ds_rpts;
var rptPath name rptID createdBy CreatedAt LastModified;
RUN;
1
2
PROC PRINT
3
DATA=ds_rpts;
4
var rptPath name rptID createdBy CreatedAt LastModified;
5
RUN;
6
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.