## recoding a sequence in SPSS

Moderators: statman, Analyst Techy, andris, Fierce, GerineL, Smash

blue123rd
Posts: 4
Joined: Tue Mar 15, 2016 5:13 pm

### recoding a sequence in SPSS

I have a sequence of numbers: 10001 01
10002 01
10003 01
10004 01
10001 02
10002 02
10003 02
10004 02
10001 03
10002 03
10003 03
10004 03
10001 04

I have the field with the 10001 in it but I want to create another field which I will concatenate later to include 01 for the first 4, then 02 for the second for and so on.

Thanks.
GerineL
Moderator
Posts: 1477
Joined: Tue Jun 10, 2008 4:50 pm

### Re: recoding a sequence in SPSS

use the concat function.
you may have to convert to string first.
blue123rd
Posts: 4
Joined: Tue Mar 15, 2016 5:13 pm

### Re: recoding a sequence in SPSS

The concat function would work great after I can create the 01 after the 10001. My issue is knowing how to create the 01 and then the 02 and so on after every set of 4 numbers 10001 to 10004. The LAG function doesn't seem to be an option.
10001 01
10002 01
10003 01
10004 01
10001 02
10002 02
10003 02
10004 02
10001 03
GerineL
Moderator
Posts: 1477
Joined: Tue Jun 10, 2008 4:50 pm

### Re: recoding a sequence in SPSS

so 2things:

Spss leaves the 0 out, so that is not possible with a numeric variable.

Second, is this every situation that happens? so you always have repetition of these same numbers 10001 10002 10003 10004?
blue123rd
Posts: 4
Joined: Tue Mar 15, 2016 5:13 pm

### Re: recoding a sequence in SPSS

Yes the 10001 to 10004 (it is actually 10001 to 10007 but I used 4 to make it shorter) represents a week. Every week is represented by the same 10001 to 10007 so what I want to do is somehow recode or add another field next to it so that the first 100001 to 10007 is 01 in the next field with the next 10001 to 10007 having 02 in the field next to it and so on. Can Spss do a loop of some sort to accomplish this? Please help. the 01 could be 1.
GerineL
Moderator
Posts: 1477
Joined: Tue Jun 10, 2008 4:50 pm

### Re: recoding a sequence in SPSS

Do you also have a variable indicating "time" or something?
Do you always have 10001 thru 10007 or are they sometimes missing?
How do you know which one should be 01 and so forth?

Depending on this, you ma for instance do something with sorting on ID and then use a lag combined with (if Id = ID+1) to do previous + 1.
blue123rd
Posts: 4
Joined: Tue Mar 15, 2016 5:13 pm

### Re: recoding a sequence in SPSS

I have solved my first issue with this line of code.
COMPUTE #X = #X + 1.
IF SUFFIX NE 1 #X = LAG(#X).
COMPUTE X = #X.
EXECUTE.
suffix and x are the last 2 numbers to right in that order.
18001 01 1.00 1.00
18002 02 2.00 1.00
18003 03 3.00 1.00
18004 04 4.00 1.00
18001 01 1.00 2.00
18002 02 2.00 2.00
18003 03 3.00 2.00
18004 04 4.00 2.00
18001 01 1.00 3.00
18002 02 2.00 3.00
18003 03 3.00 3.00
18004 04 4.00 3.00

when the suffix goes back to 1, I need the X to become 2 like in Y.

suffix and x and y are the last 3 numbers to right in that order.
18001 01 1.00 1.00 1.00
18001 01 1.00 2.00 1.00
18001 01 1.00 3.00 1.00
18001 01 1.00 4.00 1.00
18002 02 2.00 4.00 1.00
18002 02 2.00 4.00 1.00
18002 02 2.00 4.00 1.00
18002 02 2.00 4.00 1.00
18003 03 3.00 4.00 1.00
18003 03 3.00 4.00 1.00
18001 01 1.00 5.00 2.00
18001 01 1.00 6.00 2.00
18001 01 1.00 7.00 2.00
18001 01 1.00 8.00 2.00
18002 02 2.00 8.00 2.00
18002 02 2.00 8.00 2.00
18002 02 2.00 8.00 2.00
18002 02 2.00 8.00 2.00
18003 03 3.00 8.00 2.00
18003 03 3.00 8.00 2.00

### Who is online

Users browsing this forum: No registered users and 1 guest