DATA Step, Macro, Functions and more

How to overwrite Windows environment variables by Enterprise Gude

Reply
Occasional Contributor
Posts: 8

How to overwrite Windows environment variables by Enterprise Gude

Hi

I trying to overwrite windows local environment variables by executing batch through x command.

In normal mode, I want to "appservercontext_env_usermods.bat" to set production environment path to local environment variables.

For staging mode, I want to excute "appservercontext_env_usermods_env.bat" to overwrite local environment variables value to staging environment path.

But even "appservercontext_env_usermods_env.bat" executed successfully, envronment variables values stays production environment.

x command seems defferent session from enterprise guide.
Is there any way to set variables in current session?

 

program is following.

/*-----------------------------------------------------
Switch Staging Environment

This Program switches Environment variables that are set by appservercontext_env_usermods.bat to Staging Environment Value.

2018/03/05 :Ver1.00
-----------------------------------------------------*/
%macro echofile(file);
%macro dummy; %mend dummy;
data _null_; infile &file; input; put _infile_; run;
%mend echofile;

%let _APPSERVER_ROOT=%sysfunc(sysget(APPSERVER_ROOT));
%let _BatName=appservercontext_env_usermods_dev.bat;

%let fullbatpath=&_APPSERVER_ROOT\&_BatName;
%let sysrc=0;


filename xxx temp encoding="shift-jis";
%let path=%sysfunc(pathname(xxx));
x "call &fullbatpath >""&path"" 2>&1";
%echofile(xxx);

data _null_;
if &sysrc=0 then
do; put "NOTE: &_BatName is successfully Executed."; end;
else
do; put "ERROR: &_BatName finished with ERROR. Session is aborted";
abort return 2; end;
run;


%let _AZCOPY_PATH=%sysfunc(sysget(AZCOPY_PATH));
%let _AZAccount=%sysfunc(sysget(AZAccount));
%let _AZ_Key=%sysfunc(sysget(AZ_Key));
%let _AZ_Container=%sysfunc(sysget(AZ_Container));


%PUT &_AZCOPY_PATH;
%PUT &_AZAccount;
%PUT &_AZ_Key;
%PUT &_AZ_Container;

----------------------------------------------------------------------

 

Execution result is following.

 

45 filename xxx temp encoding="shift-jis";
46 %let path=%sysfunc(pathname(xxx));
47 x "call &fullbatpath >""&path"" 2>&1"
47 ! ;
48 %echofile(xxx);

NOTE: The infile XXX is:
Filename=F:\SASWORK\_TD17280_SMDC-T102SMD01_\#LN00014,
RECFM=V,LRECL=65534,File Size (bytes)=1719,
Last Modified=05Mar2018:23:51:02,
Create Time=05Mar2018:23:51:02

2 The SAS System Monday, March 5, 2018 11:40:00 PM


F:\03_SAS\Config\Lev1\SMDApp>REM /*--------------------------------------------------------------------\

F:\03_SAS\Config\Lev1\SMDApp>REM | |

F:\03_SAS\Config\Lev1\SMDApp>REM | Script to extend appservercontext_env.bat via user modifications. |

F:\03_SAS\Config\Lev1\SMDApp>REM | |

F:\03_SAS\Config\Lev1\SMDApp>REM \--------------------------------------------------------------------*/

F:\03_SAS\Config\Lev1\SMDApp>Set CONTEXT_USERMODS_OPTIONS=

F:\03_SAS\Config\Lev1\SMDApp>Set AZCOPY_PATH=C:\Program Files (x86)\Microsoft SDKs\Azure\AzCopy

F:\03_SAS\Config\Lev1\SMDApp>Set HostName=smdc-t102smd01

F:\03_SAS\Config\Lev1\SMDApp>Set Drive=F:

F:\03_SAS\Config\Lev1\SMDApp>Set SMD_PATH=F:\104_SMD

F:\03_SAS\Config\Lev1\SMDApp>Set SMD_Tool=F:\105_SMD_Tools_dev

F:\03_SAS\Config\Lev1\SMDApp>Set DATA_TRANSFER_PATH=F:\110_DataTransfer

F:\03_SAS\Config\Lev1\SMDApp>Set DPT_PATH=F:\105_SMD_Tools\FBXDataPort\DataPort

F:\03_SAS\Config\Lev1\SMDApp>Set DPT_EXE=DivaDataPortExecuter.exe

F:\03_SAS\Config\Lev1\SMDApp>Set DPT_DTsrc=DB1

F:\03_SAS\Config\Lev1\SMDApp>Set DPT_IMP=F:\105_SMD_Tools\FBXDataPort\Data\Import

F:\03_SAS\Config\Lev1\SMDApp>Set DPT_EXP=F:\105_SMD_Tools\FBXDataPort\Data\Export

F:\03_SAS\Config\Lev1\SMDApp>Set AZCOPY_PATH=C:\Program Files (x86)\Microsoft SDKs\Azure\AzCopy

F:\03_SAS\Config\Lev1\SMDApp>Set AZAccount=smdct102blb01

F:\03_SAS\Config\Lev1\SMDApp>Set AZ_Key=F:\105_SMD_Tools\AzureStorage\StorageKeys\Key4DH.dat

F:\03_SAS\Config\Lev1\SMDApp>Set AZ_Container=interfacedata
NOTE: 42 records were read from the infile XXX.
The minimum record length was 0.
The maximum record length was 110.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

49
50 data _null_;
51 if &sysrc=0 then
52 do; put "NOTE: &_BatName is successfully Executed."; end;
53 else
54 do; put "ERROR: &_BatName finished with ERROR. Session is aborted";
55 abort return 2; end;
56 run;
3 The SAS System Monday, March 5, 2018 11:40:00 PM


