# converting letters to integer numbers

16 messages
Open this post in threaded view
|

## converting letters to integer numbers

 It’s that question in various iterations that i ask every so often. Given x (a1 format) = ‘R’, for instance, I’d like to convert the value of x to a number that arithmetic can be performed on, i.e., F format.   I know that x can be displayed as ahex2 with a format statement as the manual shows (p 59-60).   I know there’s other ways, such as a do repeat, and probably other ways that more knowledgable people know but is there straight forward single statement compute based sequence maybe involving the string and numeric functions?   Oh, let me add that this is 21 and I think it is not Unicode, or, at least, the data editor does not indicate Unicode, which I think 22 does.   Thanks Gene Maguin
Open this post in threaded view
|

 I was wrong. The data encoding is Unicode.
Open this post in threaded view
|

## Re: converting letters to integer numbers

 In reply to this post by Maguin, Eugene This will give you the numeric codes for each character. data list list/letter(a1). begin data 'a' 'A' '1' end data. dataset name letters. compute number = number(letter, PIB1). list. If your inputs are actually numeric strings, then you can just use F format above. V21 (and earlier and later versions) can be in either Unicode or code page format at your option. Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] phone: 720-342-5621 From:        "Maguin, Eugene" <[hidden email]> To:        [hidden email], Date:        05/07/2014 04:03 PM Subject:        [SPSSX-L] converting letters to integer numbers Sent by:        "SPSSX(r) Discussion" <[hidden email]> It’s that question in various iterations that i ask every so often. Given x (a1 format) = ‘R’, for instance, I’d like to convert the value of x to a number that arithmetic can be performed on, i.e., F format.   I know that x can be displayed as ahex2 with a format statement as the manual shows (p 59-60).   I know there’s other ways, such as a do repeat, and probably other ways that more knowledgable people know but is there straight forward single statement compute based sequence maybe involving the string and numeric functions?   Oh, let me add that this is 21 and I think it is not Unicode, or, at least, the data editor does not indicate Unicode, which I think 22 does.   Thanks Gene Maguin
Open this post in threaded view
|

## Re: converting letters to integer numbers

 I won't ask why someone wants to perform arithmetic on letters, but what about AUTORECODE? If you need a coding that can be reused and always produce the same autorecoded values, you can create a dataset with all the letters of the alphabet (upper and lower case), and use the SAVE TEMPLATE subcommand to create an autorecode template. Rick Oliver Senior Information Developer IBM Business Analytics (SPSS) E-mail: [hidden email] From:        Jon K Peck/Chicago/IBM@IBMUS To:        [hidden email], Date:        05/07/2014 05:39 PM Subject:        Re: converting letters to integer numbers Sent by:        "SPSSX(r) Discussion" <[hidden email]> This will give you the numeric codes for each character. data list list/letter(a1). begin data 'a' 'A' '1' end data. dataset name letters. compute number = number(letter, PIB1). list. If your inputs are actually numeric strings, then you can just use F format above. V21 (and earlier and later versions) can be in either Unicode or code page format at your option. Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] phone: 720-342-5621 From:        "Maguin, Eugene" <[hidden email]> To:        [hidden email], Date:        05/07/2014 04:03 PM Subject:        [SPSSX-L] converting letters to integer numbers Sent by:        "SPSSX(r) Discussion" <[hidden email]> It’s that question in various iterations that i ask every so often. Given x (a1 format) = ‘R’, for instance, I’d like to convert the value of x to a number that arithmetic can be performed on, i.e., F format.   I know that x can be displayed as ahex2 with a format statement as the manual shows (p 59-60).   I know there’s other ways, such as a do repeat, and probably other ways that more knowledgable people know but is there straight forward single statement compute based sequence maybe involving the string and numeric functions?   Oh, let me add that this is 21 and I think it is not Unicode, or, at least, the data editor does not indicate Unicode, which I think 22 does.   Thanks Gene Maguin
Open this post in threaded view
|

## Re: converting letters to integer numbers

Open this post in threaded view
|

## Re: converting letters to integer numbers

 In reply to this post by Maguin, Eugene Postscript: At 06:02 PM 5/7/2014, Maguin, Eugene wrote: >It's that question in various iterations that i ask every so often. >Given x (a1 format) = 'R', for instance, I'd like to convert the >value of x to a number that arithmetic can be performed on, i.e., F format. Would you say how you're planning to use the numerical values? As always, that information may help us suggest alternative approaches. ===================== 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
Open this post in threaded view
|

