kaltzy1771
kaltzy1771

Reputation: 21

SPSS compute new variables with if statements if same field with multiple IDs in same field

I need to compute a new variable in SPSS that reads "1" if an ID has a score in the "Fall" and in the "Spring". Below is an examples of the data. I would like the output for ID #14576=0 since it only has ratings for the Spring, but ID #10869=1 since it has ratings in the spring and the fall. The timePeriod variable is in "string" format so I tried the formula below but it just provides "0" next to all the IDs. Do you know what I should do?

compute bothtimeperiods=0.
if (timeperiod="Fall" and timeperiod="Spring") bothtimeperiods=1.

The data is:

ID      rating  timePeriod
14576   3.0     Spring
14576   2.0     Spring
14576   5.0     Spring
14576   4.0     Spring
10869   1.0     Spring
10869   2.0     Spring
10869   4.0     Spring
10869   4.0     Spring
10869   1.0     Fall
10869   5.0     Fall
10869   5.0     Fall
10869   2.0     Fall

Upvotes: 2

Views: 618

Answers (1)

Andy W
Andy W

Reputation: 5089

One way is to compute two dummy variables signifying spring or fall, and then use AGGREGATE to say if any occur within the persons Id. This gives you the info. you need then to check. Example below.

DATA LIST FREE / ID  (F5.0) rating  (F3.1) timePeriod (A6).
BEGIN DATA
14576   3.0     Spring
14576   2.0     Spring
14576   5.0     Spring
14576   4.0     Spring
10869   1.0     Spring
10869   2.0     Spring
10869   4.0     Spring
10869   4.0     Spring
10869   1.0     Fall
10869   5.0     Fall
10869   5.0     Fall
10869   2.0     Fall
11111   3.0     Spring
22222   4.0     Fall
END DATA.

COMPUTE AnySpring = (timePeriod = "Spring").
COMPUTE AnyFall = (timePeriod = "Fall").
AGGREGATE OUTFILE=* MODE=ADDVARIABLES OVERWRITE=YES
  /BREAK ID
  /AnySpring AnyFall = MAX(AnySpring AnyFall).

COMPUTE bothTimeperiods = (AnySpring = 1) AND (AnyFall = 1).
EXE.

Upvotes: 1

Related Questions