SPSS 宏从变量打开 CSV
我是 SPSS 宏的新手,并尝试从变量中的指定路径打开 CSV 文件。
请注意,我想在文件顶部设置所有变量设置,因此我可以轻松决定导入另一个具有不同名称的文件。
这是我的代码:
/* settings */
define !SYMB() "VIX". !enddefine.
define !CSVFILE() "E:\Downloads\$" + !SYMB + ".csv". !enddefine.
/* import csv file */
GET DATA
/TYPE=TXT
/FILE=!CSVFILE
/DELCASE=LINE
/DELIMITERS=","
/ARRANGEMENT=DELIMITED
/FIRSTCASE=1
/IMPORTCASE=ALL
/VARIABLES=
V1 5X
Date ADATE10
!SYMB + 'O' F7.2
!SYMB + 'H' F7.2
!SYMB + 'L' F7.2
!SYMB + 'C' F7.2.
CACHE.
EXECUTE.
DATASET NAME "DataSet" + !SYMB WINDOW=FRONT.
不幸的是我收到以下错误:
/* 设置 */ 定义 !SYMB() "VIX"。 !enddefine。定义 !CSVFILE() “E:\Downloads\$” + !SYMB + “.csv”。 !enddefine。
<块引用>第 13 列中的警告#207。文本:E:\Downloads\$ 在文本字符串后面发现一个“+”,表示继续,但下一个 非空白字符不是引号或撇号。
/* 导入 csv 文件 */ GET DATA /TYPE=TXT /FILE=!CSVFILE
<块引用>错误。命令名称:获取数据 (2256) 无效子命令:FILE 该命令的执行停止。
错误#1。命令名称:+ 该行中的第一个单词未被识别为 SPSS Statistics 命令。 该命令的执行停止。 /DELCASE=LINE
错误#1。命令名称:/DELCASE 该行中的第一个单词未被识别为 SPSS Statistics 命令。 该命令的执行停止。 /DELIMITERS="," /ARRANGMENT=DELIMITED /FIRSTCASE=1 /IMPORTCASE=ALL /VARIABLES= V1 5X 日期 ADATE10 !SYMB + 'O' F7.2
错误#1。命令名称:+ 该行中的第一个单词未被识别为 SPSS Statistics 命令。 该命令的执行停止。 !SYMB + 'H' F7.2
错误#1。命令名称:+ 该行中的第一个单词未被识别为 SPSS Statistics 命令。 该命令的执行停止。 !SYMB + 'L' F7.2
错误#1。命令名称:+ 该行中的第一个单词未被识别为 SPSS Statistics 命令。 该命令的执行停止。 !SYMB + 'C' F7.2。
错误#1。命令名称:+ 该行中的第一个单词未被识别为 SPSS Statistics 命令。 该命令的执行停止。缓存。执行。
错误#105。命令名称:EXECUTE 在定义工作文件之前,此命令无效。 该命令的执行停止。数据集名称“DataSet”+ !SYMB WINDOW=FRONT。
我应该怎么做?
建议宏的输出:
/* settings */
define !SYMB() "VIX" !enddefine.
define !CSVFILE() !quo(!con("E:\Downloads\$", !unq(!eva(!SYMB)), ".csv")) !enddefine.
define !c(str1 = !tok(1) /str2 = !tok(1)) !con(!unq(str1), !unq(str2)) !end.
define !cq(str1 = !tok(1) /str2 = !tok(1)) !quo(!con(!unq(str1), !unq(str2))) !end.
/* import csv file */
GET DATA
/TYPE=TXT
/FILE=!CSVFILE /DELCASE=LINE
/DELIMITERS=","
/ARRANGEMENT=DELIMITED
/FIRSTCASE=1
/IMPORTCASE=ALL
/VARIABLES=
V1 5X
Date ADATE10
!c !str1=!SYMB str2="O" F7.2
>Warning # 210 in column 2. Text: !str1
>A macro symbol is invalid in this context.
>The symbol will be treated as an invalid special character.
!c !str1=!SYMB str2="H" F7.2
>Warning # 210 in column 2. Text: !str1
>A macro symbol is invalid in this context.
>The symbol will be treated as an invalid special character.
!c !str1=!SYMB str2="L" F7.2
>Warning # 210 in column 2. Text: !str1
>A macro symbol is invalid in this context.
>The symbol will be treated as an invalid special character.
!c !str1=!SYMB str2="C" F7.2.
>Warning # 210 in column 2. Text: !str1
>A macro symbol is invalid in this context.
>The symbol will be treated as an invalid special character.
>Error. Command name: GET DATA
>(2265) Unrecognized or invalid variable format. The format is invalid. For
>numeric formats, the width or decimals value may be invalid.
>Execution of this command stops.
CACHE.
EXECUTE.
>Error # 105. Command name: EXECUTE
>This command is not valid before a working file has been defined.
>Execution of this command stops.
DATASET NAME !cq !str1="DataSet" !str2=!SYMB WINDOW=FRONT.
测试输出:
set err=off.
set mpr=on.
set printback=on.
11 0 M> set printback=on.
12 0 M>
!c str1="A" str2="B".
13 0 M> !c str1="A" str2="B".
!cq str1="A" str2="B".
14 0 M> !cq str1="A" str2="B".
15 0 M>
set printback=off.
16 0 M> set printback=off.
I am new to SPSS macros and trying to open a CSV file from a specified path in a variable.
Note that I want to set all variable settings at the top of the file, so I can easily decide to import another file with a different name.
This is my code:
/* settings */
define !SYMB() "VIX". !enddefine.
define !CSVFILE() "E:\Downloads\$" + !SYMB + ".csv". !enddefine.
/* import csv file */
GET DATA
/TYPE=TXT
/FILE=!CSVFILE
/DELCASE=LINE
/DELIMITERS=","
/ARRANGEMENT=DELIMITED
/FIRSTCASE=1
/IMPORTCASE=ALL
/VARIABLES=
V1 5X
Date ADATE10
!SYMB + 'O' F7.2
!SYMB + 'H' F7.2
!SYMB + 'L' F7.2
!SYMB + 'C' F7.2.
CACHE.
EXECUTE.
DATASET NAME "DataSet" + !SYMB WINDOW=FRONT.
Unfortunately I am getting the following errors:
/* settings */ define !SYMB() "VIX". !enddefine. define !CSVFILE()
"E:\Downloads\$" + !SYMB + ".csv". !enddefine.Warning # 207 in column 13. Text: E:\Downloads\$
A '+' was found following a text string, indicating continuation, but the next
non-blank character was not a quotation mark or an apostrophe./* import csv file */ GET DATA /TYPE=TXT /FILE=!CSVFILE
Error. Command name: GET DATA
(2256) Invalid subcommand: FILE
Execution of this command stops.Error # 1. Command name: +
The first word in the line is not recognized as an SPSS Statistics command.
Execution of this command stops. /DELCASE=LINEError # 1. Command name: /DELCASE
The first word in the line is not recognized as an SPSS Statistics command.
Execution of this command stops. /DELIMITERS="," /ARRANGEMENT=DELIMITED /FIRSTCASE=1 /IMPORTCASE=ALL /VARIABLES=
V1 5X Date ADATE10 !SYMB + 'O' F7.2Error # 1. Command name: +
The first word in the line is not recognized as an SPSS Statistics command.
Execution of this command stops. !SYMB + 'H' F7.2Error # 1. Command name: +
The first word in the line is not recognized as an SPSS Statistics command.
Execution of this command stops. !SYMB + 'L' F7.2Error # 1. Command name: +
The first word in the line is not recognized as an SPSS Statistics command.
Execution of this command stops. !SYMB + 'C' F7.2.Error # 1. Command name: +
The first word in the line is not recognized as an SPSS Statistics command.
Execution of this command stops. CACHE. EXECUTE.Error # 105. Command name: EXECUTE
This command is not valid before a working file has been defined.
Execution of this command stops. DATASET NAME "DataSet" + !SYMB WINDOW=FRONT.
How should I be doing this?
Output on suggested macro:
/* settings */
define !SYMB() "VIX" !enddefine.
define !CSVFILE() !quo(!con("E:\Downloads\$", !unq(!eva(!SYMB)), ".csv")) !enddefine.
define !c(str1 = !tok(1) /str2 = !tok(1)) !con(!unq(str1), !unq(str2)) !end.
define !cq(str1 = !tok(1) /str2 = !tok(1)) !quo(!con(!unq(str1), !unq(str2))) !end.
/* import csv file */
GET DATA
/TYPE=TXT
/FILE=!CSVFILE /DELCASE=LINE
/DELIMITERS=","
/ARRANGEMENT=DELIMITED
/FIRSTCASE=1
/IMPORTCASE=ALL
/VARIABLES=
V1 5X
Date ADATE10
!c !str1=!SYMB str2="O" F7.2
>Warning # 210 in column 2. Text: !str1
>A macro symbol is invalid in this context.
>The symbol will be treated as an invalid special character.
!c !str1=!SYMB str2="H" F7.2
>Warning # 210 in column 2. Text: !str1
>A macro symbol is invalid in this context.
>The symbol will be treated as an invalid special character.
!c !str1=!SYMB str2="L" F7.2
>Warning # 210 in column 2. Text: !str1
>A macro symbol is invalid in this context.
>The symbol will be treated as an invalid special character.
!c !str1=!SYMB str2="C" F7.2.
>Warning # 210 in column 2. Text: !str1
>A macro symbol is invalid in this context.
>The symbol will be treated as an invalid special character.
>Error. Command name: GET DATA
>(2265) Unrecognized or invalid variable format. The format is invalid. For
>numeric formats, the width or decimals value may be invalid.
>Execution of this command stops.
CACHE.
EXECUTE.
>Error # 105. Command name: EXECUTE
>This command is not valid before a working file has been defined.
>Execution of this command stops.
DATASET NAME !cq !str1="DataSet" !str2=!SYMB WINDOW=FRONT.
Test output:
set err=off.
set mpr=on.
set printback=on.
11 0 M> set printback=on.
12 0 M>
!c str1="A" str2="B".
13 0 M> !c str1="A" str2="B".
!cq str1="A" str2="B".
14 0 M> !cq str1="A" str2="B".
15 0 M>
set printback=off.
16 0 M> set printback=off.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SPSS 宏语言很棘手。
1)你必须在宏内部进行字符串连接;
2) 避免在宏命令中使用点——如果没有必要的话;
3) 不要用宏命令结束行。
尝试这个未经测试的语法:
您能否运行这个测试语法并将您在输出中得到的结果发布出来。
SPSS macro language is tricky.
1) You have to do concatenation of strings inside macro;
2) Avoid dots in macro commands - if they are not necessary;
3) Do not end the line with macro command.
Try this untested syntax:
Could you please run this test syntax and post the result you get in the Output.
宏确实很棘手。出于您的目的,我建议您使用更简单的 FILE HANDLE 命令来定义您的输入。您可以在“帮助”菜单访问的“语法参考”手册中阅读相关内容。
哈特哈,
乔恩·佩克
Macros are indeed tricky. For your purposes, I suggest that you use the simpler FILE HANDLE command to define your inputs. You can read about it in the Syntax Reference manual accessible from the Help menu.
HTH,
Jon Peck