|
Hello, SPSS users, I've got a question on dynamic date writing:
I have daily exports of an increasing spss data file. File names differ in the date they were written, e.g. C:\data\090810_export.sav
C:\data\090811_export.sav ... I want to insert the date part of the file name automatically,
i.e. the actual date is to be detected by the system and inserted into the concatenated path. So far I use this solution: ===============================================
DEFINE !path ()
"C:\data\" !ENDDEFINE. DEFINE !date () "090811" !ENDDEFINE. SAVE OUTFILE = !path + !date + "_export.sav". ===============================================
Here I have to change the date part manually, however.
Any suggestions are really welcome. Good luck, Mario
Mario Giesel
Munich, Germany |
|
There are probably clever non-Python ways to do this, but if you're
using Python, getting the current local date is pretty easy using the datetime library: import datetime mydate = str(datetime.date.today()) which returns '2009-08-11' as a string. If you want the date to be formatted differently, as in your example, you can extract and rearrange the date components, e.g., something like... myyear = str(datetime.date.today().year) # extract the year from the date object, as a string mymonth = str(datetime.date.today().month) # extract the month from the date object, as a string myday = str(datetime.date.today().day) # extract the day from the date object, as a string datestring = myyear[2:4] + mymonth.zfill(2) + myday # retain just the last two digits of the year, and if the month is a single digit, left pad it with a '0' ...which gives datestring a value of '090811'. I'm sure there are more elegant ways to do this in Python, but you get the idea. Mario Giesel wrote: > > Hello, SPSS users, I've got a question on dynamic date writing: > > I have daily exports of an increasing spss data file. > File names differ in the date they were written, e.g. > C:\data\090810_export.sav > C:\data\090811_export.sav > ... > > I want to insert the date part of the file name automatically, > i.e. the actual date is to be detected by the system and inserted into > the concatenated path. So far I use this solution: > > =============================================== > DEFINE !path () > "C:\data\" > !ENDDEFINE. > DEFINE !date () > "090811" > !ENDDEFINE. > SAVE OUTFILE = !path + !date + "_export.sav". > =============================================== > > Here I have to change the date part manually, however. > Any suggestions are really welcome. > > Good luck, > Mario > > -- Daniel Robertson Senior Research and Planning Associate Institutional Research and Planning Cornell University / irp.cornell.edu ===================== 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 |
|
compute datevar=$date11.
will return the current date in the form dd-mmm-yyyy. If you don't like the dashes, add: datevar=replace(datevar, "-", ""). -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Daniel Robertson Sent: Tuesday, August 11, 2009 11:13 AM To: [hidden email] Subject: Re: Dynamic date writing There are probably clever non-Python ways to do this, but if you're using Python, getting the current local date is pretty easy using the datetime library: import datetime mydate = str(datetime.date.today()) which returns '2009-08-11' as a string. If you want the date to be formatted differently, as in your example, you can extract and rearrange the date components, e.g., something like... myyear = str(datetime.date.today().year) # extract the year from the date object, as a string mymonth = str(datetime.date.today().month) # extract the month from the date object, as a string myday = str(datetime.date.today().day) # extract the day from the date object, as a string datestring = myyear[2:4] + mymonth.zfill(2) + myday # retain just the last two digits of the year, and if the month is a single digit, left pad it with a '0' ...which gives datestring a value of '090811'. I'm sure there are more elegant ways to do this in Python, but you get the idea. Mario Giesel wrote: > > Hello, SPSS users, I've got a question on dynamic date writing: > > I have daily exports of an increasing spss data file. > File names differ in the date they were written, e.g. > C:\data\090810_export.sav > C:\data\090811_export.sav > ... > > I want to insert the date part of the file name automatically, > i.e. the actual date is to be detected by the system and inserted into > the concatenated path. So far I use this solution: > > =============================================== > DEFINE !path () > "C:\data\" > !ENDDEFINE. > DEFINE !date () > "090811" > !ENDDEFINE. > SAVE OUTFILE = !path + !date + "_export.sav". > =============================================== > > Here I have to change the date part manually, however. > Any suggestions are really welcome. > > Good luck, > Mario > > -- Daniel Robertson Senior Research and Planning Associate Institutional Research and Planning Cornell University / irp.cornell.edu ===================== 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 |
|
Well, leave it to me to come up with a complicated solution when a
simple one will do! djr Oliver, Richard wrote: > compute datevar=$date11. > > will return the current date in the form dd-mmm-yyyy. > > If you don't like the dashes, add: > > datevar=replace(datevar, "-", ""). > > -----Original Message----- > From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Daniel Robertson > Sent: Tuesday, August 11, 2009 11:13 AM > To: [hidden email] > Subject: Re: Dynamic date writing > > There are probably clever non-Python ways to do this, but if you're > using Python, getting the current local date is pretty easy using the > datetime library: > > import datetime > mydate = str(datetime.date.today()) > > which returns '2009-08-11' as a string. If you want the date to be > formatted differently, as in your example, you can extract and rearrange > the date components, e.g., something like... > > myyear = str(datetime.date.today().year) # extract the year from the > date object, as a string > mymonth = str(datetime.date.today().month) # extract the month from > the date object, as a string > myday = str(datetime.date.today().day) # extract the day from the date > object, as a string > datestring = myyear[2:4] + mymonth.zfill(2) + myday # retain just the > last two digits of the year, and if the month is a single digit, left > pad it with a '0' > > ...which gives datestring a value of '090811'. I'm sure there are more > elegant ways to do this in Python, but you get the idea. > > > Mario Giesel wrote: > >> Hello, SPSS users, I've got a question on dynamic date writing: >> >> I have daily exports of an increasing spss data file. >> File names differ in the date they were written, e.g. >> C:\data\090810_export.sav >> C:\data\090811_export.sav >> ... >> >> I want to insert the date part of the file name automatically, >> i.e. the actual date is to be detected by the system and inserted into >> the concatenated path. So far I use this solution: >> >> =============================================== >> DEFINE !path () >> "C:\data\" >> !ENDDEFINE. >> DEFINE !date () >> "090811" >> !ENDDEFINE. >> SAVE OUTFILE = !path + !date + "_export.sav". >> =============================================== >> >> Here I have to change the date part manually, however. >> Any suggestions are really welcome. >> >> Good luck, >> Mario >> >> >> > > > -- Daniel Robertson Senior Research and Planning Associate Institutional Research and Planning Cornell University / irp.cornell.edu ===================== 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 Mario Giesel
Listers, Here’s my issue. I’m trying to
manipulate the width of bars in this syntax. The x-axis is time in months. The
data is not being summed or averaged, it contains only one line per month. The
aggregations have already taken place. I am getting very skinny lines, one for
each month. I can manually go in and adjust the line width, which makes the
bars bigger. How can I do this in gpl? And is there a good reference for gpl? The
gpl reference manual is not really complete in my opinion. It seems to assume
some prior knowledge. Here’s the syntax. Can anyone tell
me how to make the bars thicker? I tried size.large and it didn’t work. Thanks Matt GGRAPH /GRAPHDATASET
NAME="graphdataset" VARIABLES=month_year opioids_sum MISSING=LISTWISE
REPORTMISSING=NO /GRAPHSPEC SOURCE=INLINE. BEGIN GPL SOURCE:
s=userSource(id("graphdataset")) DATA: month_year=col(source(s),
name("month_year")) DATA: opioids_sum=col(source(s),
name("opioids_sum")) COORD: rect(dim(1,2)) GUIDE: axis(dim(1),
label("Time")) GUIDE: axis(dim(2),
label("Total Opioid RXs")) ELEMENT:
interval(position(month_year*opioids_sum), shape.interior(shape.square),
size(size.large),
color.interior(color.green), color.exterior(color.green)) END GPL. Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 |
|
You’re getting very skinny bars because month is being
defined as a scalar variable and not a categorical variable. That is correct if
you actually have missing months in your variable since the bars will not be
evenly spaced.. You can also increase the width of the bar using the size function
on the ELEMENT statement which specifies a constant size (not a relative size
as your GPL does it). This however gets a little tricky if you have very
many bars because now the bars are not “autofit” the size of the
chart. So, to take control and leave your month_year variable as a scalar,
use size(size.”10px”) or something like that. Take a look at the
GPL reference in help for various arguments to the size function. From: SPSSX(r) Discussion
[mailto:[hidden email]] On Behalf Of Pirritano, Matthew Listers, Here’s my issue. I’m trying to manipulate the width of
bars in this syntax. The x-axis is time in months. The data is not being summed
or averaged, it contains only one line per month. The aggregations have already
taken place. I am getting very skinny lines, one for each month. I can manually
go in and adjust the line width, which makes the bars bigger. How can I do this
in gpl? And is there a good reference for gpl? The gpl reference manual is
not really complete in my opinion. It seems to assume some prior knowledge. Here’s the syntax. Can anyone tell me how to make the bars
thicker? I tried size.large and it didn’t work. Thanks Matt GGRAPH /GRAPHDATASET NAME="graphdataset" VARIABLES=month_year
opioids_sum MISSING=LISTWISE REPORTMISSING=NO /GRAPHSPEC SOURCE=INLINE. BEGIN GPL SOURCE: s=userSource(id("graphdataset")) DATA: month_year=col(source(s),
name("month_year")) DATA: opioids_sum=col(source(s),
name("opioids_sum")) COORD: rect(dim(1,2)) GUIDE: axis(dim(1), label("Time")) GUIDE: axis(dim(2), label("Total Opioid RXs")) ELEMENT: interval(position(month_year*opioids_sum),
shape.interior(shape.square), size(size.large),
color.interior(color.green), color.exterior(color.green)) END GPL. Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 |
|
Thanks ViAnn, Yes, my x-axis variable, time in months,
is scalar, and there is missing data for some months. I will give this a try. Thanks matt Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 From: ViAnn Beadle
[mailto:[hidden email]] You’re getting
very skinny bars because month is being defined as a scalar variable and not a
categorical variable. That is correct if you actually have missing months in
your variable since the bars will not be evenly spaced.. You can also increase
the width of the bar using the size function on the ELEMENT statement which
specifies a constant size (not a relative size as your GPL does it). This
however gets a little tricky if you have very many bars because now the bars
are not “autofit” the size of the chart. So, to take control
and leave your month_year variable as a scalar, use
size(size.”10px”) or something like that. Take a look at the GPL
reference in help for various arguments to the size function. From: SPSSX(r)
Discussion [mailto:[hidden email]] On
Behalf Of Pirritano, Matthew Listers, Here’s my issue. I’m trying to
manipulate the width of bars in this syntax. The x-axis is time in months. The
data is not being summed or averaged, it contains only one line per month. The
aggregations have already taken place. I am getting very skinny lines, one for
each month. I can manually go in and adjust the line width, which makes the
bars bigger. How can I do this in gpl? And is there a good reference for gpl? The
gpl reference manual is not really complete in my opinion. It seems to assume
some prior knowledge. Here’s the syntax. Can anyone tell
me how to make the bars thicker? I tried size.large and it didn’t work. Thanks Matt GGRAPH /GRAPHDATASET
NAME="graphdataset" VARIABLES=month_year opioids_sum MISSING=LISTWISE
REPORTMISSING=NO /GRAPHSPEC SOURCE=INLINE. BEGIN GPL SOURCE:
s=userSource(id("graphdataset")) DATA: month_year=col(source(s),
name("month_year")) DATA: opioids_sum=col(source(s),
name("opioids_sum")) COORD: rect(dim(1,2)) GUIDE: axis(dim(1),
label("Time")) GUIDE: axis(dim(2),
label("Total Opioid RXs")) ELEMENT:
interval(position(month_year*opioids_sum), shape.interior(shape.square),
size(size.large),
color.interior(color.green), color.exterior(color.green)) END GPL. Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 |
|
In reply to this post by Daniel Robertson
Hi Mario!
import os, time, spss path = r"c:\data" prefix = time.strftime("%Y-%m-%d") suffix = "_export.sav" spss.Submit("save outfile = '%s'." % (os.path.join(path, prefix + suffix)) The strftime module enables you to quickly change the time stamp format. For instance, "%Y%m%d" would give the 'computer style' ISO format (yyymmdd). See also [1]. [1] http://docs.python.org/library/time.html Cheers!! Albert-Jan --- On Tue, 8/11/09, Daniel Robertson <[hidden email]> wrote: > From: Daniel Robertson <[hidden email]> > Subject: Re: [SPSSX-L] Dynamic date writing > To: [hidden email] > Date: Tuesday, August 11, 2009, 6:12 PM > There are probably clever non-Python > ways to do this, but if you're > using Python, getting the current local date is pretty easy > using the > datetime library: > > import datetime > mydate = str(datetime.date.today()) > > which returns '2009-08-11' as a string. If you want the > date to be > formatted differently, as in your example, you can extract > and rearrange > the date components, e.g., something like... > > myyear = str(datetime.date.today().year) # extract > the year from the > date object, as a string > mymonth = str(datetime.date.today().month) # extract > the month from > the date object, as a string > myday = str(datetime.date.today().day) # extract the > day from the date > object, as a string > datestring = myyear[2:4] + mymonth.zfill(2) + myday > # retain just the > last two digits of the year, and if the month is a single > digit, left > pad it with a '0' > > ...which gives datestring a value of '090811'. I'm sure > there are more > elegant ways to do this in Python, but you get the idea. > > > Mario Giesel wrote: > > > > Hello, SPSS users, I've got a question on dynamic date > writing: > > > > I have daily exports of an increasing spss data file. > > File names differ in the date they were written, e.g. > > C:\data\090810_export.sav > > C:\data\090811_export.sav > > ... > > > > I want to insert the date part of the file name > automatically, > > i.e. the actual date is to be detected by the system > and inserted into > > the concatenated path. So far I use this solution: > > > > =============================================== > > DEFINE !path () > > "C:\data\" > > !ENDDEFINE. > > DEFINE !date () > > "090811" > > !ENDDEFINE. > > SAVE OUTFILE = !path + !date + "_export.sav". > > =============================================== > > > > Here I have to change the date part manually, > however. > > Any suggestions are really welcome. > > > > Good luck, > > Mario > > > > > > -- > Daniel Robertson > Senior Research and Planning Associate > Institutional Research and Planning > Cornell University / irp.cornell.edu > > ===================== > 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 |
|
Hello, just a short feedback and thanks on received solutions on my problem:
(a) I couldn't find a way how to use Richard's syntax to create a text string to be inserted in a "SAVE OUTFILE ..." command:================================= STRING datevar (A12). compute datevar=$date11. COMPUTE datevar=replace(datevar, "-", ""). =================================
(b) Daniel's solution might work well but I had no datetime module available to test it ... ================================= BEGIN PROGRAM PYTHON. import datetime mydate = str(datetime.date.today()) myyear = str(datetime.date.today().year) # extract the year from the date object, as a string mymonth = str(datetime.date.today().month) # extract the month from the date object, as a string myday = str(datetime.date.today().day) # extract the day from the date object, as a string datestring = myyear[2:4] + mymonth.zfill(2) + myday # retain just the last two digits of the year, and if the month is a single digit, left pad it with a '0' END PROGRAM. =================================
(c) ... and I didn't have to look for it as Albert-Jans solution works beautifully: ================================= BEGIN PROGRAM PYTHON. import os, time, spss # Pathname path = r"C:\data" # Filename 1st part prefix = time.strftime("%y%m%d") # Filename 2nd part suffix = "_export.sav" spss.Submit("""SAVE OUTFILE = '%s' """ % (os.path.join(path, prefix + suffix))) END PROGRAM. =================================
Thanks a lot for all contributions!! Mario Von: Albert-Jan Roskam <[hidden email]> An: [hidden email] Gesendet: Dienstag, den 11. August 2009, 21:58:55 Uhr Betreff: Re: Dynamic date writing Hi Mario! import os, time, spss path = r"c:\data" prefix = time.strftime("%Y-%m-%d") suffix = "_export.sav" spss.Submit("save outfile = '%s'." % (os.path.join(path, prefix + suffix)) The strftime module enables you to quickly change the time stamp format. For instance, "%Y%m%d" would give the 'computer style' ISO format (yyymmdd). See also [1]. [1] http://docs.python.org/library/time.html Cheers!! Albert-Jan --- On Tue, 8/11/09, Daniel Robertson <[hidden email]> wrote: > From: Daniel Robertson <[hidden email]> > Subject: Re: [SPSSX-L] Dynamic date writing > To: [hidden email] > Date: Tuesday, August 11, 2009, 6:12 PM > There are probably clever non-Python > ways to do this, but if you're > using Python, getting the current local date is pretty easy > using the > datetime library: > > import datetime > mydate = str(datetime.date.today()) > > which returns '2009-08-11' as a string. If you want the > date to be > formatted differently, as in your example, you can extract > and rearrange > the date components, e.g., something like... > > myyear = str(datetime.date.today().year) # extract > the year from the > date object, as a string > mymonth = str(datetime.date.today().month) # extract > the month from > the date object, as a string > myday = str(datetime.date.today().day) # extract the > day from the date > object, as a string > datestring = myyear[2:4] + mymonth.zfill(2) + myday > # retain just the > last two digits of the year, and if the month is a single > digit, left > pad it with a '0' > > ...which gives datestring a value of '090811'. I'm sure > there are more > elegant ways to do this in Python, but you get the idea. > > > Mario Giesel wrote: > > > > Hello, SPSS users, I've got a question on dynamic date > writing: > > > > I have daily exports of an increasing spss data file. > > File names differ in the date they were written, e.g. > > C:\data\090810_export.sav > > C:\data\090811_export.sav > > ... > > > > I want to insert the date part of the file name > automatically, > > i.e. the actual date is to be detected by the system > and inserted into > > the concatenated path. So far I use this solution: > > > > =============================================== > > DEFINE !path () > > "C:\data\" > > !ENDDEFINE. > > DEFINE !date () > > "090811" > > !ENDDEFINE. > > SAVE OUTFILE = !path + !date + "_export.sav". > > =============================================== > > > > Here I have to change the date part manually, > however. > > Any suggestions are really welcome. > > > > Good luck, > > Mario > > > > > > -- > Daniel Robertson > Senior Research and Planning Associate > Institutional Research and Planning > Cornell University / irp.cornell.edu > > ===================== > 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
Mario Giesel
Munich, Germany |
|
I found a way to use Richard’s $Date11 syntax in a “GET”
command (so SAVE should work too), but it’s very round-a-bout. It was
worth it for me because I set my syntax up once and then run it weekly from an “INSERT”
(so I never really even look at the syntax again once I write it—I’m
only ever highlighting 1 line). I’m not sure how clear this will be too explain, and
I’m sure there are more efficient ways to do this, but in case it will
help others, here it is First I created a small file that will be used every week
before my processing run. I called it DateStamp.sav. I initially created it
with this structure: *[NEW FILE.]. NUMERIC Rec (F1.0). STRING DateVar (A11) YearVar (A2) MonthVar (A2) DayVar
(A2). COMPUTE Rec=1. SAVE OUTFILE='C:\YourDirectory\DateStamp.sav'. (The Rec = 1 is basically just to create a row to populate
each week—it may not be absolutely necessary). My date had to be in the format of “yymmdd”. Richard’s
syntax gave a different format, so I open the file, create the system date
field, and then modify it into my format using the syntax below. (Not the most
efficient way, I’m sure). I then create a longer string field with the syntax for a
macro that uses the current date in it, drop all the other fields, and save the
one long string (called MacroDate) as an “.sps” file. Later when I run an insert of that .sps file, it creates
the macro for me. GET FILE='C:\YourDirectory\DateStamp.sav'. DATASET NAME DateStamp WINDOW=FRONT. *STRING DateVar (A11) YearVar (A2) MonthVar (A2) DayVar
(A2). COMPUTE datevar=$date11. COMPUTE YearVar = SUBSTR(DateVar,10,2). COMPUTE DayVar = SUBSTR(DateVar,1,2). IF(SUBSTR(DateVar,4,3)="JAN")MonthVar =
"01". IF(SUBSTR(DateVar,4,3)="FEB")MonthVar =
"02". IF(SUBSTR(DateVar,4,3)="MAR")MonthVar =
"03". IF(SUBSTR(DateVar,4,3)="APR")MonthVar =
"04". IF(SUBSTR(DateVar,4,3)="MAY")MonthVar =
"05". IF(SUBSTR(DateVar,4,3)="JUN")MonthVar =
"06". IF(SUBSTR(DateVar,4,3)="JUL")MonthVar =
"07". IF(SUBSTR(DateVar,4,3)="AUG")MonthVar =
"08". IF(SUBSTR(DateVar,4,3)="SEP")MonthVar =
"09". IF(SUBSTR(DateVar,4,3)="OCT")MonthVar =
"10". IF(SUBSTR(DateVar,4,3)="NOV")MonthVar =
"11". IF(SUBSTR(DateVar,4,3)="DEC")MonthVar =
"12". DATASET NAME DataSet1 WINDOW=FRONT. STRING MACRODate (A60). COMPUTE MACRODate = CONCAT('DEFINE !NewFiles()
',CONCAT(YearVar,MonthVar,DayVar),' !ENDDEFINE.'). exe. DELETE VARIABLES Rec DateVar YearVar MonthVar DayVar. WRITE OUTFILE='C:\YourDirectory\Macro_DateStamp.sps' TABLE /MACRODate . EXECUTE. INSERT FILE = 'C:\YourDirectory\Macro_DateStamp.sps'. The output after that insert looks like this: INSERT FILE = 'C:\YourDirectory\Macro_DateStamp.sps'. 17 0 DEFINE !NewFiles() 090817 !ENDDEFINE. 18 0 19 0 * End of INSERT and INCLUDE nesting level 01. Then
I run my import statement: DEFINE !GetData (newfiles=!tokens(1)). GET DATA /TYPE = TXT /FILE = !QUOTE(!CONCAT('C:\MyDirectory\new',!Newfiles,'.txt')) /FIXCASE = 1 /ARRANGEMENT = FIXED /FIRSTCASE = 1 /IMPORTCASE = ALL /VARIABLES = /1 MyVariable1 0-11 A12 MyVariable2 12-24 A13 [Etc. for my data structure] MyLastField 227-232 A6. CACHE. EXECUTE. DATASET NAME DataSet1 WINDOW=FRONT. !ENDDEFINE. SET PRINTBACK=ON /MPRINT=ON. !GetData newfiles=!NewFiles . SET MPRINT= OFF. That’s it. It re-defines the date macro each time
you run it based on the day you run it. Hope it makes sense. -Heidi From: SPSSX(r)
Discussion [mailto:[hidden email]] On
Behalf Of Mario Giesel Hello, just a short feedback and thanks on received solutions on
my problem: (a) I couldn't find a way how to use Richard's syntax
to create a text string to be inserted in a "SAVE OUTFILE ..."
command: ================================= STRING datevar (A12). compute datevar=$date11. COMPUTE datevar=replace(datevar, "-", ""). ================================= (b) Daniel's solution might work well but I had no datetime
module available to test it ... ================================= BEGIN PROGRAM PYTHON. import datetime mydate = str(datetime.date.today()) myyear = str(datetime.date.today().year) # extract the year from the date object, as a string mymonth = str(datetime.date.today().month) # extract the month from the date object, as a string myday = str(datetime.date.today().day) # extract the day from the date object, as a string datestring = myyear[2:4] + mymonth.zfill(2) + myday # retain just the last two digits of the year, and if the month
is a single digit, left pad it with a '0' END PROGRAM. ================================= (c) ... and I didn't have to look for it as Albert-Jans solution
works beautifully: ================================= BEGIN PROGRAM PYTHON. import os, time, spss # Pathname path = r"C:\data" # Filename 1st part prefix = time.strftime("%y%m%d") # Filename 2nd part suffix = "_export.sav" spss.Submit("""SAVE OUTFILE = '%s'
""" % (os.path.join(path, prefix + suffix))) END PROGRAM. ================================= Thanks a lot for all contributions!! Mario Von: Albert-Jan
Roskam <[hidden email]> ____________ DefenderMX2. |
|
In reply to this post by Mario Giesel
Well, here’s one old-fashioned
non-Python way: data list free /x. begin data 1 2 3 end data. string datevar (a11). compute datevar=$date11. compute
datevar=replace(datevar, "-", ""). alter type datevar (amin). do if $casenum=1. write outfile='c:\temp\temp.sps' /"save outfile='c:\temp\",
datevar, ".sav'.". end if. execute. insert
file="c:\temp\temp.sps". This is, of course, a bit
of a hack. From: SPSSX(r)
Discussion [mailto:[hidden email]] On
Behalf Of Mario Giesel Hello, just a short feedback and thanks on received solutions on
my problem: (a) I couldn't find a way how to use Richard's syntax
to create a text string to be inserted in a "SAVE OUTFILE ..."
command: ================================= STRING datevar (A12). compute datevar=$date11. COMPUTE datevar=replace(datevar, "-", ""). ================================= (b) Daniel's solution might work well but I had no datetime
module available to test it ... ================================= BEGIN PROGRAM PYTHON. import datetime mydate = str(datetime.date.today()) myyear = str(datetime.date.today().year) # extract the year from the date object, as a string mymonth = str(datetime.date.today().month) # extract the month from the date object, as a string myday = str(datetime.date.today().day) # extract the day from the date object, as a string datestring = myyear[2:4] + mymonth.zfill(2) + myday # retain just the last two digits of the year, and if the month
is a single digit, left pad it with a '0' END PROGRAM. ================================= (c) ... and I didn't have to look for it as Albert-Jans solution
works beautifully: ================================= BEGIN PROGRAM PYTHON. import os, time, spss # Pathname path = r"C:\data" # Filename 1st part prefix = time.strftime("%y%m%d") # Filename 2nd part suffix = "_export.sav" spss.Submit("""SAVE OUTFILE = '%s'
""" % (os.path.join(path, prefix + suffix))) END PROGRAM. ================================= Thanks a lot for all contributions!! Mario Von: Albert-Jan
Roskam <[hidden email]> |
|
In reply to this post by Heidi Green
There is a utility function available in the spssaux2.py supplementary
programmability module named CreateFileNameWDate. It creates a filespec
of the form Basename_datetime.ext If no basename is specified when it is called, it uses the
filename of the current active dataset if it has one (removing any previous date
stamp). So, to save the current named active file with a date stamp, you
could do this. begin program. import spss, spssaux2 name = spssaux2.CreateFileNameWDate() spss.Submit(“SAVE OUTFILE=’%s’. % name) end program. Or specify a base name (including the .sav for data files) in
the call to CreateFileNameWDate. The datetime stamp code cleans up any characters that would be
illegal in a filespec. There is also a companion function named FindMostRecentFile(basename) that will return the most recent file using the naming rules
above so that OS operations such as file copying that could change the date
stamp don’t affect its operation. HTH, Jon Peck From: SPSSX(r) Discussion
[mailto:[hidden email]] On Behalf Of Heidi Green I
found a way to use Richard’s $Date11 syntax in a “GET”
command (so SAVE should work too), but it’s very round-a-bout. It was
worth it for me because I set my syntax up once and then run it weekly from an
“INSERT” (so I never really even look at the syntax again once I write
it—I’m only ever highlighting 1 line). I’m
not sure how clear this will be too explain, and I’m sure there are more
efficient ways to do this, but in case it will help others, here it is First
I created a small file that will be used every week before my processing run. I
called it DateStamp.sav. I initially created it with this structure: *[NEW
FILE.]. NUMERIC
Rec (F1.0). STRING
DateVar (A11) YearVar (A2) MonthVar (A2) DayVar (A2). COMPUTE
Rec=1. SAVE
OUTFILE='C:\YourDirectory\DateStamp.sav'. (The
Rec = 1 is basically just to create a row to populate each week—it may
not be absolutely necessary). My
date had to be in the format of “yymmdd”. Richard’s
syntax gave a different format, so I open the file, create the system date field,
and then modify it into my format using the syntax below. (Not the most
efficient way, I’m sure). I
then create a longer string field with the syntax for a macro that uses the
current date in it, drop all the other fields, and save the one long string
(called MacroDate) as an “.sps” file. Later
when I run an insert of that .sps file, it creates the macro for me. GET
FILE='C:\YourDirectory\DateStamp.sav'. DATASET
NAME DateStamp WINDOW=FRONT. *STRING
DateVar (A11) YearVar (A2) MonthVar (A2) DayVar (A2). COMPUTE
datevar=$date11. COMPUTE
YearVar = SUBSTR(DateVar,10,2). COMPUTE
DayVar = SUBSTR(DateVar,1,2). IF(SUBSTR(DateVar,4,3)="JAN")MonthVar
= "01". IF(SUBSTR(DateVar,4,3)="FEB")MonthVar
= "02". IF(SUBSTR(DateVar,4,3)="MAR")MonthVar
= "03". IF(SUBSTR(DateVar,4,3)="APR")MonthVar
= "04". IF(SUBSTR(DateVar,4,3)="MAY")MonthVar
= "05". IF(SUBSTR(DateVar,4,3)="JUN")MonthVar
= "06". IF(SUBSTR(DateVar,4,3)="JUL")MonthVar
= "07". IF(SUBSTR(DateVar,4,3)="AUG")MonthVar
= "08". IF(SUBSTR(DateVar,4,3)="SEP")MonthVar
= "09". IF(SUBSTR(DateVar,4,3)="OCT")MonthVar
= "10". IF(SUBSTR(DateVar,4,3)="NOV")MonthVar
= "11". IF(SUBSTR(DateVar,4,3)="DEC")MonthVar
= "12". DATASET
NAME DataSet1 WINDOW=FRONT. STRING
MACRODate (A60). COMPUTE
MACRODate = CONCAT('DEFINE !NewFiles() ',CONCAT(YearVar,MonthVar,DayVar),'
!ENDDEFINE.'). exe. DELETE
VARIABLES Rec DateVar YearVar MonthVar DayVar. WRITE
OUTFILE='C:\YourDirectory\Macro_DateStamp.sps'
TABLE /MACRODate . EXECUTE. INSERT
FILE = 'C:\YourDirectory\Macro_DateStamp.sps'. The
output after that insert looks like this: INSERT FILE =
'C:\YourDirectory\Macro_DateStamp.sps'. 17 0 DEFINE
!NewFiles() 090817 !ENDDEFINE. 18 0 19 0 * End of INSERT
and INCLUDE nesting level 01. Then I run my import statement: DEFINE !GetData (newfiles=!tokens(1)). GET DATA /TYPE = TXT /FILE =
!QUOTE(!CONCAT('C:\MyDirectory\new',!Newfiles,'.txt')) /FIXCASE = 1 /ARRANGEMENT = FIXED /FIRSTCASE = 1 /IMPORTCASE = ALL /VARIABLES = /1 MyVariable1 0-11 A12 MyVariable2 12-24
A13 [Etc. for my data structure]
MyLastField 227-232 A6. CACHE. EXECUTE. DATASET NAME DataSet1 WINDOW=FRONT. !ENDDEFINE. SET PRINTBACK=ON /MPRINT=ON. !GetData newfiles=!NewFiles . SET MPRINT= OFF. That’s
it. It re-defines the date macro each time you run it based on the day you run
it. Hope it makes sense. -Heidi From: SPSSX(r) Discussion
[mailto:[hidden email]] On Behalf Of Mario Giesel Hello, just a short
feedback and thanks on received solutions on my problem: (a) I couldn't find
a way how to use Richard's syntax to create a text string to be inserted in a "SAVE
OUTFILE ..." command: ================================= STRING datevar (A12). compute
datevar=$date11. COMPUTE
datevar=replace(datevar, "-", ""). ================================= (b) Daniel's
solution might work well but I had no datetime module available to test it ... ================================= BEGIN PROGRAM
PYTHON. import datetime mydate =
str(datetime.date.today()) myyear =
str(datetime.date.today().year) # extract the year
from the date object, as a string mymonth =
str(datetime.date.today().month) # extract the month
from the date object, as a string myday = str(datetime.date.today().day)
# extract the day
from the date object, as a string datestring =
myyear[2:4] + mymonth.zfill(2) + myday # retain just the
last two digits of the year, and if the month is a single digit, left pad it
with a '0' END PROGRAM. ================================= (c) ... and I
didn't have to look for it as Albert-Jans solution works beautifully: ================================= BEGIN PROGRAM
PYTHON. import os, time,
spss # Pathname path =
r"C:\data" # Filename 1st part prefix = time.strftime("%y%m%d") # Filename 2nd part suffix =
"_export.sav" spss.Submit("""SAVE
OUTFILE = '%s' """ % (os.path.join(path, prefix + suffix))) END PROGRAM. ================================= Thanks a lot for
all contributions!! Mario Von: Albert-Jan Roskam
<[hidden email]>
|
| Free forum by Nabble | Edit this page |
