access environment variables from within spss

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

access environment variables from within spss

Albert-Jan Roskam
Hi,

How can I the values of environment variables under Spss for Windows? It's easy enough with Python (see below), but can it be done without it? It would be nice if onecould say:
FILE HANDLE mydir /NAME='%homedrive%'.
GET FILE =  'mydir/somefile.sav'.

The goal is to make many syntaxes as invulnerable to path name changes as possible. I experimented with HOST and SHOW ENVIRONMENT a bit, but this will become kludgy at best.

begin program python.
# first way
import os, spss
spss.Submit("file handle mydir0 /name = '%s'." % (os.getenv("temp")))
# second way (only with temp dir)
import tempfile, spss
spss.Submit("file handle mydir /name = '%s'." % (tempfile.gettempdir()))
end program.
 
Cheers!!
Albert-Jan  
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Reply | Threaded
Open this post in threaded view
|

Re: access environment variables from within spss

Jon K Peck
Environment variables can be used in FILE HANDLE and filespecs as you describe.

The variable must be defined before Statistics is started, since the process view of environment is not updated after a process is running, and it must be defined in scope for the Statistics process.  On Windows that would usually mean defining it via the Control Panel before starting Statistics.

I defined data as c:\spss20\samples\english.

Then I can do
get file="%data%\employee data.sav".

If I do
file handle ar/name="%data%".

then I can write
get file="ar\bankloan.sav",

If you run SHOW HANDLES, you can see the handle definitions with the environment variables resolved.

On Unix-like systems, you refer to environment variables as $var.

HTH

Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        Albert-Jan Roskam <[hidden email]>
To:        [hidden email]
Date:        02/03/2012 03:11 AM
Subject:        [SPSSX-L] access environment variables from within spss
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Hi,

How can I the values of environment variables under Spss for Windows? It's easy enough with Python (see below), but can it be done without it? It would be nice if onecould say:
FILE HANDLE mydir /NAME='%homedrive%'.
GET FILE =  'mydir/somefile.sav'.

The goal is to make many syntaxes as invulnerable to path name changes as possible. I experimented with HOST and SHOW ENVIRONMENT a bit, but this will become kludgy at best.

begin program python.
# first way
import os, spss
spss.Submit("file handle mydir0 /name = '%s'." % (os.getenv("temp")))
# second way (only with temp dir)
import tempfile, spss
spss.Submit("file handle mydir /name = '%s'." % (tempfile.gettempdir()))
end program.

 
Cheers!!
Albert-Jan  
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Reply | Threaded
Open this post in threaded view
|

Automatic reply: access environment variables from within spss

Marhefka, Stephanie

Hello. I will be out of the office  until February 6, 2012. I will do my best to respond to you my return.

 

Thank you,

Stephanie L. Marhefka, Ph.D.

 

Reply | Threaded
Open this post in threaded view
|

Re: access environment variables from within spss

Albert-Jan Roskam
In reply to this post by Jon K Peck
Hi Jon,
 
