I'm going crazy. I cannot figure out how to resolve this issue.
I want to save a file under the condition that the latest modified date is greater than the last saved date.
It works but it does not update correctly the macro vars _mod and _last.
Like a headless chicken I'm trying to solve this by brute force doing things like calling twice the macro, usage of symget, call symputx, select :into, call symdel...
Without success. At run time it doesn't get the latest updated values of _mod (last modified) and _last (last saved).
cas mySession sessopts=(caslib=casuser timeout=1800 locale="en_US");
caslib _all_ assign;
/* --- Begin Edit --- */
%let BASE_URI=%sysfunc(getoption(servicesbaseurl));
%put &base_uri.;
%let baseurl=https:/xxxxxxxxx;
%let ppath=/Users/xxxxxxxx@comp.com/My Folder/in_pdf/;
filename myfldr filesrvc folderPath="&ppath.";
data filenames;
length fname $200;
did = dopen('myfldr');
do i = 1 to dnum(did);
fname = dread(did,i);
if index(lowcase(fname), 'json') then output;
end;
did = dclose(did);
keep fname;
run;
filename folders temp;
proc http
url = "https://xxxxxxxxx"
out= folders
oauth_bearer = sas_services;
headers
'Accept'= 'application/vnd.sas.collection+json';
run;
libname folders clear;
libname folders json;
%let hostname = xxxxxxxxx;
%let endpoint = /folders/folders;
%let path = "/Public/ODAP_REPORTS" ;
%let output = "/out_json_backup";
%let currentDateTime = %sysfunc(datetime(), datetime20.);
%put ¤tDateTime;
/**********************************************************/
/* Retrieve the ID of the folder where the report resides */
/**********************************************************/
filename folders clear;
filename folders temp;
proc http
url = "&hostname.&endpoint/@item"
query = ("path"=&path)
out= folders
oauth_bearer = sas_services;
headers
'Accept'= 'application/vnd.sas.content.folder+json';
run;
libname folders clear;
libname folders json;
/*************************************************/
/* Get a list of objects in that specific folder */
/*************************************************/
/* Identify the endpoint to be used */
proc sql ;
select href, type into :endpoint, :type
from folders.links
where method="GET" and rel = "members";
quit;
/* Retrieve the list of objects */
filename memList clear;
filename memList temp;
proc http
url = "&hostname.%trim(&endpoint)"
out= memList
oauth_bearer = sas_services;
headers
"Accept"= "%trim(&type)+json";
run;
libname memList clear;
libname memList json;
/******************************************************/
/* Extract report content (structure) for each report */
/******************************************************/
/* Macro to write the output to a json file */
%sysmacdelete writeJson;
%macro writeJson(name, path);
filename outcsv FILESRVC FOLDERPATH="&ppath." FILENAME="_&name._¤tDateTime..json";
proc json out=outcsv nosastags pretty noscan ;
export casuser.info;
run;
%mend;
%macro get_bu_date(name);
proc sql;
select substr(fname, 1, prxmatch("m/(?<=_)(\d{2}.*)(?=\.json)/oi",fname)-2) as rep_name, floor(max(input(substr(fname, prxmatch("m/(?<=_)(\d{2}.*)(?=\.json)/oi",fname), 18), anydtdtm.))) format=best12. into :nom, :_last
from WORK.FILENAMES
where prxmatch("m/_&name/oi",fname)
group by 1;
quit;
data _null_;
set WORK.FILENAMES;
where prxmatch("m/_&name/oi",fname);
retain valor;
valor=max(valor, floor(max(input(substr(fname, prxmatch("m/(?<=_)(\d{2}.*)(?=\.json)/oi",fname), 18), anydtdtm.))));
call symputx('_last', valor);
run;
%put &_last.;
%mend;
/* Macro to read the report content and generate the output file */
%sysmacdelete readReportContent;
%macro readReportContent (url, name, path, endpoint, output);
filename f FILESRVC FOLDERPATH="&path." FILENAME="text.txt" lrecl=2000000;
/* filename f temp lrecl=2000000; */
proc http
url="&url.%trim(&endpoint)/content"
out=f
oauth_bearer=sas_services;
headers
"Accept"="application/vnd.sas.report.content+json";
quit;
%let n=60;
data casuser.info;
length report $128 path $1024 c1-c&n. varchar(32767) content varchar(2000000);
infile f lrecl=2000000 recfm=v truncover;
input (c1-c&n.) ($32767.);
content=cats(of c1-c&n.);
array c[*] c1-c&n. content;
report="&name";
path="&path";
drop c2-c&n.;
run;
proc sql;
select compress(substr(content, 1, 1000), '"') into :inf
from casuser.info;
run;
data _null_;
pid=prxparse('/(?<=datemodified:)(.{19})/io');
format want $26. fecha datetime19.;
string="&inf.";
put string=;
s=1;e=length(string);
call prxnext(pid,s,e,string,p,l);
do while(p>0);
want=catx(' ',want,substr(string,p+1,l));
want=tranwrd(want,'T', ' ');
fecha=input(want, anydtdtm.);
call prxnext(pid,s,e,string,p,l);
put string= want= p= l= fecha=;
call symputx('_mod', fecha);
end;
temp=symget('_last');
call symputx('lasted', temp);
run;
%put &_mod. &_last. &lasted. ;
%if %sysevalf(&_mod. > &_last.,boolean) %then %do;
%put "executed";
data _null_;
set casuser.info;
where path ne '';
location=tranwrd(path, "/", "_");
mcall=cat('%writeJson(', trim(report), ',', trim(location), ')');
call execute(mcall);
run;
%end;
%mend;
/* Generate a view containing information to call readReportContent */
proc sql;
create table merged as
select "&hostname" as url,
a.ordinal_items,
a. name,
a.uri,
b.href,
b.type ,
b.method
from memlist.items as a
left join memlist.items_links as b
on a.ordinal_items=b.ordinal_items
having a.contentType="report" and b.rel="getResource";
quit;
options mlogic;
/* Call readReportContent macro for each report */
data _null_;
length mcall $1024;
set merged(obs=3 firstobs=3);
out=&output;
path=&path;
/* call symdel("_last"); */
/* call symdel("lasted"); */
/* call symdel("_mod"); */
mstart=cat('%get_bu_date(', trim(name), ')');
call execute (%nrstr(mstart));
/* mcall=cat('%readReportContent(', trim(url), ",", trim(name), ",", trim(path),",", trim(href), ",", trim(out), ')'); */
/* call execute(mcall); */
run;
data _null_;
length mcall $1024;
set merged(obs=3 firstobs=3);
out=&output;
path=&path;
temp= symget("_last");
call symputx('_last', temp);
/* call symdel("lasted"); */
/* call symdel("_mod"); */
mstart=cat('%get_bu_date(', trim(name), ')');
call execute (%nrstr(mstart));
mcall=cat('%readReportContent(', trim(url), ",", trim(name), ",", trim(path),",", trim(href), ",", trim(out), ')');
call execute(mcall);
run;
... View more