Date: Fri, 3 Feb 2012 08:40:15 -0700
Reply-To: Jon K Peck <peck@us.ibm.com>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: Jon K Peck <peck@us.ibm.com>
Subject: Re: access environment variables from within spss
In-Reply-To: <1328282794.70875.YahooMailNeo@web110714.mail.gq1.yahoo.com>
Content-Type: multipart/alternative;
Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
peck@us.ibm.com
new phone: 720-342-5621
From: Albert-Jan Roskam <fomcl@yahoo.com>
To: Jon K Peck/Chicago/IBM@IBMUS, "SPSSX-L@LISTSERV.UGA.EDU"
<SPSSX-L@LISTSERV.UGA.EDU>
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 <peck@us.ibm.com>
To: SPSSX-L@LISTSERV.UGA.EDU
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
peck@us.ibm.com
new phone: 720-342-5621
From: Albert-Jan Roskam <fomcl@yahoo.com>
To: SPSSX-L@listserv.uga.edu
Date: 02/03/2012 03:11 AM
Subject: [SPSSX-L] access environment variables from within spss
Sent by: "SPSSX(r) Discussion" <SPSSX-L@listserv.uga.edu>
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?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[text/html]