That's great, very handy! It seems that this is not documented. I thought it didn't work because I defined an environment variable from within spss, using HOST, to test whether it worked. And as you mentioned, this doesn't work. The combination  of environment variables and file handles seems nicest.
* path=\\theServer\theShare\theDir . /* path name prefix that only very occasionally changes.
file handle projectX /name='%path%/userX/projectX'.
get file = 'projectX/source.sav'.
 
But it might lead to hard-to-detect errors if the environment variable resolves to multiple locations.
What if I have a variable %temp% that refers to c:/temp, then d:/temp? I guess the first possible file is opened, isn't it?
 
Thanks again!
 
Regards,
Albert-Jan   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: Jon K Peck <[hidden email]>
To: [hidden email]
Sent: Friday, February 3, 2012 2:46 PM
Subject: Re: [SPSSX-L] access environment variables from within spss

Environment variables can be used in FILE HANDLE and filespecs as you describe.

The variable must be defined before Statistics is started, since the process view of environment is not updated after a process is running, and it must be defined in scope for the Statistics process.  On Windows that would usually mean defining it via the Control Panel before starting Statistics.

I defined data as c:\spss20\samples\english.

Then I can do
get file="%data%\employee data.sav".

If I do
file handle ar/name="%data%".

then I can write
get file="ar\bankloan.sav",

If you run SHOW HANDLES, you can see the handle definitions with the environment variables resolved.

On Unix-like systems, you refer to environment variables as $var.

HTH

Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        Albert-Jan Roskam <[hidden email]>
To:        [hidden email]
Date:        02/03/2012 03:11 AM
Subject:        [SPSSX-L] access environment variables from within spss
Sent by:        "SPSSX(r) Discussion" <[hidden email]>



Hi,

How can I the values of environment variables under Spss for Windows? It's easy enough with Python (see below), but can it be done without it? It would be nice if onecould say:
FILE HANDLE mydir /NAME='%homedrive%'.
GET FILE =  'mydir/somefile.sav'.

The goal is to make many syntaxes as invulnerable to path name changes as possible. I experimented with HOST and SHOW ENVIRONMENT a bit, but this will become kludgy at best.

begin program python.
# first way
import os, spss
spss.Submit("file handle mydir0 /name = '%s'." % (os.getenv("temp")))
# second way (only with temp dir)
import tempfile, spss
spss.Submit("file handle mydir /name = '%s'." % (tempfile.gettempdir()))
end program.

 
Cheers!!
Albert-Jan  
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Reply | Threaded
Open this post in threaded view
|

Re: access environment variables from within spss

Jon K Peck

Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        Albert-Jan Roskam <[hidden email]>
To:        Jon K Peck/Chicago/IBM@IBMUS, "[hidden email]" <[hidden email]>
Date:        02/03/2012 08:28 AM
Subject:        Re: [SPSSX-L] access environment variables from within spss




Hi Jon,
 
That's great, very handy! It seems that this is not documented. I thought it didn't work because I defined an environment variable from within spss, using HOST, to test whether it worked. And as you mentioned, this doesn't work. The combination  of environment variables and file handles seems nicest.
* path=\\theServer\theShare\theDir . /* path name prefix that only very occasionally changes.
file handle projectX /name='%path%/userX/projectX'.
get file = 'projectX/source.sav'.
>>>It is mentioned in passing in a few places in the CSR, but that could be clearer.
Processes are hierarchical.  HOST runs a subprocess, and the subprocess cannot influence the environment of the parent process.  You can use SHOW ENVIRONMENT to see what is defined to the Statistics backend process.
 
But it might lead to hard-to-detect errors if the environment variable resolves to multiple locations.
What if I have a variable %temp% that refers to c:/temp, then d:/temp? I guess the first possible file is opened, isn't it?
>>>I have never tried this, but I suspect that you would get a failure on that.  

You might find the STATS FIND FILE extension command useful.  Here is the beginning of the help.
Define a File Handle Pointing to the Location of a Specified File
The command creates a file handle pointing to the location where a file is found following a specified search strategy. The strategy consists of a list of locations to look in. This can be a list of folders and/or a list of environment variables whose values are lists of folders. The first location containing the file is the one used in defining the handle.

By using this command, you can create jobs that do not have to know exactly where their input data and syntax files reside.

The file specification can be a complete name or it can be a wildcard expression. Path information is not specified.

Regards,

Jon


 
Thanks again!
 
Regards,
Albert-Jan   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: Jon K Peck <[hidden email]>
To:
[hidden email]
Sent:
Friday, February 3, 2012 2:46 PM
Subject:
Re: [SPSSX-L] access environment variables from within spss


Environment variables can be used in FILE HANDLE and filespecs as you describe.

The variable must be defined before Statistics is started, since the process view of environment is not updated after a process is running, and it must be defined in scope for the Statistics process.  On Windows that would usually mean defining it via the Control Panel before starting Statistics.


I defined data as c:\spss20\samples\english.


Then I can do

get file="%data%\employee data.sav".


If I do

file handle ar/name="%data%".


then I can write

get file="ar\bankloan.sav",


If you run SHOW HANDLES, you can see the handle definitions with the environment variables resolved.


On Unix-like systems, you refer to environment variables as $var.


HTH


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621





From:        
Albert-Jan Roskam <[hidden email]>
To:        
[hidden email]
Date:        
02/03/2012 03:11 AM
Subject:        
[SPSSX-L] access environment variables from within spss
Sent by:        
"SPSSX(r) Discussion" <[hidden email]>



Hi,

How can I the values of environment variables under Spss for Windows? It's easy enough with Python (see below), but can it be done without it? It would be nice if onecould say:
FILE HANDLE mydir /NAME='%homedrive%'.
GET FILE =  'mydir/somefile.sav'.

The goal is to make many syntaxes as invulnerable to path name changes as possible. I experimented with HOST and SHOW ENVIRONMENT a bit, but this will become kludgy at best.

begin program python.
# first way
import os, spss
spss.Submit("file handle mydir0 /name = '%s'." % (os.getenv("temp")))
# second way (only with temp dir)
import tempfile, spss
spss.Submit("file handle mydir /name = '%s'." % (tempfile.gettempdir()))
end program.
 
Cheers!!
Albert-Jan  
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Reply | Threaded
Open this post in threaded view
|

Re: access environment variables from within spss

Bruce Weaver
Administrator
In reply to this post by Jon K Peck
Jon K Peck wrote
Environment variables can be used in FILE HANDLE and filespecs as you
describe.

The variable must be defined before Statistics is started, since the
process view of environment is not updated after a process is running, and
it must be defined in scope for the Statistics process.  On Windows that
would usually mean defining it via the Control Panel before starting
Statistics.

--- snip ---
For anyone who doesn't know what Jon is talking about, the following links might help.  The first one shows the Control Panel method he described; the second shows the SET command which could be used at the DOS prompt or in a .BAT file.

http://www.chem.gla.ac.uk/~louis/software/faq/q1.html
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/set.mspx?mfr=true

HTH.
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: access environment variables from within spss

Jon K Peck
Bruce,

Thanks for clarifying.  But users need to be careful about the DOS SET command.  A variable created with SET is only visible in that process or its children.  So in a bat file that also launches Statistics, it would work, but running the SET command and then starting Statistics from the Windows menus will not.

Regards,

Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        Bruce Weaver <[hidden email]>
To:        [hidden email]
Date:        02/03/2012 01:06 PM
Subject:        Re: [SPSSX-L] access environment variables from within spss
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Jon K Peck wrote
>
> Environment variables can be used in FILE HANDLE and filespecs as you
> describe.
>
> The variable must be defined before Statistics is started, since the
> process view of environment is not updated after a process is running, and
> it must be defined in scope for the Statistics process.  On Windows that
> would usually mean defining it via the Control Panel before starting
> Statistics.
>
> --- snip ---
>

For anyone who doesn't know what Jon is talking about, the following links
might help.  The first one shows the Control Panel method he described; the
second shows the SET command which could be used at the DOS prompt or in a
.BAT file.

http://www.chem.gla.ac.uk/~louis/software/faq/q1.html
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/set.mspx?mfr=true

HTH.


-----
--
Bruce Weaver
[hidden email]
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

NOTE: My Hotmail account is not monitored regularly.
To send me an e-mail, please use the address shown above.

--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/access-environment-variables-from-within-spss-tp5453395p5454931.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD


Reply | Threaded
Open this post in threaded view
|

Re: access environment variables from within spss

Bruce Weaver
Administrator
Ah, very good.  Thank you for clarifying too.  ;-)


Jon K Peck wrote
Bruce,

Thanks for clarifying.  But users need to be careful about the DOS SET
command.  A variable created with SET is only visible in that process or
its children.  So in a bat file that also launches Statistics, it would
work, but running the SET command and then starting Statistics from the
Windows menus will not.

Regards,

Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:   Bruce Weaver <[hidden email]>
To:     [hidden email]
Date:   02/03/2012 01:06 PM
Subject:        Re: [SPSSX-L] access environment variables from within
spss
Sent by:        "SPSSX(r) Discussion" <[hidden email]>



Jon K Peck wrote
>
> Environment variables can be used in FILE HANDLE and filespecs as you
> describe.
>
> The variable must be defined before Statistics is started, since the
> process view of environment is not updated after a process is running,
and
> it must be defined in scope for the Statistics process.  On Windows that
> would usually mean defining it via the Control Panel before starting
> Statistics.
>
> --- snip ---
>

For anyone who doesn't know what Jon is talking about, the following links
might help.  The first one shows the Control Panel method he described;
the
second shows the SET command which could be used at the DOS prompt or in a
.BAT file.

http://www.chem.gla.ac.uk/~louis/software/faq/q1.html
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/set.mspx?mfr=true


HTH.


-----
--
Bruce Weaver
[hidden email]
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

NOTE: My Hotmail account is not monitored regularly.
To send me an e-mail, please use the address shown above.

--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/access-environment-variables-from-within-spss-tp5453395p5454931.html

Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).