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=LINE
Error # 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.2
Error # 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.2
Error # 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.2
Error # 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.