/******************************************************************************
 * Programme : Testing SAS Viya Job Execution and Management Macros
 * Reference : TESTIN3A46
 * Source    : https://www.wearecas.eu/de/sampleCode/TESTIN3A46
 ******************************************************************************/

/* --- BLOC 1 --- */
filename testref temp;
data _null_;
  file testref;
  put 'data;run;';
  put 'endsas;';
run;

/* --- BLOC 2 --- */
%mv_createjob(
  path=&mcTestAppLoc/jobs/temp,
  code=testref,
  name=testjob
)

/* --- BLOC 3 --- */
%mv_jobexecute(
  path=&mcTestAppLoc/jobs/temp,
  name=testjob,
  outds=work.info
)

/* --- BLOC 4 --- */
data work.info;
  set work.info;
  where method='GET' and rel='state';
run;

/* --- BLOC 5 --- */
%mv_jobwaitfor(ALL,inds=work.info,outds=work.jobstates)

/* --- BLOC 6 --- */
data _null_;
  set work.jobstates;
  call symputx('uri',uri);
run;

/* --- BLOC 7 --- */
%mp_assertscope(SNAPSHOT)
%mv_getjoblog(uri=%str(&uri),outref=mylog,mdebug=1)

/* --- BLOC 8 --- */
/* ignore auto proc json vars */
%mp_assertscope(COMPARE
  ,ignorelist=MCLIB2_JADP2LEN MCLIB2_JADPNUM MCLIB2_JADVLEN
)

/* --- BLOC 9 --- */
data _null_;
  infile mylog end=eof;
  input;
  putlog _infile_;
  retain found 0;
  if index(_infile_,'endsas;') then do;
    found=1;
    call symputx('found',found);
  end;
  else if eof and found ne 1 then call symputx('found',0);
run;

/* --- BLOC 10 --- */
%mp_assert(
  iftrue=(%str(&found)=1),
  desc=Check if the log was still fetched even though endsas was submitted
)

