How do I combine multiple dummy variables into one variable in SPSS? - variable-assignment

I have multiple dummy variables and I want to combine them into one variable. For ex. if variable A=1, then it is category 1, if variable B=1, then it is category 2, if variable C=1 it is category 3 etc. How do I do this on SPSS?

Here are a couple of ways to do it, both assuming there are never cases where more then one of the variables A/B/C withe value 1:
The standard way, using a loop:
do repeat vr=A B C/vl=1 2 3.
if vr=1 category=vl.
end repeat.
If the variables used here are consecutive in the file you can write the first line this way:
do repeat vr=A to G/vl=1 to 7.
And this is a different approach to the problem:
compute category=sum(1*A, 2*B, 3*C).

Related

How to find max&min of all variables with SPSS and display in table?

I have a table with about 500 variables and 2000 cases. The type of these variables varies. My supervisor has asked me to produce a table listing all the numeric variables, along with their maximums and minimums. I am supposed to use SPSS because R apparently messes up the value labels.
I've only done very basic things in SPSS before this, like finding statistics for single variables, and I'm not sure how to do this. I think I should probably do something like:
*Create new table*
DATASET DECLARE maxAndMin.
*Loop through all variables: Use conditional statement to identify numeric variables*
DO REPEAT R=var1 TO varN.
FREQUENCIES VARIABLES /STATISTICS=MINIMUM
END REPEAT
*Find max and minimum*
I'm not sure how to go about this though. Any suggestions would be appreciated.
The following code will first make a list of all numeric variables in the dataset (and store it in a macro called !nums) and then it will run an analysis of those variables to tell you the mean, maximum and minimum of each:
SPSSINC SELECT VARIABLES MACRONAME="!nums" /PROPERTIES TYPE= NUMERIC.
DESCRIPTIVES !nums /STATISTICS=MEAN MIN MAX.
You can use the following code to create a tiny dataset to test the above code on:
data list list/n1 (f1) t1(a1) n2(f1) t2(a1).
begin data
1 "a" 34 "b"
2 "a" 23 "b"
3 "a" 52 "b"
4 "a" 71 "b"
end data.
If SUMMARIZE produces a nice enough table for you, here is a "non-extension" way of doing it.
file handle mydata /name="<whatever/wherever>".
data list free /x (f1) y (a5) z (F4.2).
begin data.
1 yes 45.67
2 no 32.00
3 maybe .
4 yes 22.02
5 no 12.79
end data.
oms select tables
/destination format=sav outfile=mydata
/if subtypes="Descriptive Statistics" /tag="x".
des var all.
omsend tag="x".
get file mydata.
summarize Var1 Mean Minimum Maximum /format list nocasenum nototal
/cells none /statistics none /title "Numeric Variables Only".
or use a DATASET command instead of file handle if you don't need the file on disk.

Creating a list of all variables except some specific ones

I have multiple spss file having multiple number of variables(col1,col2,...col150).I am trying to create a common code for restructure the file using VARSTOCVASES. in this i need to KEEP 3 variables(col1,col34,col66)these are common in all files but the rest variables are different.I know the normal way in that we will add all the remaining variables in to MAKE sub command. that i am adding bellow
VARSTOCVASES
/MAKE VariableName1 FROM Col1 Col2 Col3 ....etc(except 3)
/INDEX=VariableName(VariableName1)
/KEEP=Col1 Col34 Col66
instead of this i want to create some variable list using the (SPSSINC SELECT VARIABLES) command.I got this idea but i don't have any examples for the same.This Select query must be small which means this query should dynamically select all the variables except these 3(Col1 Col34 Col66)because i have different SPSS files and in that these 3(Col1 Col34 Col66) variables are same but the rest are different and all containing different number of variables.
IF i have a variable list(dynamically generated by excluding the 3) then i can point that in MAKE sub Command.Please any one help me.
one way to go about this could be to rename these specific columns and then select all other variables that start with "col":
rename variables (col1 col34 col66=var1 var34 var66).
spssinc select variables MACRONAME = "!allCOL"
/PROPERTIES PATTERN="Col*".
Now all variables with names starting with "Col" are in the list called "!allCOL" which you can use in your syntax, for example:
VARSTOCVASES
/MAKE VariableName1 FROM !allCOL /INDEX=VariableName(VariableName1) .
EDIT: another solution
The solution above is valid only if there is a constant pattern to all the variables you want on the list. If that is not the case, this following solution enables you to name the variables that you don't want on the list, and put all the rest on the list.
* first we define a new attribute in which we mark the
variables we don't want on the list.
VARIABLE ATTRIBUTE VARIABLES=Car_Model_1 Car_Model_2
ATTRIBUTE=IncludeInMake ("no").
* now we create the list, leaving out the unwanted variables.
spssinc select variables MACRONAME = "!forMake"
/ATTRVALUES NAME=IncludeInMake VALUE="".
VARSTOCVASES /MAKE Val FROM !forMake /INDEX=var(val) .

How can I check the measure (nominal/ordinal/scale) of a variable using syntax?