## Re: converting letters to integer numbers

 Jon, thank you. It worked excellently. And thank you to all who replied. Richard, I wrote a tidbit of code to compute random permutations and I wanted to check them for internal consistency. Rather than permutating numbers, I permutated letters. The check is that no letter appears more than once in the string. I had thought the summing the letter's numeric codes would give me that check but it won't. Gene Maguin -----Original Message----- From: Richard Ristow [mailto:[hidden email]] Sent: Wednesday, May 07, 2014 7:31 PM To: Maguin, Eugene; [hidden email] Subject: Re: converting letters to integer numbers Postscript: At 06:02 PM 5/7/2014, Maguin, Eugene wrote: >It's that question in various iterations that i ask every so often. >Given x (a1 format) = 'R', for instance, I'd like to convert the value >of x to a number that arithmetic can be performed on, i.e., F format. Would you say how you're planning to use the numerical values? As always, that information may help us suggest alternative approaches. ===================== 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
Open this post in threaded view
|

## Re: converting letters to integer numbers

 Administrator Hi Gene, Does the 3rd segment of code do what you require? /* Generate 10000 cases of uniform (0,1) */. NEW FILE. DATASET CLOSE ALL. DATASET DECLARE one. MATRIX. SAVE UNIFORM(10000,1)/OUTFILE one/VARIABLES junk. END MATRIX. DATASET ACTIVATE one. /* Generate permutations of length 5 from {A:Z}*/. STRING #alpha (A26). IF (\$CASENUM EQ 1) #alpha="ABCDEFGHIJKLMNOPQRSTUVWXYZ". STRING Permutation (A5). LOOP #=1 TO 5. COMPUTE Permutation=CONCAT(Permutation,CHAR.SUBSTR(#alpha,TRUNC(RV.UNIFORM(1,CHAR.LENGTH(#alpha)),1))). END LOOP. EXECUTE. /* Test for repetitions */. LOOP #=1 TO CHAR.LENGTH(Permutation)-1. COMPUTE repetition = CHAR.RINDEX(Permutation,CHAR.SUBSTR(Permutation,#,1),1) GT #. END LOOP IF repetition. FREQUENCIES repetition. Maguin, Eugene wrote Jon, thank you. It worked excellently. And thank you to all who replied. Richard, I wrote a tidbit of code to compute random permutations and I wanted to check them for internal consistency. Rather than permutating numbers, I permutated letters. The check is that no letter appears more than once in the string. I had thought the summing the letter's numeric codes would give me that check but it won't. Gene Maguin -----Original Message----- From: Richard Ristow [mailto:[hidden email]] Sent: Wednesday, May 07, 2014 7:31 PM To: Maguin, Eugene; [hidden email]Subject: Re: converting letters to integer numbers Postscript: At 06:02 PM 5/7/2014, Maguin, Eugene wrote: >It's that question in various iterations that i ask every so often. >Given x (a1 format) = 'R', for instance, I'd like to convert the value >of x to a number that arithmetic can be performed on, i.e., F format. Would you say how you're planning to use the numerical values? As always, that information may help us suggest alternative approaches. ===================== 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 Please reply to the list and not to my personal email. Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Open this post in threaded view
|

