/******************************************************************************
 * Programme : Erstellen einer FCMP-Funktion zum Schreiben in eine Datei
 * Reference : ERSTELB41A
 * Source    : https://www.wearecas.eu/fr/sampleCode/ERSTELB41A
 ******************************************************************************/

/* --- BLOC 1 --- */
proc fcmp outlib=&lib..&cat..&pkg;

function mcf_string2file(filepath $, string $, mode $);
  if mode='APPEND' then fmode='a';
  else fmode='o';
  length fref $8;
  rc=filename(fref,filepath);
  if (rc ne 0) then return( -1 );
  fid = fopen(fref,fmode);
  if (fid = 0) then return( -1 );
  rc=fput(fid, string);
  rc=fwrite(fid);
  rc=fclose(fid);
  rc=filename(fref);
  return(0);
endsub;


quit;

/* --- BLOC 2 --- */
/* insert the CMPLIB if not already there */
%let cmpval=%sysfunc(getoption(cmplib));
%let found=0;
%do i=1 %to %sysfunc(countw(&cmpval,%str( %(%))));
  %let var=%scan(&cmpval,&i,%str( %(%)));
  %if &var=&lib..&cat %then %let found=1;
%end;
%if &found=0 %then %do;
  options insert=(CMPLIB=(&lib..&cat));
%end;

