Saving Today's Date as Part of Filename?

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

Saving Today's Date as Part of Filename?

Moser, Gary
Hello to everybody on the list,

 

I'm having no luck figuring out how to have the current date included as
part of the save out filename. I know this is probably very basic, but
it is eluding me.

 

It would be great to be able to do something like this:

 

Save out = 'c:\temp\filename_' + (TODAY'S DATE) + '.sav'.

 

Gary Moser

Research Analyst

Institutional Research

Dominican University of California

Ph: 415.482.2400

Fax:415.257.1367

Email: [hidden email] <mailto:[hidden email]>

 

 
Reply | Threaded
Open this post in threaded view
|

Re: Saving Today's Date as Part of Filename?

Richard Ristow
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 <[hidden email]>
Subject:      Re: file with actual date and time
To:           [hidden email]

See also:
Date:         Wed, 20 Jul 2005 19:19:04 -0400
From:         Richard Ristow <[hidden email]>
Subject:      Re: SPSS Macro to combine 'filename' + 'mmddyyy'
Comments: To: Vishal Dave <[hidden email]>
To:           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Saving Today's Date as Part of Filename?

Max Bell-2
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:[hidden email]] On Behalf Of
Richard Ristow
Sent: woensdag 19 juli 2006 6:53
To: [hidden email]
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 <[hidden email]>
Subject:      Re: file with actual date and time
To:           [hidden email]

See also:
Date:         Wed, 20 Jul 2005 19:19:04 -0400
From:         Richard Ristow <[hidden email]>
Subject:      Re: SPSS Macro to combine 'filename' + 'mmddyyy'
Comments: To: Vishal Dave <[hidden email]>
To:           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Saving Today's Date as Part of Filename?

Peck, Jon
In reply to this post by Moser, Gary
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:[hidden email]] On Behalf Of Richard Ristow
Sent: Tuesday, July 18, 2006 11:53 PM
To: [hidden email]
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 <[hidden email]>
Subject:      Re: file with actual date and time
To:           [hidden email]

See also:
Date:         Wed, 20 Jul 2005 19:19:04 -0400
From:         Richard Ristow <[hidden email]>
Subject:      Re: SPSS Macro to combine 'filename' + 'mmddyyy'
Comments: To: Vishal Dave <[hidden email]>
To:           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Saving Today's Date as Part of Filename?

Oliver, Richard
In reply to this post by Moser, Gary
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:[hidden email]] On Behalf Of Peck, Jon
Sent: Wednesday, July 19, 2006 6:49 AM
To: [hidden email]
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:[hidden email]] On Behalf Of Richard Ristow
Sent: Tuesday, July 18, 2006 11:53 PM
To: [hidden email]
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 <[hidden email]>
Subject:      Re: file with actual date and time
To:           [hidden email]

See also:
Date:         Wed, 20 Jul 2005 19:19:04 -0400
From:         Richard Ristow <[hidden email]>
Subject:      Re: SPSS Macro to combine 'filename' + 'mmddyyy'
Comments: To: Vishal Dave <[hidden email]>
To:           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Saving Today's Date as Part of Filename?

Moser, Gary
In reply to this post by Moser, Gary
Well, it's no wonder I was having no luck figuring this out. Thank you
very much Richard--I will put this to good use, and I'll sift through
Raynald's wonderful website as well.

FYI, For my data warehouse archiving purposes, I want to create a syntax
file with a bunch of include commands which will dump the outputs of
each included file into their respective archive folder. Then I can have
weekly snapshots of the database. Having to manually name each file
would be annoying.

I'm disappointed that SPSS does not have some easy way of doing this. I
would think a LOT of users would want to do this type of thing.

Gary Moser
Research Analyst
Institutional Research
Dominican University of California
Ph: 415.482.2400
Fax:415.257.1367
Email: [hidden email]


-----Original Message-----
From: Richard Ristow [mailto:[hidden email]]
Sent: Tuesday, July 18, 2006 9:53 PM
To: Moser, Gary; [hidden email]
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 <[hidden email]>
Subject:      Re: file with actual date and time
To:           [hidden email]

See also:
Date:         Wed, 20 Jul 2005 19:19:04 -0400
From:         Richard Ristow <[hidden email]>
Subject:      Re: SPSS Macro to combine 'filename' + 'mmddyyy'
Comments: To: Vishal Dave <[hidden email]>
To:           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Saving Today's Date as Part of Filename?

Moser, Gary
In reply to this post by Moser, Gary
My gratitude!

Gary Moser
Research Analyst
Institutional Research
Dominican University of California
Ph: 415.482.2400
Fax:415.257.1367
Email: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Saving Today's Date as Part of Filename? (without Python)

Alex Rasker
In reply to this post by Moser, Gary
From the reactions to this thread it's no wonder lots of people think SPSS
syntax is difficult to grasp.

