英文:
How to save SPSS outfile export with current system date in filename, formatted as yyyy-mm-dd
问题
此代码 来自Raynald的SPSS工具成功地将系统日期添加到输出文件名中,但日期的格式如下:
filename_20-MAR-23.sav
我需要将当前日期添加到输出文件名中,使用格式yyyy-mm-dd,如下所示:
filename_2023-03-20.sav
我的安装(SPSS 27)的语法参考文件包括日期格式YMDHMS,看起来更接近我需要的格式,但我只想要日期,不想要时间:
我已经尝试将YMDHMS和其他日期格式的语法替换到Raynald的代码中,但到目前为止都没有成功。有人知道如何实现吗?谢谢!
以下是供参考的代码:
* 目标:将当前日期保存为文件名的一部分。
* rlevesque@videotron.ca 2004/05/24。
* 定义虚拟数据。
DATA LIST FREE /a.
BEGIN DATA
1 2 3
END DATA.
* 定义一个宏,用于生成当前日期。
STRING #cdate(A9).
COMPUTE #cdate=$DATE.
DO IF $casenum=1.
WRITE OUTFILE 'c:\\temp\\temp.sps' /"DEFINE !date()'"#cdate"!ENDDEFINE.".
END IF.
EXECUTE.
* 运行宏。
INCLUDE 'c:\\temp\\temp.sps'.
SAVE OUTFILE='c:\\temp\\example1_' + !date + '.sav'.
英文:
This code from Raynald's SPSS Tools successfully adds the system date to an output filename, but the date comes out formatted like this:
filename_20-MAR-23.sav
I need to add the current date to output filenames using the format yyyy-mm-dd, like this:
filename_2023-03-20.sav
The syntax reference file for my installation (SPSS 27) includes the date format YMDHMS which looks more like what I need, but I only want the date, not the time:
I have tried substituting YMDHMS and other date format syntax into Raynald's code, but nothing has worked so far. Does anyone know how to do this? Thank you!
Here is the code for reference:
* Objective: Save data file with the current date as part of the name.
* rlevesque@videotron.ca 2004/05/24.
* Define dummy data .
DATA LIST FREE /a.
BEGIN DATA
1 2 3
END DATA.
* Define a macro which will yield the current date.
STRING #cdate(A9).
COMPUTE #cdate=$DATE.
DO IF $casenum=1.
WRITE OUTFILE 'c:\\temp\\temp.sps' /"DEFINE !date()'"#cdate"'!ENDDEFINE.".
END IF.
EXECUTE.
* Run the macro.
INCLUDE 'c:\\temp\\temp.sps'.
SAVE OUTFILE='c:\\temp\\example1_' + !date + '.sav'.
答案1
得分: 1
你可以将计算后的#cdate
转换为日期变量(现在它是一个字符串),然后更改其格式,然后再转换为字符串以进行优化:
cd "C:\Users\eli\work\seker tool".
DATA LIST FREE /a.
BEGIN DATA
1 2 3
END DATA.
* 定义一个宏,用于获取当前日期。
string #dtt(a11).
compute #dtt=replace(concat("_", string(number($DATE, DATE9), SDATE10)), "/","-").
DO IF $casenum=1.
WRITE OUTFILE 'temp.sps' /"DEFINE !date()"#dtt"!ENDDEFINE.".
END IF.
EXECUTE.
* 运行宏。
INCLUDE 'temp.sps'.
* 现在返回到您的原始数据集并保存它。
dataset activate YourOriginalDataset.
SAVE OUTFILE='YourDataName' + !date + '.sav'.
现在,如果原始日期cdate
是"21-MAR-23",新日期dtt
将是"_2023-03-21",并将以这种方式显示在文件名中。
注意 - 在原始代码中,你在变量名称的开头使用#
,这有助于避免混杂的工作数据,但由于所有这些过程都发生在独立的临时数据集上,所以在这里没有必要使用它们 - 当你实际查看数据时,调试更容易。
英文:
Once you've computed #cdate
you can change it into a date variable (right now it's a string), then change it's format, then back to a string in order to optimize it:
cd "C:\Users\eli\work\seker tool".
DATA LIST FREE /a.
BEGIN DATA
1 2 3
END DATA.
* Define a macro which will yield the current date.
string #dtt(a11).
compute #dtt=replace(concat("_", string(number($DATE, DATE9), SDATE10)), "/","-").
DO IF $casenum=1.
WRITE OUTFILE 'temp.sps' /"DEFINE !date()'"#dtt"'!ENDDEFINE.".
END IF.
EXECUTE.
* Run the macro.
INCLUDE 'temp.sps'.
*now get back to your original dataset and save it.
dataset activate YourOriginalDataset.
SAVE OUTFILE='YourDataName' + !date + '.sav'.
Now if the original date cdate
was "21-MAR-23" the new one dtt
is "_2023-03-21" and will show like that in the file name.
NOTE - in the original code you use #
in the beginnig of variable names - that's nice to avoid cluttering your work data, but since all this procedure is happening on an independent temporary dataset, you'd do better without them - debugging is easier when you can actually see your data
答案2
得分: 1
这也可以通过在SPSS语法中运行一个简短的Python脚本来实现。
begin program python3.
import time, spss
spss.SetMacroValue(""!timestamp"", ""'" + time.strftime(""%Y-%m-%d"") + ""'"")
end program.
save outfile = ""C:\temp\Python_timestamp_2_"" + !timestamp + "".sav"".
英文:
This can also be done by running a short python script within SPSS syntax.
begin program python3.
import time, spss
spss.SetMacroValue("!timestamp", "'" + time.strftime("%Y-%m-%d") + "'")
end program.
save outfile = "C:\temp\Python_timestamp_2_" + !timestamp + ".sav".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论