/**************************************************************************************/ 
/* This code establishes a connection to your SAS Viya session.  It also defines      */
/* macros that will be used for uploading data to Viya and downloading your results.  */
/* Your Viya and CAS sessions must already be running.  Contact your system           */
/* administrator for values to use for these parameters.                              */
/*                                                                                    */
/* Fill in your value for each of the following parameters displayed within <> in     */
/* the code below:                                                                    */
/*    <viyahost>:  Viya host name. e.g., host.your.company.com                        */
/*    <viyaport>:  Viya port. e.g., 1234                                              */
/*    <username>:  Your user name to log in to SAS Viya                               */
/*    <password>:  Your SAS Viya password. Note:  We recommend you first run proc     */
/*                 pwencode to encode the value to use in your code.  For more        */
/*                 information, refer to user documentation.  Here is an example:     */ 
/*                 proc pwencode in='password';                                       */
/*    <cashost>:   CAS host name. e.g., node001.your.company.com                      */
/*    <casport>:   CAS port. e.g., 1234                                               */
/**************************************************************************************/

/* Specify SAS/CONNECT sign-on parameters for the Viya SAS client */
%let viyahost=<viyahost>;

/* Specify your SAS Viya username and password to login to SAS Viya */
signon viyahost.<viyaport> user=<username> pwd="<password>";

/* Remote submit the code from SAS 9.4 to SAS Viya */
rsubmit;

/* Specify the Viya CAS server parameters */
options cashost="<cashost>" casport=<casport>;

/*  Create a CAS session with default name CASAUTO */
cas; 

/* Create CAS engine librefs for existing caslibs */
caslib _all_ assign;

/* Define a macro used to supply ODS parameters to the SAS Viya host */
%macro _Begin_Viya_ODS_();
     ods _all_ close;
     filename _htmlout temp;
     ods html5 (id=web) device=png encoding=utf8 file=_htmlout style=Htmlblue 
           options(bitmap_mode='inline' webdms='on' svg_mode='inline' 
           css_prefix=".ods_&submissionid." body_id="div_&submissionid.");
     ods html5(id=web) style=Htmlblue;
%mend _Begin_Viya_ODS_;

/* Define a macro used to transfer ODS results from the SAS Viya host to the SAS 9.4 host */
%macro _End_Viya_ODS_;
     ods _all_ close;
     proc download infile=_htmlout outfile=_htmlout binary;
     run;
     filename _htmlout clear;
     ods listing;
%mend _End_Viya_ODS_;

endrsubmit;

/* Define a macro used to communicate the SAS 9.4 submission id to the SAS Viya host */
%macro _Get_Studio_SubmissionID_;
     ods _all_ close;
     filename _htmlout temp;
     %let _imagespos=%index(&_SASWSTEMP_, /.images/);
     %let submissionid=%substr(&_SASWSTEMP_, &_imagespos+9);
     %syslput submissionid=&submissionid;
%mend _Get_Studio_SubmissionID_;

