Selecting cases based on values in previous cases - spss

I've got the following list in SPSS:
Subjekt Reactiontime correct/incorrect
1 x 1
1 x 0
1 x 1
1 x 0
I now want to select all rows/cases that follow AFTER "0" (in the column correct/incorrect) because I want to compute the mean of all reactiontimes that come after "0".
How can I do that in SPSS?

One way to do this would be to add a column that keeps track of whether the prior row was equal to 0 in your correct field and then calculate the mean Reactiontime of those cases.
First let's make a variable to flag cases we want included in the average.
* set prev_correct to 0 if the prior case was 0 .
IF (LAG(correct)=0) prev_correct=0 .
* else set to -1 .
RECODE prev_correct (SYSMIS=-1) .
EXE .
Now we can calculate the mean reaction time, splitting by our new variable.
MEANS Reactiontime BY prev_correct /CELLS MEAN .
Or, if we only want to output the mean when prev_correct=0 .
TEMP .
SELECT IF prev_correct=0 .
MEANS Reactiontime /CELLS MEAN .

Here's a shorter approach (though less generic than #user45392's full process):
if lag(correct)=0 ReactiontimeAfter0=Reactiontime.
now you can just run means ReactiontimeAfter0.

Related

How to blank all cases with a specific year in SPSS?

I have 3 datasets each from a particular year. I have already merge all 3 but I want to blank cases where year=2016. So far this is the syntax I came up with:
Do (if subyr=2016).
Recode X1 to X32 (Lowest to Highest=SYMIS)(Else=SYMIS).
End if.
You should be able to simply use
DO IF (subyr=2016) .
RECODE X1 TO X32 (ELSE=SYSMIS) .
END IF .
EXE .
If you ever wanted to code the valid values differently from the SYSMIS values, you could use
DO IF (subyr=2016) .
RECODE X1 TO X32 (LO THRU HI=0)(ELSE=SYSMIS) .
END IF .
EXE .
which would give you that flexibility. This example sets valid values to 0 and keep SYSMIS values as SYSMIS.

How to copy from one variable to another on SPSS

I have a data set with two parent/carer respondents (main and partner) for each participant (child). For one of the variables, only one respondent has given an answer - usually the main respondent, but in some cases it was the partner respondent. I therefore need to fill in some missing main respondent data with data from the partner respondent.
My data looks roughly like this:
MAIN PARTNER I would like the final var as below:
2 -1 2
1 -1 1
-1 2 2
1 . 1
-9 2 2
-8 1 1
2 . 2
1 . 1
etc.
(-1, -8 and -9 are missing values)
All variables are numeric. Where a response is missing from the main respondent, I would like to fill it in from the partner. I cannot seem to get the DO IF/RECODE commands to work.
Any advice on how to do this in SPSS would be hugely appreciated!
More than one way to skin a cat. Depending on your taste, you might create your final variable responder like so:
MISSING VALUES main (-1,-8,-9) .
IF (MISSING(main)) responder=partner .
IF (NOT(MISSING(main))) responder=main .
EXE .
First assign your missing values. Then assign a value to responder based on whether main is missing. Note that MISSING(main) will evaluate true when main has a specified missing value (in this case: -1, -8, or -9) or a system missing value.

Finding the total from a single dimensional array - COBOL

I need to iterate through a 1D array and add all of the elements together to find the total. I must use a Perfrom ... Varying statement, this is what I have come up with so far.
perform 100-read-input-file
varying emp-rec-calls(ws-emp-total)
from 1 by ws-emp-total
until (ws-eof-flag = 'Y'
OR ws-array-counter > ws-array-max)
add emp-rec-calls(ws-emp-total) to ws-total-temp
The code for 100-read-input-file is simply
read input-file at end move 'y' to found-eof.
The problem I am currently getting is "Subscript out of range:" on this line "perform 100-read-input-file". All help is appretiated, thanks!
Let's analyze the code you provided:
perform 100-read-input-file
varying emp-rec-calls(ws-emp-total)
from 1 by ws-emp-total
until (ws-eof-flag = 'Y'
OR ws-array-counter > ws-array-max)
add emp-rec-calls(ws-emp-total) to ws-total-temp
This loop doesn't really make any sense. You are saying perform this loop varying occurance X of the array EMP-REC-CALLS from 1 by X until a flag that never gets set within the loop is equal to yes OR a counter you are not incrementing is greater than the array size.
I think you are trying to achieve something like this:
PERFORM VARYING WS-ARRAY-COUNTER
FROM 1 BY 1
UNTIL WS-ARRAY-COUNTER > WS-ARRAY-MAX
ADD EMP-REC-CALLS(WS-COUNTER) TO WS-TOTAL-TEMP
END-PERFORM
This will vary the counter WS-ARRAY-COUNTER by 1 every iteration of the loop (starting at 1) until that counter is greater than the max defined.

List still being treated as a set even after converting

So i have an instance where even after converting my sets to lists, they aren't recognized as lists.
So the idea is to delete extra columns from a data frame comparing with columns in another. I have two data frames say df_test and df_train . I need to remove columns in df_test which are not in train .
extracols = set(df_test.columns) - set(df_train.columns) #Gives cols 2b
deltd
l = [extracols] # or list(extracols)
Xdp.dropna( subset = l, how ='any' , axis = 0)
I get an error : Unhashable type set
Even on printing l it prints like a set with {} curlies.
[{set}] doesn't cast to list, it just creates a list of length 1 with your set inside it.
Are you sure that list({set}) isn't working for you? Maybe you should post more of your code as it is hard to see where this is going wrong for you.

How can I calculate the number of cases in SPSS?

I would like to calculate the last column with SPSS without turning the columns into 1.
You can use the COUNT command to do this, small example below.
DATA LIST FREE / Tool1 Tool2 Tool3.
BEGIN DATA
1 3 8
1 5 1
1 . .
2 3 .
3 . .
END DATA.
COUNT Tool# = Tool1 TO Tool3 (LOWEST THRU HIGHEST).
EXECUTE.

Resources