SPSS Statistics

 View Only
  • 1.  Need help with inserting 0 automatically to get 02 instead of 2

    Posted Thu March 28, 2024 09:56 AM

    Hi everyone, 

    I am currently working on my data file and saw that the output that was generated changed the numbers. We have an personal identifier code that was assessed which contains of 4 letters and 2 numbers (e. g. AB02CD). The problem is,  that during the export of our data from the online survey, the format changed so that now the code is displayed as "AB2CD" instead of "AB02CD". Is there any way, where I can automatically insert the 0 for all "single numbers"? We also have the single variables for the code, meaning "AB", "02" and "CD" which I could use to insert the 0 and merge them back togther afterwards.

    I hope my problem is understandable and someone has a solution or a syntax for me. Thanks everyone! 



    ------------------------------
    Ronja
    ------------------------------


  • 2.  RE: Need help with inserting 0 automatically to get 02 instead of 2

    IBM Champion
    Posted Thu March 28, 2024 10:17 AM
    The problem is probably that the variable holding the numeric part is type numeric rather than string.  One way to fix the ids is this, where id is the variable holding the six character code.  It creates a new variable id2 with a zero inserted  before a numeric character

    spssinc trans result = id2 type=6
        /formula 're.sub(r"(\d)", r"0\1", id)'.

    spssinc trans is an extension command.  You probably already have it, but if not, install it via Extensions > Extension Hub.

    You could, alternatively, do this in standard syntax by changing the type of the numeric variable to string and concatenating a 0 at the start with the concat function and then remerging the three parts.

    --





  • 3.  RE: Need help with inserting 0 automatically to get 02 instead of 2

    Posted Thu March 28, 2024 10:31 AM

    Thank you for the quick answer! If I would to it as suggested SPSS would insert the 0 in front of every number, right? So if there is the Code "AB24CD" it would change into "AB024CD" which would also be incorrect for our further use. Or did I misunderstand?



    ------------------------------
    Ronja Helmchen
    ------------------------------



  • 4.  RE: Need help with inserting 0 automatically to get 02 instead of 2

    IBM Champion
    Posted Thu March 28, 2024 12:04 PM
    That's a different case. Try this instead.
    spssinc trans result = id3 type=8
        /formula 're.sub(r"(\D)(\d)(\D)", r"\1\060\2\3", id2, flags=re.U)'.

    --





  • 5.  RE: Need help with inserting 0 automatically to get 02 instead of 2

    IBM Champion
    Posted Thu March 28, 2024 12:05 PM

    in that code, id3 is the output variable and id2 is the input.  Change as desired.



    ------------------------------
    Jon Peck
    ------------------------------