Can SPSS check for the presence of data files?

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

Can SPSS check for the presence of data files?

Ron0z
I seem to recall in SQL you could test for something before carrying out an action.  For example, you might want to check to see if a table exists prior to attempting to create it.  Is there anything similar in SPSS?

For example, I regularly deal with groups of 4 data sets.  A data set will be created at the beginning of the year and as the year progresses a second, third, and fourth data set will be created as the year progresses.  My command files will call these data files.  Usually something like:  

add files    
/file = VLL15sys11    
/file = VLL15sys12    
/file = VLL15sys21    
/file = VLL15sys22.

So, here we are in 2016 and the appropriate usage would be as follows:

add files    
/file = VLL15sys11    
/file = VLL15sys12.

If I were to run the code with the first example of add files command (above) which calls all four files when only two exist an error will result and execution will stop.  What I’m looking for is something that would look like:

add files    
/if exists (VLL15sys11) then file = VLL15sys11    
/ if exists (VLL15sys12) then file = VLL15sys12    
/ if exists (VLL15sys21 ) then file = VLL15sys21    
/ if exists (VLL15sys21 ) then file = VLL15sys21.

(this is just a wish-list syntax)

In this way, I could leave the code untouched throughout the year irrespective whether there is one or more files present, and the code would run error free each time I ran it.  Can SPSS do anything like that, maybe using some other command(s)?
Reply | Threaded
Open this post in threaded view
|

Re: Can SPSS check for the presence of data files?

Jon Peck
Yes, you can do extensive generalization of syntax jobs using programmability.  Here is an example for your case.  It finds all the sav files in a specified directory whose names start with VLL15 and builds and runs an ADD FILES command for them.

begin program.
import spss, glob
files = glob.glob("c:/temp/VLL15*.sav")
if files:
  cmd = "\n".join(['/file="%s"' % item for item in files])
  print cmd
  spss.Submit("add files " + cmd)
end program.


On Mon, May 30, 2016 at 10:40 PM, Ron0z <[hidden email]> wrote:
I seem to recall in SQL you could test for something before carrying out an
action.  For example, you might want to check to see if a table exists prior
to attempting to create it.  Is there anything similar in SPSS?

For example, I regularly deal with groups of 4 data sets.  A data set will
be created at the beginning of the year and as the year progresses a second,
third, and fourth data set will be created as the year progresses.  My
command files will call these data files.  Usually something like:

add files
/file = VLL15sys11
/file = VLL15sys12
/file = VLL15sys21
/file = VLL15sys22.

So, here we are in 2016 and the appropriate usage would be as follows:

add files
/file = VLL15sys11
/file = VLL15sys12.

If I were to run the code with the first example of add files command
(above) which calls all four files when only two exist an error will result
and execution will stop.  What I’m looking for is something that would look
like:

add files
/if exists (VLL15sys11) then file = VLL15sys11
/ if exists (VLL15sys12) then file = VLL15sys12
/ if exists (VLL15sys21 ) then file = VLL15sys21
/ if exists (VLL15sys21 ) then file = VLL15sys21.

(this is just a wish-list syntax)

In this way, I could leave the code untouched throughout the year
irrespective whether there is one or more files present, and the code would
run error free each time I ran it.  Can SPSS do anything like that, maybe
using some other command(s)?




--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Can-SPSS-check-for-the-presence-of-data-files-tp5732274.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



--
Jon K Peck
[hidden email]

===================== 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: Can SPSS check for the presence of data files?

Ron0z
Hi Jon

The syntax you provided worked so well and perfect to my needs that it was almost scary.  I ran it several times in a little test file, and believe me when I say watching it run was thrilling. I changed file names in C drive to test it and the code picked up only the files with the appropriate names.  It was a delight to behold.  If you could only see the smile on my face!

I asked this question rather tentatively.  I almost didn’t raise it at all thinking there would be no way. I was almost sure someone would have responded; if you want to use database syntax use a database and not SPSS.   Then thought: If you don’t ask you don’t get.  Well, you have made my day.

Now, to a more pertinent issue: I don’t recognise the code.  What language is that and where can I study it?
Reply | Threaded
Open this post in threaded view
|

Re: Can SPSS check for the presence of data files?

Jon Peck
I am glad this was of help.  You can use wildcards in the file selection expression in the same way you can use them in a DOS or Windows Explorer command line.

The code is in Python and uses the functions provided by the Python plugin installed with Statistics.

There are vast resources for learning Python as it has become very popular, but the best starting point for Statistics users is the Programming and Data Management book available from the Community website (https://developer.ibm.com/predictiveanalytics/ or the old one at www.ibm.com/developerworks/spssdevcentral in the Books and Articles section).  Much of the book is devoted to examples of using Python to solve typical SPSS Statistics problems.

If you go for a standard Python book, be aware that Statistics uses Python 2.7 or, new in V24, offers a choice of 2.7 or 3.4, so choose appropriately.

On Tue, May 31, 2016 at 4:17 PM, Ron0z <[hidden email]> wrote:
Hi Jon

The syntax you provided worked so well and perfect to my needs that it was
almost scary.  I ran it several times in a little test file, and believe me
when I say watching it run was thrilling. I changed file names in C drive to
test it and the code picked up only the files with the appropriate names.
It was a delight to behold.  If you could only see the smile on my face!

I asked this question rather tentatively.  I almost didn’t raise it at all
thinking there would be no way. I was almost sure someone would have
responded; if you want to use database syntax use a database and not SPSS.
Then thought: If you don’t ask you don’t get.  Well, you have made my day.

Now, to a more pertinent issue: I don’t recognise the code.  What language
is that and where can I study it?




--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Can-SPSS-check-for-the-presence-of-data-files-tp5732274p5732292.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



--
Jon K Peck
[hidden email]

===================== 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: Can SPSS check for the presence of data files?

Ron0z
Many thanks, Ron
Reply | Threaded
Open this post in threaded view
|

Re: Can SPSS check for the presence of data files?

Bruce Weaver
Administrator
Thanks to you too, Jon.  ;-)

Ron0z wrote
Many thanks, Ron
--
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/).