Try (barring misplaced quotes etc) saving the following in your macro
workbook or as a separate file with an appropriate name
("C:\MyFolder\Set_Todays_Date.sps":

DEFINE !setdate ( )
DO IF $CASENUM=1.
WRITE OUTFILE 'C:\Temp\Todays_Date.sps'
/"DEFINE !today ( ) '" $DATETIME "' !ENDDEFINE.".
END IF.
INCLUDE FILE='C:\Temp\Todays_Date.sps'.
!ENDDEFINE.
!setdate.

where the "$DATETIME" (or $DATE) should be (whatever works) and whatever
format of the system value that gives you the date format you need.

Now put in your code (after activating a dataset):
INCLUDE FILE='C:\MyFolder\Set_Todays_Date.sps'.

And use :

SAVE OUTFILE='C:\Destinationfolder\Filename_' + !today + ' .sav'.
whenever you need.

Good luck,

Alex Rasker

From: "Moser, Gary" <[hidden email]>
Reply-To: "Moser, Gary" <[hidden email]>
To: [hidden email]
Subject: Re: Saving Today's Date as Part of Filename?
Date: Wed, 19 Jul 2006 09:22:18 -0700

Well, it's no wonder I was having no luck figuring this out. Thank you
very much Richard--I will put this to good use, and I'll sift through
Raynald's wonderful website as well.

FYI, For my data warehouse archiving purposes, I want to create a syntax
file with a bunch of include commands which will dump the outputs of
each included file into their respective archive folder. Then I can have
weekly snapshots of the database. Having to manually name each file
would be annoying.

I'm disappointed that SPSS does not have some easy way of doing this. I
would think a LOT of users would want to do this type of thing.

Gary Moser
Research Analyst
Institutional Research
Dominican University of California
Ph: 415.482.2400
Fax:415.257.1367
Email: [hidden email]


-----Original Message-----
From: Richard Ristow [mailto:[hidden email]]
Sent: Tuesday, July 18, 2006 9:53 PM
To: Moser, Gary; [hidden email]
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 <[hidden email]>
Subject:      Re: file with actual date and time
To:           [hidden email]

See also:
Date:         Wed, 20 Jul 2005 19:19:04 -0400
From:         Richard Ristow <[hidden email]>
Subject:      Re: SPSS Macro to combine 'filename' + 'mmddyyy'
Comments: To: Vishal Dave <[hidden email]>
To:           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Generalizability theory/ Generalizability coefficient

Jeff Stuewig
In reply to this post by Peck, Jon
        Has anybody used spss or created syntax (or macro) to compute
generalizability coefficients (i.e. Shavelson & Webb, 1991)?  We have a 5
item scale measured at 5 different time points.  Using alpha as a measure of
reliability our coefficients range from .65 to .69.  A reviewer suggested we
compute generalizability coefficients instead.  From what I've read I will
probably want to look at variance by person, item, and occasion but I'm
stumped as to how exactly to do this in spss and I didn't see anything on
Raynald's site.  Any help appreciated.  Thanks.


                        Jeff
Reply | Threaded
Open this post in threaded view
|

Re: Generalizability theory/ Generalizability coefficient

Sarraf, Shimon Aaron
Jeff,

I found some material (syntax, mock data, class notes, etc.) from my
psychometrics class here at IU-Bloomington related to calculating
g-coefficients.  If you like, I can send you the material from class.
All this can be done with SPSS but you'll need to reformat your data,
typically, before running the variance component analysis (from the drop
down menu: analyze-> general linear model -> variance component).

I would read Shavelson and Webb's "Generalizability Theory: A Primer" to
get the theoretical background on all of this, assuming you haven't read
it already.

Take care,

Shimon

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Jeff Stuewig
Sent: Friday, November 10, 2006 11:22 AM
To: [hidden email]
Subject: Generalizability theory/ Generalizability coefficient

        Has anybody used spss or created syntax (or macro) to compute
generalizability coefficients (i.e. Shavelson & Webb, 1991)?  We have a
5
item scale measured at 5 different time points.  Using alpha as a
measure of
reliability our coefficients range from .65 to .69.  A reviewer
suggested we
compute generalizability coefficients instead.  From what I've read I
will
probably want to look at variance by person, item, and occasion but I'm
stumped as to how exactly to do this in spss and I didn't see anything
on
Raynald's site.  Any help appreciated.  Thanks.


                        Jeff
Reply | Threaded
Open this post in threaded view
|

Re: Generalizability theory/ Generalizability coefficient

Patricia Rego
In reply to this post by Jeff Stuewig
Hello, Jeff

I've used SPSS to calculate variance components (GLM/Variance Components). However, you might find the software GENOVA (and its companion, "G-String") more useful. It is authored by Joe Crick and Robert Brennan and can be downloaded gratis. Robert Brennan's book (Generalizability Theory, 2001, Springer) is interesting and worthwhile, if heavy-going.

Regards
Patricia


______________________________
Patricia RĂ©go
Evaluation Officer
School of Medicine
The University of Queensland
(Ph: 61-7-33464683; [hidden email])

Evaluation Consultant
Skills Development Centre
Queensland Health
(Ph: 61-7-3636-6449; [hidden email])


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Jeff Stuewig
Sent: Saturday, 11 November 2006 2:22 AM
To: [hidden email]
Subject: Generalizability theory/ Generalizability coefficient

        Has anybody used spss or created syntax (or macro) to compute
generalizability coefficients (i.e. Shavelson & Webb, 1991)?  We have a 5
item scale measured at 5 different time points.  Using alpha as a measure of
reliability our coefficients range from .65 to .69.  A reviewer suggested we
compute generalizability coefficients instead.  From what I've read I will
probably want to look at variance by person, item, and occasion but I'm
stumped as to how exactly to do this in spss and I didn't see anything on
Raynald's site.  Any help appreciated.  Thanks.


                        Jeff