LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (July 2006)Back to main SPSSX-L pageJoin or leave SPSSX-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
=========================================================================
Date:         Wed, 19 Jul 2006 08:37:37 -0500
Reply-To:     "Oliver, Richard" <roliver@SPSS.COM>
Sender:       "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From:         "Oliver, Richard" <roliver@SPSS.COM>
Subject:      Re: Saving Today's Date as Part of Filename?
Comments: To: "Peck, Jon" <peck@spss.com>
Content-Type: text/plain; charset="US-ASCII"

Following up on the theme of how easy this is to do in Python, while the solution provided by Jon Peck is probably the best method for doing this, here's a crude but simple way that I cobbled together, and I'm definitely no Python expert (I'm barely a novice):

begin program. import spss, datetime today="'" + str(datetime.date.today()) + "'" spss.SetMacroValue('!today',today) end program. save outfile='filename_(' + !today + ').sav'.

The import statement gets the spss module installed with the SPSS plug-in, and the datetime module comes with Python. The job creates an SPSS string substitution macro named !today that contains the current date as a quoted string in the form 'yyyy-mm-dd'. (The original post requested a filename in the form filename_(date).sav. I put the underscore and the parentheses in the SAVE command specification, but you could just as easily create the string '_(date)' in Python.)

But as Jon Peck points out, this and many other commonly requested tasks can be accomplished with the many tools available at www.spss.com/devcentral.

-----Original Message----- From: SPSSX(r) Discussion [mailto:SPSSX-L@LISTSERV.UGA.EDU] On Behalf Of Peck, Jon Sent: Wednesday, July 19, 2006 6:49 AM To: SPSSX-L@LISTSERV.UGA.EDU Subject: Re: Saving Today's Date as Part of Filename?

If you have SPSS 14 and Python installed, you could do this.

BEGIN PROGRAM. import spss, spssaux2

spss.Submit("SAVE OUTFILE='" + spssaux2.CreateFileNameWDate() + "'.") END PROGRAM.

This little job runs a save command writing by default to a file whose name will be the name of the current active file with the date and time inserted before the .sav. So it might be fred_yyyy-mm-dd-hh--mm.sav The CreateFileNameWDate function is part of the spssaux2 module available from SPSS Developer Central (www.spss.com/devcentral) You can also call that function supplying a base name optionally including a path, and it will add the date/time stamp to it. (If the current active file is unnamed, you must supply a name.) And it is smart enough to remove the date/time stamp if present before constructing a new name.

And as an example of the power of Python + SPSS, the source code for this function, ignoring blank lines and comments, is just 11 lines long.

HTH Jon Peck SPSS

-----Original Message----- From: SPSSX(r) Discussion [mailto:SPSSX-L@LISTSERV.UGA.EDU] On Behalf Of Richard Ristow Sent: Tuesday, July 18, 2006 11:53 PM To: SPSSX-L@LISTSERV.UGA.EDU Subject: Re: [SPSSX-L] Saving Today's Date as Part of Filename?

At 07:38 PM 7/18/2006, Moser, Gary wrote:

>I'm having no luck figuring out how to have the current date included >as part of the save out filename. It would be great to be able to do >something like this: > >Save out = 'c:\temp\filename_' + (TODAY'S DATE) + '.sav'.

This is another one that comes up from time to time. It's not entirely simple, because it's dynamically modifying code - changing code based on data. (In this case, the data is the current date.)

If you have SPSS 14, the best solution is probably with Python, which I'm not up to speed on, yet, to post. For long before, the standard way to generate dynamic code was to write the code to an external file, often as a macro definition, and INCLUDE it. In your case, your "(TODAY'S DATE)" should be a macro that evaluates to the date, in the form you want it, IN QUOTES. (the pioneer and master of this technique is Raynald Levesque. There should be a number of relevant examples on his Web site, http://www.spsstools.net/.)

You'll have to modify this, but here's a solution I wrote and posted a while back(*). It creates a macro !act that evaluates to

'c:\TempStor\SPSS\dd-MMM-yyyy hh-mm.sav'

It's tested code; this is SPSS draft output. There's some code that's in just to trace what's happening, so it could be shortened. .......................... * Macro !MacEcho is used to display the macro expansion . DEFINE !MacEcho(!POS !NOEXPAND !CMDEND) ECHO !QUOTE(!CONCAT(' Call : ',!1)). ECHO !QUOTE(!CONCAT(' Result: ',!EVAL(!1))). !ENDDEFINE.

* Create the macro definition, and write it to a file . INPUT PROGRAM. . STRING TIME_NOW (A17). . COMPUTE TIME_NOW = STRING($TIME,DATETIME17). . END CASE. END FILE. END INPUT PROGRAM. /* Change colon in time value to a hyphen . COMPUTE #WHERE = INDEX(TIME_NOW,':'). COMPUTE SUBSTR(TIME_NOW,#WHERE,1) = '-'. STRING MAC_TEXT(A85). COMPUTE MAC_TEXT =CONCAT("define !act ()" ,"'" ,"c:\TempStor\SPSS\",TIME_NOW,".sav" ,"' " ,"!enddefine."). PRINT / 'Macro text is:' / MAC_TEXT. WRITE OUTFILE='c:\TempStor\SPSS\2004-04-01 Moser.Mac' / MAC_TEXT. EXECUTE. Macro text is: define !act ()'c:\TempStor\SPSS\01-APR-2004 14-15.sav' !enddefine. NEW FILE.

* Recover the macro definition, define the macro, and display . INCLUDE 'c:\TempStor\SPSS\2004-04-01 Moser.Mac'. 52 0 define !act ()'c:\TempStor\SPSS\01-APR-2004 14-15.sav' !enddefine. 55 0 57 0 * End of INCLUDE nesting level 01. !MacEcho !Act. Call : !Act Result: 'c:\TempStor\SPSS\01-APR-2004 14-15.sav'

* Use the macro to save and reload a test file . INPUT PROGRAM. . STRING MESSAGE(A40). . COMPUTE MESSAGE='Data in file named with current time'. . END CASE. END FILE. END INPUT PROGRAM. PRESERVE. SET MPRINT ON. SAVE OUTFILE=!act. 73 0 M> SAVE OUTFILE='c:\TempStor\SPSS\01-APR-2004 14-15.sav' 74 0 M> . 75 0 M> GET FILE=!act. 76 0 M> GET FILE='c:\TempStor\SPSS\01-APR-2004 14-15.sav' 77 0 M> . LIST. 78 0 M> LIST. List MESSAGE Data in file named with current time

Number of cases read: 1 Number of cases listed: 1 RESTORE. 79 0 M> RESTORE. .......................... (*) Citation: Date: Thu, 1 Apr 2004 14:22:26 -0500 From: Richard Ristow <wrristow@mindspring.com> Subject: Re: file with actual date and time To: SPSSX-L@LISTSERV.UGA.EDU

See also: Date: Wed, 20 Jul 2005 19:19:04 -0400 From: Richard Ristow <wrristow@mindspring.com> Subject: Re: SPSS Macro to combine 'filename' + 'mmddyyy' Comments: To: Vishal Dave <VishalDave@Affina.com>


Back to: Top of message | Previous page | Main SPSSX-L page