4

Im looking for a way to loop through variables (eg week01 to week52) and count the number of times the value changes across the them. For example week01 to week18 may be coded as 1 week19 to week40 may be coded as 4 and week 41 to 52 may be coded as 3

That would be 2 transistions within the data.

How could i go about writing a code that can find me this information? I'm rather new to this and some help to get me in the right direction would be very appreciated.

1 Answers1

4

You can use the DO REPEAT command to loop through variable lists. Below is an example of using this command to create a before date and after date to compare, and increment a count variable whenever these two variables are different.

data list fixed / observation (A1).
begin data
1
2
3
4
5
end data.

*making random data.
vector week(52).
do repeat week = week1 to week52.
compute week = RND(RV.UNIFORM(0.5,4.4)).
end repeat.
execute.

*initialize count to zero.
compute count = 0.

do repeat week_after = week2 to week52 / week_before = week1 to week51.
if week_after <> week_before count = count + 1.
end repeat.
execute.
Andy W
  • 5,031
  • 3
  • 25
  • 51