|
I would like to add the current date to the file name automatically when saving data files from SPSS using command syntax, as in
SAVE TRANSLATE OUTFILE='xy 2010-06-23.csv' (however, the date string should be updated automatically to the current date). Can anybody think of a solution? Andreas. |
|
|
Hi Albert-Jan:
Great idea! BUT: when I executze the program code I receive the erroe message 'Configration file spssdxcfg.ini is invalid because the LIB_NAME is NULL.' Which programming language did you use? I may have to install it first ... Cheers, Andreas |
|
|
In reply to this post by Frank Furter
You can generate file names with date and time information using the function CreateFileNameWDate in the spssaux2.py module available from SPSS Developer Central (www.spss.com/devcentral). There is a companion function in the same module named FindMostRecentFile that will return the file name of the most recent file by using the naming structure produced by the first function. Here is an example program that saves the active data file using this naming scheme. begin program. import spss, spssaux, spssaux2 spssaux.SaveDataFile(spssaux2.CreateFileNameWDate(basename="Andreas")) end program. You can omit the basename if the active file already has a name, and the function will build a dated name from the current name (removing any date stamp it already had). HTH, Jon Peck SPSS, an IBM Company [hidden email] 312-651-3435
I would like to add the current date to the file name automatically when saving data files from SPSS using command syntax, as in SAVE TRANSLATE OUTFILE='xy 2010-06-23.csv' (however, the date string should be updated automatically to the current date). Can anybody think of a solution? Andreas. -- View this message in context: http://old.nabble.com/Adding-a-date-to-a-file-name-through-syntax-tp28968491p28968491.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 has given a very neat solution to your question. It
relies on Python command syntax. I have done the same with SPSS syntax and
macros only quite a while ago. This macro is part of our CONCERTO macro libary.
If you like plain SPSS syntax I post the macro here, although I am not sure if
this is supported with current SPSS versions later than SPSS 12. Unfortunately
my mailer does not preserve tabs spacing, but I could ship the file directly if
you like.
/* [ Name ] -------------------------------------------*/ . /* GetSysDateTime (GET SYStem DATE and TIME) /* /* [ Status ] -----------------------------------------*/ . /* Author : Raynald Levesque, Georg Maubach (MRSC) /* Date : 03-MAY-2006 /* Status : released /* Version : 1 /* Release : 1 /* /* [ Platform ] ---------------------------------------*/ . /* Source System : SPSS (Win), Rel.12.0.1 (11 Nov 2003) /* Target System : SPSS (Win), Rel.12 /* /* [ Description ] ------------------------------------*/ . /* The macro extracts the system date and time /* information from the operating system and stores it /* in a numeric variable, in a string variable and/or /* in a macro for further usage. /* /* [ Parameter ] --------------------------------------*/ . /* pinc (quoted) = path for a temporary include /* file /* finc (quoted) = name of a temporary include /* file /* mode (quoted) = number of the mode the macro /* shall be run in /* /* [ Result ] ------------------------------------------*/ . /* The macro can deliver a numeric and/or a string /* variable containing the actual system date and time /* and/or a macro containing this information also as a /* string. /* /* [ Operation ] ---------------------------------------*/ . /* The system variable $time is stored in a variable in /* the dataset and then reformatted in the desired way. /* /* The format of the numeric date and time variable is /* DATETIME(24.2) and the format of the string variable /* is 'YYYYMMDD_HHMMSSTT' where /* YYYY: Year with for digits /* MM : Month with always two digits /* DD : Day with alway two digits /* HH : Hour /* MM : Minutes /* SS : Seconds /* TT : Milliseconds /* /* The modes are: /* /* 1 = only numeric SystemDatetime /* 2 = only string SystemDatetime /* 3 = both numeric and string SystemDatetime /* 4 = numeric and string SystemDatetime and macro with string System Datetime /* 5 = only macro with string SystemDatetime /* /* As a standard only the macro is created. /* /* [ Call ] --------------------------------------------*/ . /* @GetSysDateTime pinc = 'c:\temp' /* finc = 'dynamic.inc' /* mode = 5 . /* /* [ Example ] -----------------------------------------*/ . /* The macro @strSysDateTime could be used in other macros /* to included into a file name or a file label. /* /* [ Release Notes ] -----------------------------------*/ . /* None . /* /* [ Developer Notes ] ---------------------------------*/ . /* None . /* /*----------1---------2---------3---------4---------5---*/ . DEFINE @GetSysDateTime (pinc !TOKENS(1) !DEFAULT('c:\temp\') /finc !TOKENS(1) !DEFAULT('dynamic.inc') /mode !TOKENS(1) !DEFAULT(5) ) @trace '*===== [ Begin @GetSysDateTime ] =====*' . * Set english output language to be able to use english date format . PRESERVE . SET OLANG = ENGLISH . DO IF ($casenum = 1) . - COMPUTE numSysDateTime = $time . - FORMATS numSysDateTime (DATETIME24.2) . END IF . EXECUTE . * Generate variables . ** Set errors and warnings off in case the variable already exists. * PRESERVE . * SET ERRORS = OFF WARNINGS = OFF . STRING strSysDateTime (A24) . RESTORE . STRING day (a2) . STRING month3 (a3) . STRING month2 (a2) . STRING year (a4) . STRING time1 time2 time3 time4 (a2) . * Generate string variable in desired format . DO IF ($casenum = 1) . - COMPUTE strSysDateTime = STRING(numSysDateTime, DATETIME24.2) . - COMPUTE strSysDateTime = SUBSTR(strSysDateTime,2,23) . - COMPUTE day = SUBSTR(strSysDateTime,1,2) . - COMPUTE month3 = SUBSTR(strSysDateTime,4,6) . - COMPUTE year = SUBSTR(strSysDateTime,8,11) . - COMPUTE time1 = SUBSTR(strSysDateTime,13,2) . - COMPUTE time2 = SUBSTR(strSysDateTime,16,2) . - COMPUTE time3 = SUBSTR(strSysDateTime,19,2) . - COMPUTE time4 = SUBSTR(strSysDateTime,22,2) . - COMPUTE month3 = UPCASE(month3) . - RECODE month3 ('JAN' = '01') ('FEB' = '02') ('MAR' = '03') ('APR' = '04') ('MAY' = '05') ('JUN' = '06') ('JUL' = '07') ('AUG' = '08') ('SEP' = '09') ('OCT' = '10') ('NOV' = '11') ('DEC' = '12') (ELSE = '99') . - COMPUTE month2 = month3 . - COMPUTE strSysDateTime = CONCAT(year,month2,day,'_',time1,time2,time3,time4) . END IF . EXECUTE . * Delete not needed variables . MATCH FILES FILE = * /DROP day month3 month2 year time1 TO time4 . EXECUTE . * Evaluate parameter 'mode' . !IF (!UNQUOTE(!mode) = 1) !THEN MATCH FILES FILE = * /DROP strSysDateTime . !IFEND !IF (!UNQUOTE(!mode) = 2) !THEN MATCH FILES FILE = * /DROP numSysDateTime . !IFEND !IF (!UNQUOTE(!mode) = 3) !THEN /* nothing */ . !IFEND !IF (!UNQUOTE(!mode) = 4) !THEN DO IF ($casenum = 1) . WRITE OUTFILE = !QUOTE( !CONCAT( !UNQUOTE(!EVAL(!pinc)), !UNQUOTE(!EVAL(!finc)) ) ) / 'DEFINE @strSysDateTime () ' . WRITE OUTFILE = !QUOTE( !CONCAT( !UNQUOTE(!EVAL(!pinc)), !UNQUOTE(!EVAL(!finc)) ) ) / strSysDateTime . WRITE OUTFILE = !QUOTE( !CONCAT( !UNQUOTE(!EVAL(!pinc)), !UNQUOTE(!EVAL(!finc)) ) ) / '!ENDDEFINE .' . END IF . EXECUTE . INCLUDE FILE = !QUOTE( !CONCAT( !UNQUOTE(!EVAL(!pinc)), !UNQUOTE(!EVAL(!finc)) ) ) . !IFEND !IF (!UNQUOTE(!mode) = 5) !THEN DO IF ($casenum = 1) . WRITE OUTFILE = !QUOTE( !CONCAT( !UNQUOTE(!EVAL(!pinc)), !UNQUOTE(!EVAL(!finc)) ) ) / 'DEFINE @strSysDateTime () ' . WRITE OUTFILE = !QUOTE( !CONCAT( !UNQUOTE(!EVAL(!pinc)), !UNQUOTE(!EVAL(!finc)) ) ) / strSysDateTime . WRITE OUTFILE = !QUOTE( !CONCAT( !UNQUOTE(!EVAL(!pinc)), !UNQUOTE(!EVAL(!finc)) ) ) / '!ENDDEFINE .' . END IF . EXECUTE . MATCH FILES FILE = * /DROP numSysDateTime strSysDateTime . EXECUTE . INCLUDE FILE = !QUOTE( !CONCAT( !UNQUOTE(!EVAL(!pinc)), !UNQUOTE(!EVAL(!finc)) ) ) . !IFEND @trace '*===== [ End @GetSysDateTime ] =====*' . !ENDDEFINE .
* EOF * .
Best regards Georg
Von: SPSSX(r) Discussion [mailto:[hidden email]] Im Auftrag von Jon K Peck Gesendet: Mittwoch, 23. Juni 2010 14:56 An: [hidden email] Betreff: Re: Adding a date to a file name through syntax You can generate file names with date and time information using the function CreateFileNameWDate in the spssaux2.py module available from SPSS Developer Central (www.spss.com/devcentral). There is a companion function in the same module named FindMostRecentFile that will return the file name of the most recent file by using the naming structure produced by the first function. Here is an example program that saves the active data file using this naming scheme. begin program. import spss, spssaux, spssaux2 spssaux.SaveDataFile(spssaux2.CreateFileNameWDate(basename="Andreas")) end program. You can omit the basename if the active file already has a name, and the function will build a dated name from the current name (removing any date stamp it already had). HTH, Jon Peck SPSS, an IBM Company [hidden email] 312-651-3435
I would like to add the current date to the file name automatically when saving data files from SPSS using command syntax, as in SAVE TRANSLATE OUTFILE='xy 2010-06-23.csv' (however, the date string should be updated automatically to the current date). Can anybody think of a solution? Andreas. -- View this message in context: http://old.nabble.com/Adding-a-date-to-a-file-name-through-syntax-tp28968491p28968491.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 |
|
In reply to this post by Jon K Peck
Stop doing SPSS: watch England vs Slovenia
instead!
|
|
Administrator
|
Let's be fair about this, John...Jon may prefer watching USA v Algeria. ;-)
--
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/). |
|
Bruce Weaver wrote:
> Let's be fair about this, John...Jon may prefer watching USA v Algeria. ;-) > Congratulations USA, leaders of their group! Marta GG (Yes, I DO like soccer) > > John F Hall wrote: > >> Stop doing SPSS: watch England vs Slovenia instead! >> ----- Original Message ----- >> From: Jon K Peck >> To: [hidden email] >> Sent: Wednesday, June 23, 2010 2:56 PM >> Subject: Re: Adding a date to a file name through syntax >> -- For miscellaneous SPSS related statistical stuff, visit: http://gjyp.nl/marta/ ===================== 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 |
|
In reply to this post by Jon K Peck
|
|
You could certainly make the pattern more general, but that's something I did not want to do. I wanted to make sure that characters that would be invalid in file names on some OS's such as : and / or \ could not get into these strings. I also wanted to make sure that the dates were sufficiently consistent that the file names would sort in date order (within root). For example writing a month or day number without forcing the length of each datum to be the same wreaks havoc with string sorting. Re's are wonderful things, though, once you get past the somewhat arcane notation. Jon Peck SPSS, an IBM Company [hidden email] 312-651-3435
|
|
In reply to this post by Albert-Jan Roskam
Hi guys, I've been trying to get this working for older versions of spss (without using python): define !savewdate(fname=!tokens(1)) do if $casenum=1. write outfile "c:\$jdate.sps" /"define !crtdate() '",$jdate (date11),"' !enddefine.". end if. exec. include "c:\$jdate.sps". exec. *kill "c:\$jdate.sps". exec. save outfile=!quote(!concat(!unq(!fname),'_',!unq(!eval(!crtdate)),'.sav')) /compressed. !enddefine. !savewdate fname='c:\my_dated_file'. the only problem is that I cannot make it work from the first shot, it only evaluates correctly the !crtdate at second run... the second issue would be to make $jdate to read the correct date ... but i've got stuck in !crtdate for now :) any suggestions? Cheers, Marinica From: Albert-Jan Roskam <[hidden email]> To: [hidden email] Sent: Wed, June 23, 2010 10:52:44 AM Subject: Re: Adding a date to a file name through syntax
|
|
Administrator
|
In reply to this post by Frank Furter
Some of the other (non-Python) methods seem a bit complicated. Doesn't something simple like the following do what you want?
* Create some test data . data list free / v1 (f1.0) . begin data 1 2 3 4 5 end data. string #savecmd(a150). do if $casenum EQ 1. - compute #savecmd = concat("SAVE TRANSLATE OUTFILE='C:\temp\myfile_",$date,".csv' /TYPE=CSV /MAP /REPLACE /FIELDNAMES /CELLS=VALUES."). - write outfile = "C:\temp\save.txt" / #savecmd. end if. exe. include file = "C:\temp\save.txt". Jon, note that I deliberately used INCLUDE rather than INSERT, because there has been some mention of people wanting a method that would work with old versions of SPSS. ;-)
--
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/). |
|
Thanks everybody for all the creative answers - surely there are 'many roads that lead to Rome' (as a German proverb says).
Anyway: Do not miss Germany vs. England on Sunday ![]() Cheers from Germany Andreas |
|
Here in France we also will be
watching...
<IMG border=0 hspace=0 alt="" align=baseline
src="Q:\DCIM\100OLYMP\P6257733.JPG">
<IMG border=0 hspace=0 alt="" align=baseline
src="Q:\DCIM\100OLYMP\P6257734.JPG">
|
| Free forum by Nabble | Edit this page |