## Re: converting letters to integer numbers

 At 11:07 AM 5/8/2014, David Marso posted in this thread. I think the code posted has a couple of bugs: . It'll never select letter 'Z'. . It draws characters with replacement, so it'll easily generate repetitions The code below is David's, except, . I've used old-form string functions rather than CHAR. functions, because of the age of my SPSS version. . I've broken up the longest statement, in the loop to generate permutation, by introducing intermediate scratch variables. The permutation-generating code is, COMPUTE #AlphLen = LENGTH(RTRIM(#alpha)). LOOP #=1 TO 5. .  COMPUTE #RandLtr = SUBSTR(#alpha,                               TRUNC(RV.UNIFORM(1,#AlphLen)),                               1). .  COMPUTE Permutation=     CONCAT(RTRIM(Permutation),#RandLtr). END LOOP. A. This selects letters at random with replacement, so it can easily pull duplicate letters. B. I don't think it will ever select 'Z'. I think you want TRUNC(RV.UNIFORM(1,#AlphLen+1)) Here's the output of a test run. Note repeated letters in cases 1011, 1012, 1017 and 1018. |-----------------------------|---------------------------| |Output Created               |08-MAY-2014 13:03:52       | |-----------------------------|---------------------------|   [Cases]    ID Permutation Repetition 1001 SMDEJ            0 1002 NBPQE            0 1003 BTXLW            0 1004 VCJYO            0 1005 JWFCH            0 1006 CRDBV            0 1007 DTJNE            0 1008 PCRDJ            0 1009 SRKUQ            0 1010 HPIUF            0 1011 HQDOQ            1 1012 XSUGU            1 1013 YSKLF            0 1014 NAKHS            0 1015 PYKLS            0 1016 VQHIG            0 1017 RJOGG            1 1018 RFROU            1 1019 SIPXB            0 1020 ACWIS            0 Number of cases read:  20    Number of cases listed:  20 ========================================= APPENDIX: All code (not saved separately) ========================================= SET RNG = MT       /* 'Mersenne twister' random number generator */ . SET MTINDEX = 9104 /*  Pocket calculator RNG                     */ . NEW FILE. INPUT PROGRAM. .  NUMERIC ID (N4). .  LOOP    ID = 1001 TO 1020. .    END CASE. .  END LOOP. .  END FILE. END INPUT PROGRAM. DATASET NAME     Cases WINDOW=FRONT. /* Generate permutations of length 5 from {A:Z}*/. STRING  #alpha (A26). IF (\$CASENUM EQ 1) #alpha="ABCDEFGHIJKLMNOPQRSTUVWXYZ". STRING  #RandLtr (A1). STRING  Permutation (A5). NUMERIC #AlphLen (F3). COMPUTE #AlphLen = LENGTH(RTRIM(#alpha)). LOOP #=1 TO 5. .  COMPUTE #RandLtr = SUBSTR(#alpha,                               TRUNC(RV.UNIFORM(1,#AlphLen)),                               1). .  COMPUTE Permutation=     CONCAT(RTRIM(Permutation),#RandLtr). END LOOP. EXECUTE. /* Test for repetitions */. NUMERIC Repetition (F2). /* Test for repetitions */. LOOP #=1 TO LENGTH(RTRIM(Permutation))-1. .  COMPUTE repetition = RINDEX(Permutation,                                 SUBSTR(Permutation,#,1),1) GT #. END LOOP IF repetition. LIST. ===================== 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
Open this post in threaded view
|

## Re: converting letters to integer numbers

 Administrator "A. This selects letters at random with replacement, so it can easily pull duplicate letters. B. I don't think it will ever select 'Z'. I think you want TRUNC(RV.UNIFORM(1,#AlphLen+1)) " A.The sampling with replacement was intentional in order to generate data which could be tested for repetitions ;-) B.  Good catch Richard, +1 it should be.  I could argue that this was intentional as well in order to keep you and others on your toes ;-))) -- Richard Ristow wrote At 11:07 AM 5/8/2014, David Marso posted in this thread. I think the code posted has a couple of bugs: . It'll never select letter 'Z'. . It draws characters with replacement, so it'll easily generate repetitions The code below is David's, except, . I've used old-form string functions rather than CHAR. functions, because of the age of my SPSS version. . I've broken up the longest statement, in the loop to generate permutation, by introducing intermediate scratch variables. The permutation-generating code is, COMPUTE #AlphLen = LENGTH(RTRIM(#alpha)). LOOP #=1 TO 5. .  COMPUTE #RandLtr = SUBSTR(#alpha,                               TRUNC(RV.UNIFORM(1,#AlphLen)),                               1). .  COMPUTE Permutation=     CONCAT(RTRIM(Permutation),#RandLtr). END LOOP. A. This selects letters at random with replacement, so it can easily pull duplicate letters. B. I don't think it will ever select 'Z'. I think you want TRUNC(RV.UNIFORM(1,#AlphLen+1)) Here's the output of a test run. Note repeated letters in cases 1011, 1012, 1017 and 1018. |-----------------------------|---------------------------| |Output Created               |08-MAY-2014 13:03:52       | |-----------------------------|---------------------------|   [Cases]    ID Permutation Repetition 1001 SMDEJ            0 1002 NBPQE            0 1003 BTXLW            0 1004 VCJYO            0 1005 JWFCH            0 1006 CRDBV            0 1007 DTJNE            0 1008 PCRDJ            0 1009 SRKUQ            0 1010 HPIUF            0 1011 HQDOQ            1 1012 XSUGU            1 1013 YSKLF            0 1014 NAKHS            0 1015 PYKLS            0 1016 VQHIG            0 1017 RJOGG            1 1018 RFROU            1 1019 SIPXB            0 1020 ACWIS            0 Number of cases read:  20    Number of cases listed:  20 ========================================= APPENDIX: All code (not saved separately) ========================================= SET RNG = MT       /* 'Mersenne twister' random number generator */ . SET MTINDEX = 9104 /*  Pocket calculator RNG                     */ . NEW FILE. INPUT PROGRAM. .  NUMERIC ID (N4). .  LOOP    ID = 1001 TO 1020. .    END CASE. .  END LOOP. .  END FILE. END INPUT PROGRAM. DATASET NAME     Cases WINDOW=FRONT. /* Generate permutations of length 5 from {A:Z}*/. STRING  #alpha (A26). IF (\$CASENUM EQ 1) #alpha="ABCDEFGHIJKLMNOPQRSTUVWXYZ". STRING  #RandLtr (A1). STRING  Permutation (A5). NUMERIC #AlphLen (F3). COMPUTE #AlphLen = LENGTH(RTRIM(#alpha)). LOOP #=1 TO 5. .  COMPUTE #RandLtr = SUBSTR(#alpha,                               TRUNC(RV.UNIFORM(1,#AlphLen)),                               1). .  COMPUTE Permutation=     CONCAT(RTRIM(Permutation),#RandLtr). END LOOP. EXECUTE. /* Test for repetitions */. NUMERIC Repetition (F2). /* Test for repetitions */. LOOP #=1 TO LENGTH(RTRIM(Permutation))-1. .  COMPUTE repetition = RINDEX(Permutation,                                 SUBSTR(Permutation,#,1),1) GT #. END LOOP IF repetition. LIST. ===================== 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 Please reply to the list and not to my personal email. Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Open this post in threaded view
|

## Re: converting letters to integer numbers

Open this post in threaded view
|

## Re: converting letters to integer numbers

 Administrator In reply to this post by David Marso I previously posted a macro to create random permutations and realized it was not particularly general and/or commented consistently with the code. Here's another try. DEFINE !Permute(!POSITIONAL !TOKENS(1)    / !POSITIONAL !TOKENS(1)    / FROM !CMDEND !DEFAULT (ABCDEFGHIJKLMNOPQRSTUVWXYZ)). PRESERVE. SET MXLOOPS=1000000. STRING #alpha (!CONCAT("A",!LENGTH(!FROM))). IF (\$CASENUM EQ 1) #alpha=!QUOTE(!FROM). STRING !2 (!CONCAT("A",!1)). STRING #ltr (A1). COMPUTE #=1. COMPUTE #alphalength=CHAR.LENGTH(#alpha) + 1. LOOP. +  COMPUTE #ltr=CHAR.SUBSTR(#alpha,TRUNC(RV.UNIFORM(1,#alphalength))). +  DO IF CHAR.INDEX(!2,#ltr) EQ 0. +    COMPUTE !2=CONCAT(RTRIM(!2),#ltr). +    COMPUTE # = # + 1. +  END IF. END LOOP IF # GT !1. RESTORE. !ENDDEFINE. SET MPRINT ON. /*Create random permutations of length 6 from default set and put in Perm6 */. !Permute 6 Perm6. /*Create random permutations of length 5 from arbitrary character set and put in Perm5 */. !Permute 5 Perm5 FROM 1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ . EXECUTE. Please reply to the list and not to my personal email. Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Open this post in threaded view
|

## Re: converting letters to integer numbers

Open this post in threaded view
|

## Re: converting letters to integer numbers

 Administrator Here is a MUCH MORE EFFICIENT rewrite of the exhaustive list method. Don't know what I was thinking earlier ;-))) No need to run through twice nor any requirement for a second matrix. SET MXLOOPS=10000000. /* Exhaustive list */. MATRIX. COMPUTE x=9. COMPUTE p=T({1:x}). LOOP #=1 TO x-1. +  COMPUTE p={KRONEKER(T({1:x}),MAKE(NROW(p),1,1)),KRONEKER(MAKE(x,1,1),p)}. +  COMPUTE goodrow=1. +  LOOP ##=1 TO NROW(p). +    COMPUTE target=p(##,1). +    LOOP ###=2 TO NCOL(p). +        COMPUTE found=(p(##,###) EQ target). +    END LOOP IF found. +    DO IF (found EQ 0). +      COMPUTE p(goodrow,:)=p(##,:). +      COMPUTE goodrow=goodrow+1. +    END IF. +  END LOOP. +  COMPUTE p=p(1:(goodrow-1),:). END LOOP. SAVE p / OUTFILE * . END MATRIX. Please reply to the list and not to my personal email. Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"