converting random string to a number

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

converting random string to a number

leah rubin
Hi,

Can someone help me with the necessary syntax to convert 901-24 to the number 1, 902-24 to the number 2, etc?
TIA,
Leah
Reply | Threaded
Open this post in threaded view
|

Re: converting random string to a number

Art Kendall
Open a new instance of SPSS.  Copy the syntax below to a syntax file.
Click <run>. Click <all>.
Is this what you are looking for?



data list list/ mystring (a6).
begin data
901-24
902-24
ab3-xx
--5mmm
end data.
numeric mynumber (f1).
compute mynumber= number(substr(mystring,3,1),F1).
LIST .


Art Kendall
Social Research Consultants

leah rubin wrote:
> Hi,
>
> Can someone help me with the necessary syntax to convert 901-24 to the
> number 1, 902-24 to the number 2, etc?
> TIA,
> Leah

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: converting random string to a number

Art Kendall-2
If you do not need the very specific recode, you might consider the autorecode.

Open a new instance of SPSS.  Copy the syntax below to a syntax file. Click <run>. Click <all>.
Is this what you are looking for?



data list list/ image_v1 (a6).
begin data
901-24                
902-24                
903-24                
9                     
11                    
19                    
25                    
31                    
34                    
39                    
48                    
51                    
54                    
58                    
76                    
77                    
80                    
84                    
86                    
88                    
89                    
92                    
93                    
96                    
98                    
100                   
108                   
113                   
119                   
122                   
125                   
137                   
141                   
150                   
153                   
161                   
166                   
168                   
171                   
end data.
numeric IMAGE_V1_R (f2).
RECODE IMAGE_V1 ('901-24' = 0)('902-24' = 1)('903-24' = 2)('9' = 3)('11' = 4)('19' = 5)
               ('25' = 6)('31' = 7)('34' = 8)('39' = 9)('48' = 10)('51' = 11)('54' = 12)('58' = 13)('76' = 14)
               ('77' = 15)('80' = 16)('84' = 17)('86' = 18)('88' = 19)('89' = 20)('92' = 21)('93' = 22)('96' = 23)
               ('98' = 24)('100' = 25)('108' = 26)('113' = 27)('119' = 28)('122' = 29)('125' = 30)('137' = 31)
               ('141' = 32)('150' = 33)('153' = 34)('161' = 35)('166' = 35)('168' = 36)('171' = 37)('173' = 38) INTO IMAGE_V1_R.
autorecode variables = image_v1 /into image_v1A /print.
LIST .

Art Kendall
Social Research Consultants

leah rubin wrote:
Dr. Kendall,

I have a string variable called image and for some reason the first 3 values are different from the last 37 (see values below). I need to recode this variable into a new one where 901-24 = 0, 902-24 = 1, 903-24 = 3, 9 = 4...all the way to
171 = 38.

I wrote the following but this only converts the last 37 values properly and not the first 3.
Thanks for your help!
Leah

RECODE IMAGE_V1 (CONVERT) ('901-24' = 0)('902-24' = 1)('903-24' = 2)('9' = 3)('11' = 4)('19' = 5)
               ('25' = 6)('31' = 7)('34' = 8)('39' = 9)('48' = 10)('51' = 11)('54' = 12)('58' = 13)('76' = 14)
               ('77' = 15)('80' = 16)('84' = 17)('86' = 18)('88' = 19)('89' = 20)('92' = 21)('93' = 22)('96' = 23)
               ('98' = 24)('100' = 25)('108' = 26)('113' = 27)('119' = 28)('122' = 29)('125' = 30)('137' = 31)
               ('141' = 32)('150' = 33)('153' = 34)('161' = 35)('166' = 35)('168' = 36)('171' = 37)('173' = 38) INTO IMAGE_V1_R.
execute

901-24                 
902-24                 
903-24                 
9                      
11                     
19                     
25                     
31                     
34                     
39                     
48                     
51                     
54                     
58                     
76                     
77                     
80                     
84                     
86                     
88                     
89                     
92                     
93                     
96                     
98                     
100                    
108                    
113                    
119                    
122                    
125                    
137                    
141                    
150                    
153                    
161                    
166                    
168                    
171                    

<snip>

leah rubin wrote:
Hi,

Can someone help me with the necessary syntax to convert 901-24 to the number 1, 902-24 to the number 2, etc?
TIA,
Leah