/******************************************************************************
 * Programme : Bestimmung des Sitzungsbenutzers
 * Reference : BESTIM486F
 * Source    : https://www.wearecas.eu/en/sampleCode/BESTIM486F
 ******************************************************************************/

/* --- BLOC 1 --- */
%macro mf_getuser(
)/*/STORE SOURCE*/;
  %local user;

  %if %symexist(_sasjs_username) %then %let user=&_sasjs_username;
  %else %if %symexist(SYS_COMPUTE_SESSION_OWNER) %then %do;
    %let user=&SYS_COMPUTE_SESSION_OWNER;
  %end;
  %else %if %symexist(_metaperson) %then %do;
    %if %length(&_metaperson)=0 %then %let user=&sysuserid;
    /* sometimes SAS will add @code_sas/DYNAMIC_ASSIGNMENT_OF_AUTHDOMAIN.sas extension - remove for consistency */
    /* but be sure to quote in case of usernames with commas */
    %else %let user=%unquote(%scan(%quote(&_metaperson),1, @));
  %end;
  %else %let user=&sysuserid;

  %quote(&user)

%mend mf_getuser;

/* --- BLOC 2 --- */
/* DYNAMIC_ASSIGNMENT_OF_AUTHDOMAIN.sas */
%put &sysuserid.;
%macro setenv;
%global access_auth;
%if %substr(&sysuserid.,1,3) = app %then %do;
	%let access_auth = DefaultAuth;
%end;
%else %do;
	%let access_auth = LDAP_Auth;
%end;
%mend setenv;
%setenv;

%put _all_;

/* --- BLOC 3 --- */
%put NOTE: You have called the macro _VERSION, 2006-02-15.;
%put NOTE: Copyright (c) 2001-2006 Rodney Sparapani;
%put;

/*
Author:  Rodney Sparapani <rsparapa @mcw.edu>
Created: 2001-00-00

This file is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this file; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/* _VERSION Documentation

    Returns a one (true) if the current version of SAS is equal to or
    greater than the version requested; zero (false) otherwise.
    
    POSITIONAL Parameters  
    
    ARG1            version requested
    
    Specific OPTIONAL Parameters
    
    NOTES=          default is to not display the returned value in a NOTE,
                    if set to something, then do display
                    
    VERSION=ARG1    alias 
*/

%macro _version(arg1, notes=, version=&arg1);

%local result;
%let result=%eval(%scan(&sysver, 1, .)*100+0%scan(&sysver, 2, .)>=%scan(&version, 1, .)*100+0%scan(&version, 2, .));
%if %length(&notes) %then %put NOTE: _VERSION is returning the value &result;

&result

%mend _version;

%*VALIDATION TEST STREAM;

/* --- BLOC 4 --- */
/*** HELP START ***//**
  @code_sas_json_prod_multi/mf_getuniquefileref_de.json mm_getdetails.sas
  @code_sas/macro_with_brief_tag.sas extracts metadata attributes and associations for a particular uri
  @code_sas_json_prod/print_macro_parameters.json [in] uri the metadata object for which to return
    attributes / associations
  @code_sas_json_prod/print_macro_parameters.json [in] sortoptions= Enables sorting of the output datasets, for example,
    `SORTSEQ=LINGUISTIC`
  @code_sas_json_prod/print_macro_parameters.json [out] outattrs= (work.attributes)
    The dataset to create that contains the list of attributes
  @code_sas_json_prod/print_macro_parameters.json [out] outassocs= (work.associations)
    The dataset to contain the list of associations

  <h4> Related Files </h4>
  @code_sas_json_prod_multi/mf_getuniquelibref.test_de.json mm_getobjects.sas
  @code_sas_json_prod_multi/mf_getuniquelibref.test_de.json mm_gettypes.sas

**//*** HELP END ***/

%macro mm_getdetails(uri
  ,outattrs=work.attributes
  ,outassocs=work.associations
  ,sortoptions=
)/*/STORE SOURCE*/;

data &outassocs;
  keep assoc assocuri name;
  length assoc assocuri name $256;
  call missing(of _all_);
  rc1=1;n1=1;
  do while(rc1>0);
    /* Walk through all possible associations of this object. */
    rc1=metadata_getnasl("&uri",n1,assoc);
    rc2=1;n2=1;
    do while(rc2>0);
      /* Walk through all the associations on this machine object. */
      rc2=metadata_getnasn("&uri",trim(assoc),n2,assocuri);
      if (rc2>0) then do;
        rc3=metadata_getattr(assocuri,"Name",name);
        output;
      end;
      call missing(name,assocuri);
      n2+1;
    end;
    n1+1;
  end;
run;
proc sort &sortoptions;
  by assoc name;
run;

data &outattrs;
  keep type name value;
  length type $4 name $256 value $32767;
  rc1=1;n1=1;type='Prop';name='';value='';
  do while(rc1>0);
    rc1=metadata_getnprp("&uri",n1,name,value);
    if rc1>0 then output;
    n1+1;
  end;
  rc1=1;n1=1;type='Attr';
  do while(rc1>0);
    rc1=metadata_getnatr("&uri",n1,name,value);
    if rc1>0 then output;
    n1+1;
  end;
run;
proc sort &sortoptions;
  by type name;
run;

%mend mm_getdetails;

