=========================================================================
Date: Wed, 19 Jul 2006 12:34:45 +0200
Reply-To: Max Bell <m.bell@bellherrmann.nl>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: Max Bell <m.bell@bellherrmann.nl>
Subject: Re: Saving Today's Date as Part of Filename?
In-Reply-To: A<7.0.1.0.2.20060719002625.0276fd10@mindspring.com>
Content-Type: text/plain; charset="us-ascii"
I made a syntax called saveBackup.sps to do this job.
I include a syntax to define a save-with-date-macro and then I call that
macro with a number of variables.
I assume the file C:\temp\StandBackUp.sav exists. This can be any
existing file, in my case the file contains one variable called 's'
which has numeric value '1').
In the follwing example I make a backup of file C:\temp\xxx.sav to
directory C:\temp\backup\
INCLUDE 'c:\.....\SaveBackup.SPS'.
!Backup DirRoot= 'C:\' SubPath= 'temp' BackupPath= 'Backup' BasisNaam=
'xxx' .
The macro does the following:
- determines what date it is
- writes save-command syntax to a file (including date specification)
- opens file C:\temp\xxx.sav
- and saves it to
C:\temp\backup\xxxyear-month-day-hours-minutes-seconds.sav (e.g.
xxx20060718_14-55-33.sav)
Macro SaveBackup.SPS:
DEFINE !BackUp (DirRoot= !TOKENS(1) / SubPath= !TOKENS(1) / BackupPath=
!TOKENS(1) / BasisNaam= !TOKENS(1) / Naam = !TOKENS(1))
GET FILE='C:\temp\StandBackUp.sav'.
COMPUTE d = CONCAT('"',STRING(XDATE.YEAR($TIME),F4.0),
STRING(XDATE.MONTH($TIME),F2.0), STRING(XDATE.MDAY($TIME),F2.0),'_',
STRING(XDATE.HOUR($TIME),F2.0),'-', STRING(XDATE.MINUTE($TIME),F2.0),
'-',STRING(XDATE.SECOND($TIME),F2.0), '.sav"') .
LOOP IF INDEX(d," ")>0.
+COMPUTE SUBSTR(d,INDEX(d," "),1)="0".
END LOOP.
EXE.
COMPUTE a = "SAVE OUTFILE=" .
COMPUTE b = !QUOTE(!CONCAT('
"',!UNQUOTE(!dirRoot),'\',!UNQUOTE(!SubPath
),'\',!UNQUOTE(!BackupPath),'\',!UNQUOTE(!BasisNaam),'"+')) .
WRITE OUTFILE= 'C:\Temp\Save.SPS' / a .
WRITE OUTFILE= 'C:\Temp\Save.SPS' / b .
WRITE OUTFILE= 'C:\Temp\Save.SPS' / ' ', d .
WRITE OUTFILE= 'C:\Temp\Save.SPS' / ' /COMPRESSED.' .
EXE.
GET FILE= !QUOTE(!CONCAT(!UNQUOTE(!dirRoot),'\',!UNQUOTE(!SubPath
),'\',!UNQUOTE(!BasisNaam),'.sav')) .
INCLUDE 'C:\Temp\Save.sps' .
!ENDDEFINE .
Greetings, Max
-----Original Message-----
From: SPSSX(r) Discussion [mailto:SPSSX-L@LISTSERV.UGA.EDU] On Behalf Of
Richard Ristow
Sent: woensdag 19 juli 2006 6:53
To: SPSSX-L@LISTSERV.UGA.EDU
Subject: Re: 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>
|