Search code examples
syntaxspss

Syntax to generate a Syntax in SPSS


I’m trying to construct a Syntax to generate a Syntax in SPSS, but I’m having some issues… I have an excel file with metadata and I would like to use it in order to make a syntax to extract information from it (like this, if I have a huge database, I just need to keep the excel updated – add/delete variables, etc. - and then run a syntax to extract the needed information for a new syntax). I also noticed the produced syntax has always around 15Mb, which is a lot (applied to more than 500 lines)! I don’t use Python due to run syntax in different computers and/or configurations. Any ideas? Can anyone please help me? Thank you in advance.

Example:
(test.xlsx – sheet 1)
Var Code Label List Var_label (concatenate Var+Label)
V1 3 Sex 1 V1 “Sex”
V2 1 Work 2 V2 “Work”
V3 3 Country 3 V3 “Country”
V4 1 Married 2 V4 “Married”
V5 1 Kids 2 V5 “Kids”
V6 2 Satisf1 4 V6 “Satisf1”
V7 2 Satisf2 4 V7 “Satisf2”

(information from other file)
List = 1
1 “Male”
2 “Female”
List = 2
1 “Yes”
2 “No”
List = 3
1 “Europe”
2 “America”
3 “Asia”
4 “Africa”
5 “Oceania”
List = 4
1 “Very unsatisfied”
10 “Very satisfied”

I want to make a Syntax that generates a new syntax to apply “VARIABLE LABELS” and “VALUE LABELS”. So, I thought about something like this:

GET DATA
/TYPE=XLSX
/FILE="test.xlsx"
/SHEET=name 'sheet 1'
/CELLRANGE=FULL
/READNAMES=ON
/DATATYPEMIN PERCENTAGE=95.0.
EXECUTE.

STRING vlb (A15) labels (A150) value (A12) lab (A1500) point (A2) separate (A50) space (A2) list1 (A100) list2 (A100).

SELECT IF (Code=1).
COMPUTE vlb = "VARIABLE LABELS".
COMPUTE labels = CONCAT (RTRIM(Var_label)," ").
COMPUTE point = ".".
COMPUTE value = "VALUE LABELS".
COMPUTE lab = CONCAT (RTRIM(Var)," ").
COMPUTE list1 = '1 " Yes "'.
COMPUTE list2 = '2 "No".'.
COMPUTE space = " ".
COMPUTE separate="************************************************.".

WRITE OUTFILE = "list_01.sps" / vlb.
WRITE OUTFILE = "list_01.sps" /labels.
WRITE OUTFILE = "list_01.sps" /point.
WRITE OUTFILE = "list_01.sps" /value.
WRITE OUTFILE = "list_01.sps" /lab.
WRITE OUTFILE = "list_01.sps" /list1.
WRITE OUTFILE = "list_01.sps" /list2.
WRITE OUTFILE = "list_01.sps" /space.
WRITE OUTFILE = "list_01.sps" /separate.
WRITE OUTFILE = "list_01.sps" /space.

If there is only one variable with same list (ex: V1), it works ok. However, if there is more than one variable having the same list, it reproduces the codes as much times as number of variables (Ex: V2, V4 and V5).

What I have (Ex: V2, V4 and V5), after running code above:

VARIABLE LABELS
V2 "Work"
.
VALUE LABELS
V2
1 " Yes "
2 " No "
************************************************.
VARIABLE LABELS
V4 "Married"
.
VALUE LABELS
V4
1 " Yes "
2 " No "
************************************************.
VARIABLE LABELS
V5 "Kids"
.
VALUE LABELS
V5
1 " Yes "
2 " No "
************************************************.

What I would like to have:

VARIABLE LABELS
V2 "Work"
V4 "Married"
V5 "Kids"
.
VALUE LABELS
V2 V4 V5
1 " Yes "
2 " No "


Solution

  • I think there are probably ways to automate the whole process better, including the use of your second data source. But for the scope of this question I will suggest a way to get what you asked for specifically.

    The key is to build the command with special conditions for first and last lines:

    string cmd1 cmd2 (a200).
    sort cases by code.
    match files /file=* /first=first /last=last /by code. /* marking first and last lines.
    do if first.
        compute cmd1="VARIABLE LABELS".
        compute cmd2="VALUE LABELS".
    end if.
    if not first cmd1=concat(rtrim(cmd1), " /"). /* "/" only appears from the second varname.
    compute cmd1=concat(rtrim(cmd1), " ", Var_label).
    compute cmd2=concat(rtrim(cmd2), " ", Var).
    do if last.
        compute cmd1=concat(rtrim(cmd1), " .").
        compute cmd2=concat(rtrim(cmd2), " ", '  1 " Yes "  2 "No". ').
    end if.
    exe.
    

    The commands are now ready, but we don't want to get them mixed up so we'll stack them one under the other, and only then write them out:

    add files /file=* /rename cmd1=cmd /file=* /rename cmd2=cmd.
    exe.
    WRITE OUTFILE = "var definitions.sps" / cmd .
    exe.
    

    EDIT: Note that the code above assumes you've already run a select cases if code = ... and that there is a single code in all the remaining lines.
    Note also I added an exe. command at the end - without running that the new syntax will appear empty.