Dear All,
I have been searching for a way to include the value of a specific variable when saving my data through syntax. Each week I recieve 4 files containing updated data on our subjects. I've written syntax that combines these files and generates some output. The syntax also extracts the day and month the last new case was added and writes that date to a new variable (the value is the same for all cases). Now i want to save the outfile and have spss automaticaly include that date in the filename. I've already searched for a macro or python script that can do this but can only find how to include the current date. I was wondering if somebody knows a way to do this or can point me in the right direction. Thanks in advance, Thoring |
Administrator
|
Here's an old-school BP solution that Jon will hate, even though I used INSERT FILE rather than INCLUDE FILE. ;-)
data list list / v1 to v3 (3f2.0) lastdate (date11) . begin data. 1 2 3 20-Apr-2011 4 5 6 20-Apr-2011 7 8 9 20-Apr-2011 end data. do if $casenum EQ 1. - write outfile = "C:\temp\save file.SPS" / "save outfile = 'C:\temp\filename (", lastdate,").sav' ." . end if. execute. insert file = "C:\temp\save file.SPS". The INSERT FILE line expands to give: 8813 0 save outfile = 'C:\temp\filename (20-APR-2011).sav' . 8814 0 8815 0 * End of INSERT and INCLUDE nesting level 01. HTH.
--
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/). |
In reply to this post by thoring
Thoring,
Here's a python block I use to add a datamart extract date to my file name, where the extract date is contained in the variable SNAPSHOT_DATE (which gets imported from the datamart formatted as DATE20). The block uses the spssdata module to read SNAPSHOT_DATE from the first row in the dataset. BEGIN PROGRAM. import spss, spssdata data = spssdata.Spssdata(indexes=('SNAPSHOT_DATE'), cvtDates=('SNAPSHOT_DATE',)) mydate=str(data.fetchone()[0])[0:10] data.close() spss.Submit(r""" SAVE OUTFILE = 'X:\Data\FinAid\IRP_FinAid_Award_%(mydate)s.SAV' . """ % {'mydate':mydate}) END PROGRAM. -- Daniel Robertson Senior Research and Planning Associate Institutional Research and Planning Cornell University / irp.cornell.edu -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of thoring Sent: Wednesday, April 20, 2011 9:51 AM To: [hidden email] Subject: [SPSSX-L] include value from variable in filename when saving Dear All, I have been searching for a way to include the value of a specific variable when saving my data through syntax. Each week I recieve 4 files containing updated data on our subjects. I've written syntax that combines these files and generates some output. The syntax also extracts the day and month the last new case was added and writes that date to a new variable (the value is the same for all cases). Now i want to save the outfile and have spss automaticaly include that date in the filename. I've already searched for a macro or python script that can do this but can only find how to include the current date. I was wondering if somebody knows a way to do this or can point me in the right direction. Thanks in advance, Thoring -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/include-value-from-variable-in-filename-when-saving-tp4326879p4326879.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 ===================== 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 Bruce Weaver
Glad to see the use of INSERT rather than
INCLUDE, but here is a programmability solution that removes the hack of
writing out a syntax file. :-)
begin program. import spss with spss.DataStep(): last = spss.Dataset(cvtDates=True).cases[0][-1] cmd = """save outfile='c:/temp/filename(%s-%s-%s).sav'""" % (last.day, last.month, last.year) spss.Submit(cmd) end program. Jon Peck Senior Software Engineer, IBM [hidden email] 312-651-3435 From: Bruce Weaver <[hidden email]> To: [hidden email] Date: 04/20/2011 08:24 AM Subject: Re: [SPSSX-L] include value from variable in filename when saving Sent by: "SPSSX(r) Discussion" <[hidden email]> Here's an old-school BP solution that Jon will hate, even though I used INSERT FILE rather than INCLUDE FILE. ;-) data list list / v1 to v3 (3f2.0) lastdate (date11) . begin data. 1 2 3 20-Apr-2011 4 5 6 20-Apr-2011 7 8 9 20-Apr-2011 end data. do if $casenum EQ 1. - write outfile = "C:\temp\save file.SPS" / "save outfile = 'C:\temp\filename (", lastdate,").sav' ." . end if. execute. insert file = "C:\temp\save file.SPS". The INSERT FILE line expands to give: 8813 0 save outfile = 'C:\temp\filename (20-APR-2011).sav' . 8814 0 8815 0 * End of INSERT and INCLUDE nesting level 01. HTH. thoring wrote: > > Dear All, > > I have been searching for a way to include the value of a specific > variable when saving my data through syntax. > > Each week I recieve 4 files containing updated data on our subjects. I've > written syntax that combines these files and generates some output. The > syntax also extracts the day and month the last new case was added and > writes that date to a new variable (the value is the same for all cases). > > Now i want to save the outfile and have spss automaticaly include that > date in the filename. I've already searched for a macro or python script > that can do this but can only find how to include the current date. > > I was wondering if somebody knows a way to do this or can point me in the > right direction. > > Thanks in advance, > > Thoring > ----- -- Bruce Weaver [hidden email] http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." NOTE: My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/include-value-from-variable-in-filename-when-saving-tp4326879p4327908.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 Daniel J. Robertson
Thanks to everyone who replied. I used Daniels pyhton code as it seemed the most easy to adapt to my situation. And it worked like a charm!
|
Free forum by Nabble | Edit this page |