=========================================================================
Date: Wed, 19 Jul 2006 06:49:25 -0500
Reply-To: "Peck, Jon" <peck@spss.com>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: "Peck, Jon" <peck@spss.com>
Subject: Re: Saving Today's Date as Part of Filename?
Content-Type: text/plain; charset="UTF-8"
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>