I would like to find the measure of a variable using syntax and then use this in an If-statement. Is this possible using syntax?
For example, if I have two variables a (nominal) and b (ordinal):
DO IF (a is nominal?)
...
END IF
You can create a list of all the nominal variables in your data. In the following example the list will be stored under the macro call !noms:
SPSSINC SELECT VARIABLES MACRONAME="!noms" /PROPERTIES LEVEL=NOMINAL.
* now, for example you can run frequencies on all nominal variables.
freq !noms.
If you want to transform all the nominal variables you can use do repeat. For example:
do repeat NomVrs=!noms.
recode NomVrs ("cat2"="persian").
end repeat.
If you want to test only one specific variable (in this example called AmInominal), you can use a macro this way:
define DoIfNom ()
!do !vr !in (!eval(!noms))
!if (!vr="AmInominal") !then
variable label AmInominal "this variable is indeed nominal".
recode AmInominal ("cat2"="persian").
frequencies AmInominal.
!ifend
!doend
!enddefine.
DoIfNom.

Looping through a set of variables sharing the same prefix

I routinely work with student exam files, where each response to an exam item is recorded in points. I want to transform that variable into 1 or 0 (effectively reducing each item to Correct or Incorrect).
Every dataset has the same nomenclature, where the variable is prefixed with points_ and then followed by an identification number (e.g., points_18616). I'm using the following syntax:
RECODE points_18616 (0=Copy) (SYSMIS=SYSMIS) (ELSE=1) INTO Binary_18616.
VARIABLE LABELS Binary_18616 'Binary Conversion of Item_18616'.
EXECUTE.
So I end up creating this syntax for each variable, and since every dataset is different, it gets tedious. Is there a way to loop through a dataset and perform this transformation on all variables that are prefixed with points_?
Here is a way to do it:
First I'll create a little fake data to demonstrate on:
data list list/points_18616 points_18617 points_18618 points_18619 (4f2).
begin data
4 5 6 7
5 6 7 8
6 7 8 9
7 8 9 9
end data.
* the following code will create a list of all the relevant variables in a new file.
SPSSINC SELECT VARIABLES MACRONAME="!list" /PROPERTIES PATTERN = "points_*".
* now we'll use the created list in a macro to loop your syntax over all the vars.
define !doList ()
!do !lst !in(!eval(!list))
RECODE !lst (0=Copy) (SYSMIS=SYSMIS) (ELSE=1) INTO !concat("Binary", !substr(!lst,7)).
VARIABLE LABELS !concat("Binary", !substr(!lst,7)) !concat("'Binary Conversion of Item",!substr(!lst,7) ,"'.").
!doend
!enddefine.
!doList.
EXECUTE.

Conditional processing in SPSS

I would like to conditionally process blocks of syntax where the condition is based on the active data set.
Within an SPSS macro, you can conditionally process a block of syntax using the !IF/!IFEND macro command. However, as far as I can tell, the user is required to explicitly give a value to the flag by either using the !LET command (!LET !FLAG = 1), or by using a Macro input variable. This is wildly different from my experience with other languages, where I can write code that has branching logic based on the data I'm working with.
Say that there is a block of syntax that I only want to run if there are at least 2 records in the active data set. I can create a variable in the data set which is equal to the number of records using the AGGREGATE function, but I can't find a way to make a macro variable equal to that value in a way that is usable as a !IF condition. Below is a very simple version of what I'd like to do.
COMPUTE DUMMY=1.
AGGREGATE
/OUTFILE = * MODE = ADDVARIABLES
/BREAK DUMMY
/NUMBER_OF_CASES = N.
!LET !N_CASES = NUMBER_OF_CASES.
!IF (!N_CASES > 1) !THEN
MEANS TABLES = VAR1 VAR2 VAR3.
!IFEND
Is what I'm attempting possible? Thanks in advance for your time and consideration.
Following is a way to put a value from the dataset into a macro, which you can then use wherever you need - including in another macro.
First we'll make a little dataset to recreate your example:
data list free/var1 var2 var3.
begin data
1 1 1 2 2 2 3 3 3
end data.
* this will create the number of cases value:
AGGREGATE /OUTFILE = * MODE = ADDVARIABLES /BREAK /NUMBER_OF_CASES = N.
Now we can send the value into a macro - by writing a separate syntax file with the macro definition.
do if $casenum=1.
write out='SomePath\N_CASES.sps' /"define !N_CASES() ", NUMBER_OF_CASES, " !enddefine.".
end if.
exe.
insert file='SomePath\N_CASES.sps'.
The macro is now defined and you can use the value in calculations (e.g if you want to use it for analysis of a different dataset, or later in your syntax when the current data is not available).
for example:
compute just_checking= !N_CASES .
You can also use it in your macro as in your example - you'll see that the new macro can't read the !N_CASES macro as is, that's why you need the !eval() function:
define !cond_means ()
!IF (!eval(!N_CASES) > 1) !THEN
MEANS TABLES = VAR1 VAR2 VAR3.
!IFEND
!enddefine.
Now running the macro will produce nothing if there is just one line in your data, and will run means if there was more than one line:
!cond_means.