NOTE: appservercontext_env_usermods_dev.bat is successfully Executed.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

57
58
59 %let _AZCOPY_PATH=%sysfunc(sysget(AZCOPY_PATH));
60 %let _AZAccount=%sysfunc(sysget(AZAccount));
61 %let _AZ_Key=%sysfunc(sysget(AZ_Key));
62 %let _AZ_Container=%sysfunc(sysget(AZ_Container));
63
64
65 %PUT &_AZCOPY_PATH;
C:\Program Files (x86)\Microsoft SDKs\Azure\AzCopy
66 %PUT &_AZAccount;
smdct102blb01
67 %PUT &_AZ_Key;
F:\05_SMD_Tools\AzureStorage\StorageKeys\Key4DH.dat
68 %PUT &_AZ_Container;
interfacedata
69
70 GOPTIONS NOACCESSIBLE;
71 %LET _CLIENTTASKLABEL=;
72 %LET _CLIENTPROCESSFLOWNAME=;
73 %LET _CLIENTPROJECTPATH=;
74 %LET _CLIENTPROJECTPATHHOST=;
75 %LET _CLIENTPROJECTNAME=;
76 %LET _SASPROGRAMFILE=;
77 %LET _SASPROGRAMFILEHOST=;
78
79 ;*';*";*/;quit;run;
80 ODS _ALL_ CLOSE;
81
82
83 QUIT; RUN;
84

 

thanks

SAS Employee
Posts: 127

Re: How to overwrite Windows environment variables by Enterprise Gude

[ Edited ]
Posted in reply to AsakoOkano

You cannot modify an environment variable value for the currently running Windows process from a command line invoked in that process. When the command line is invoked, it spawns a NEW "child" process which inherits its characteristics from the currently running "parent" process. Setting an environment variable in that command window modifies the environment only for the child process, not the parent process. You can demonstrate the problem it this way:

1. Make a batch file (setenv.bat) that sets an environment variable value, then queries the environment for the new value and writes the result to a text file:

 

set test="This is a test" 
set TEST > C:\TEMP\env.txt

2. Execute the batch from SAS, then read in the text file:

 

options noxwait;
data _null_;
   if _n_=1 then do;
      command='C:\temp\setenv.bat';
      call system(command);
   END;
run;

data _null_;
   infile "c:\temp\env.txt";
   input;
   put _INFILE_;
run;

A look at the log tells us that, in the Windows session where the SET command executed, the variable was actually set:

 

 

145  data _null_;
146     if _n_=1 then do;
147        command='C:\temp\setenv.bat';
148        call system(command);
149     END;
150  run;

NOTE: DATA statement used (Total process time):
      real time           0.18 seconds
      cpu time            0.06 seconds

151
152  data _null_;
153     infile "c:\temp\env.txt";
154     input;
155     put _INFILE_;
156  run;

NOTE: The infile "c:\temp\env.txt" is:
      Filename=c:\temp\env.txt,
      RECFM=V,LRECL=32767,File Size (bytes)=21,
      Last Modified=05Mar2018:13:28:43,
      Create Time=05Mar2018:13:26:52

test="This is a test"
NOTE: 1 record was read from the infile "c:\temp\env.txt".
      The minimum record length was 19.
      The maximum record length was 19.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

3. Write a program to query the current Windows environment for a variable we are can be sure exists (to test the process) and then for the value we set with the batch file:

 

 

data _null_;
   sashome=sysget('sashome');
   put sashome=;
   test=sysget('test');
   put test=;
run;

The SAS log shows success getting the SASHOME variable value:

 

 

INFO: Character variables have defaulted to a length of 200 at the
places given by: (Line):(Column). Truncation can result.
180:4 sashome
182:4 test
sashome=C:\Program Files\SASHome

 

but the TEST variable does not exist in this (the parent) Windows session:

 

NOTE: Invalid argument to function SYSGET('test') at line 182 column 9.
test=
sashome=C:\Program Files\SASHome test=  _ERROR_=1 _N_=1


I'm thinking that you probably just want to modify an environment variable for the current SAS session, and that can easily be done at invocation. Either

 

1. Add the specification to the SAS configuration file, or adding it to the command that invokes SAS. If you had the SAS Windowing Edition installed, you could look at the properties for the shortcut that starts SAS. The value in the Target: box on the Shortcut tab looks something like this:

 

"C:\Program Files\SASHome\SASFoundation\9.4\sas.exe" -CONFIG "C:\Program Files\SASHome\SASFoundation\9.4\nls\en\sasv9.cfg" 

To set the environment variable TEST for the SAS sessions invoked by this shortcut, just change that to read:

  

"C:\Program Files\SASHome\SASFoundation\9.4\sas.exe" -CONFIG "C:\Program Files\SASHome\SASFoundation\9.4\nls\en\sasv9.cfg"  set test "This is a test"

2. Alternatively, you can edit the configuration file and add the following line:

   -test "This is a test"


In the above example, you can see from the shortcut that the configuration file can be found at:
C:\Program Files\SASHome\SASFoundation\9.4\nls\en\sasv9.cfg 


Because you are using Enterprise Guide, you may not have access to modify SAS invocation options. If you are just using Enterprise Guide as the interface for locally installed SAS, you should be able to find and update the configuration file. For a shared SAS environment with a SAS workspace server, you will need to contact the SAS administrator.

 
I hope this helps.

Mark

 

Ask a Question
Discussion stats
  • 1 reply
  • 253 views
  • 1 like
  • 2 in conversation