Remove leading zeros from string variable

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

Remove leading zeros from string variable

Maria Suchard
Hello Fellow Listers,

I am working on a problem with student ids, I am are often finding that leading zeros are coming into our datasets for various reasons.  When attempting to match students across different datasets, this is becoming an issue due to that not all student ids (for the same student) will have leading zeros.  With this, I am trying to figure out a way to remove leading zeros from student ids that can vary in length.  I have seen ids with 1 leading zero to 5 leading zeros.  Student ids in our datasets are defined as string variables as there are times that alpha characters are used.

With this, I have searched the web.  I have tried using ltrim, char.substr., there is something I am missing.  

Here are snipits of code I have tried: I know these are very wrong.

STRING STUDID(a13).
COMPUTE STUDID=char.substr(STUID,1,length(STUID)-1) IF CHAR.SUBSTR(STUID,-1,1)="0".
or
STRING STUDID(a13).
COMPUTE STUDID=substr(STUID,"0", ' ', 1).
OR
STRING STUDID(a13).
COMPUTE STUDID=LTRIM(FIRST,'0').

Any assistance would be greatly appreciated.

Thank you.

Maria S.
Reply | Threaded
Open this post in threaded view
|

Re: Remove leading zeros from string variable

David Marso
Administrator
Untested:

LOOP.
IF CHAR.SUBSTR(id,1,1) EQ "0" ID=CHAR.SUBSTR(ID,2).
END LOOP IF CHAR.SUBSTR(id,1,1) NE "0".

Maria Suchard wrote
Hello Fellow Listers,

I am working on a problem with student ids, I am are often finding that leading zeros are coming into our datasets for various reasons.  When attempting to match students across different datasets, this is becoming an issue due to that not all student ids (for the same student) will have leading zeros.  With this, I am trying to figure out a way to remove leading zeros from student ids that can vary in length.  I have seen ids with 1 leading zero to 5 leading zeros.  Student ids in our datasets are defined as string variables as there are times that alpha characters are used.

With this, I have searched the web.  I have tried using ltrim, char.substr., there is something I am missing.  

Here are snipits of code I have tried: I know these are very wrong.

STRING STUDID(a13).
COMPUTE STUDID=char.substr(STUID,1,length(STUID)-1) IF CHAR.SUBSTR(STUID,-1,1)="0".
or
STRING STUDID(a13).
COMPUTE STUDID=substr(STUID,"0", ' ', 1).
OR
STRING STUDID(a13).
COMPUTE STUDID=LTRIM(FIRST,'0').

Any assistance would be greatly appreciated.

Thank you.

Maria S.
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?"
Reply | Threaded
Open this post in threaded view
|

Re: Remove leading zeros from string variable

Rick Oliver-3
I don't see why LTRIM using "0" as the character won't work.

data list free /id (a8).
begin data
000123 0456 0000078 00A
end data.
compute id=ltrim(id, "0").
list.


Rick Oliver
Senior Information Developer
IBM Business Analytics (SPSS)
E-mail: [hidden email]




From:        David Marso <[hidden email]>
To:        [hidden email]
Date:        02/06/2015 02:54 PM
Subject:        Re: Remove leading zeros from string variable
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Untested:

LOOP.
IF CHAR.SUBSTR(id,1,1) EQ "0" ID=CHAR.SUBSTR(ID,2).
END LOOP IF CHAR.SUBSTR(id,1,1) NE "0".


Maria Suchard wrote
> Hello Fellow Listers,
>
> I am working on a problem with student ids, I am are often finding that
> leading zeros are coming into our datasets for various reasons.  When
> attempting to match students across different datasets, this is becoming
> an issue due to that not all student ids (for the same student) will have
> leading zeros.  With this, I am trying to figure out a way to remove
> leading zeros from student ids that can vary in length.  I have seen ids
> with 1 leading zero to 5 leading zeros.  Student ids in our datasets are
> defined as string variables as there are times that alpha characters are
> used.
>
> With this, I have searched the web.  I have tried using ltrim,
> char.substr., there is something I am missing.  
>
> Here are snipits of code I have tried: I know these are very wrong.
>
> STRING STUDID(a13).
> COMPUTE STUDID=char.substr(STUID,1,length(STUID)-1) IF
> CHAR.SUBSTR(STUID,-1,1)="0".
> or
> STRING STUDID(a13).
> COMPUTE STUDID=substr(STUID,"0", ' ', 1).
> OR
> STRING STUDID(a13).
> COMPUTE STUDID=LTRIM(FIRST,'0').
>
> Any assistance would be greatly appreciated.
>
> Thank you.
>
> Maria S.





-----
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?"
--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Remove-leading-zeros-from-string-variable-tp5728593p5728594.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
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


===================== 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
Reply | Threaded
Open this post in threaded view
|

Re: Remove leading zeros from string variable

Albert-Jan Roskam-2
In reply to this post by Maria Suchard
Or simply:
Compute id = ltrim(id, "0").


------------------------------
On Fri, Feb 6, 2015 9:52 PM CET David Marso wrote:

>Untested:
>
>LOOP.
>IF CHAR.SUBSTR(id,1,1) EQ "0" ID=CHAR.SUBSTR(ID,2).
>END LOOP IF CHAR.SUBSTR(id,1,1) NE "0".
>
>
>Maria Suchard wrote
>> Hello Fellow Listers,
>>
>> I am working on a problem with student ids, I am are often finding that
>> leading zeros are coming into our datasets for various reasons.  When
>> attempting to match students across different datasets, this is becoming
>> an issue due to that not all student ids (for the same student) will have
>> leading zeros.  With this, I am trying to figure out a way to remove
>> leading zeros from student ids that can vary in length.  I have seen ids
>> with 1 leading zero to 5 leading zeros.  Student ids in our datasets are
>> defined as string variables as there are times that alpha characters are
>> used.
>>
>> With this, I have searched the web.  I have tried using ltrim,
>> char.substr., there is something I am missing.  
>>
>> Here are snipits of code I have tried: I know these are very wrong.
>>
>> STRING STUDID(a13).
>> COMPUTE STUDID=char.substr(STUID,1,length(STUID)-1) IF
>> CHAR.SUBSTR(STUID,-1,1)="0".
>> or
>> STRING STUDID(a13).
>> COMPUTE STUDID=substr(STUID,"0", ' ', 1).
>> OR
>> STRING STUDID(a13).
>> COMPUTE STUDID=LTRIM(FIRST,'0').
>>
>> Any assistance would be greatly appreciated.
>>
>> Thank you.
>>
>> Maria S.
>
>
>
>
>
>-----
>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?"
>--
>View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Remove-leading-zeros-from-string-variable-tp5728593p5728594.html
>Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>
>=====================
>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

=====================
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
Reply | Threaded
Open this post in threaded view
|

Re: Remove leading zeros from string variable

Maria Suchard
In reply to this post by David Marso
Thank you all for the help.  All options worked! Although, I did get the following warning when running the loop syntax.  I found that if you SET MXLOOPS = 1000 it will help with the error below.  

However, if I run the loop syntax on a file of over 150,000 cases should I set the MXLOOPS='a larger value'?

Thank you all again!!
Best,
Maria
 
>Warning # 534
>Execution of a loop was terminated after MXLOOPS trips.  The value of MXLOOPS
>can be displayed with the SHOW command and changed with the SET command.
>Command line: 684  Current case: 78  Current splitfile group: 1