Syntax for creating a string variable out of the last 6 digits of another string variable

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

Syntax for creating a string variable out of the last 6 digits of another string variable

Nancy Rusinak
Hello - have a string variable with data of differing lengths in it.  However, the last  6-digits are a unique identifier.  I'd like to create a separate string variable with only those 6 digits.  Anyone know some syntax for this?  I'm sure it is an easy fix!  Can't seem to use strung or concat functions as the data are different lengths.  Example below.  Many thanks!  Nancy

Jones, Bob~bvm312
Roberts, Samuel~ccm546
Smith, Ramona~bnd567

Reply | Threaded
Open this post in threaded view
|

Re: Syntax for creating a string variable out of the last 6 digits of another string variable

Bruce Weaver
Administrator
Look up examples for STRING (to declare the new string variable), LENGTH (to obtain the length of the original string variable), and SUBSTR (to extract the last 6 digits).  I don't have SPSS on this machine, but I think something like this (untested) will work:

STRING UniqueID (A6).
COMPUTE #D1 = LENGTH(YourStringVar) - 5. /* position of first digit .
IF #D1 GE 1 UniqueID = SUBSTR(YourStringVar,#D1,6).
EXECUTE.

HTH.


Nancy Rusinak wrote
Hello - have a string variable with data of differing lengths in it.
 However, the last  6-digits are a unique identifier.  I'd like to create a
separate string variable with only those 6 digits.  Anyone know some syntax
for this?  I'm sure it is an easy fix!  Can't seem to use strung or concat
functions as the data are different lengths.  Example below.  Many thanks!
 Nancy

Jones, Bob~bvm312
Roberts, Samuel~ccm546
Smith, Ramona~bnd567
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: Syntax for creating a string variable out of the last 6 digits of another string variable

Spousta Jan
Bruce, your solution is almost correct, but does not work because of blanks at the end of the strings. Better is to include the RTRIM function:

STRING UniqueID (A6).
COMPUTE #D1 = LENGTH(rtrim(YourStringVar)) - 5. /* position of first digit .
IF #D1 GE 1 UniqueID = SUBSTR(YourStringVar,#D1,6).
EXECUTE.

Best regards,

Jan

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Bruce Weaver
Sent: Thursday, November 24, 2011 2:59 AM
To: [hidden email]
Subject: Re: Syntax for creating a string variable out of the last 6 digits of another string variable

Look up examples for STRING (to declare the new string variable), LENGTH (to obtain the length of the original string variable), and SUBSTR (to extract the last 6 digits).  I don't have SPSS on this machine, but I think something like this (untested) will work:

STRING UniqueID (A6).
COMPUTE #D1 = LENGTH(YourStringVar) - 5. /* position of first digit .
IF #D1 GE 1 UniqueID = SUBSTR(YourStringVar,#D1,6).
EXECUTE.

HTH.



Nancy Rusinak wrote

>
> Hello - have a string variable with data of differing lengths in it.
>  However, the last  6-digits are a unique identifier.  I'd like to
> create a separate string variable with only those 6 digits.  Anyone
> know some syntax for this?  I'm sure it is an easy fix!  Can't seem to
> use strung or concat functions as the data are different lengths.
> Example below.  Many thanks!
>  Nancy
>
> Jones, Bob~bvm312
> Roberts, Samuel~ccm546
> Smith, Ramona~bnd567
>


-----
--
Bruce Weaver
[hidden email]
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

NOTE: My Hotmail account is not monitored regularly.
To send me an e-mail, please use the address shown above.

--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Syntax-for-creating-a-string-variable-out-of-the-last-6-digits-of-another-string-variable-tp5018788p5019029.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



_____________
Tato zpráva a všechny připojené soubory jsou důvěrné a určené výlučně adresátovi(-ům). Jestliže nejste oprávněným adresátem, je zakázáno jakékoliv zveřejňování, zprostředkování nebo jiné použití těchto informací. Jestliže jste tento mail dostali neoprávněně, prosím, uvědomte odesilatele a smažte zprávu i přiložené soubory. Odesilatel nezodpovídá za jakékoliv chyby nebo opomenutí způsobené tímto přenosem.

Jste si jisti, že opravdu potřebujete vytisknout tuto zprávu a/nebo její přílohy? Myslete na přírodu.


This message and any attached files are confidential and intended solely for the addressee(s). Any publication, transmission or other use of the information by a person or entity other than the intended addressee is prohibited. If you receive this in error please contact the sender and delete the message as well as all attached documents. The sender does not accept liability for any errors or omissions as a result of the transmission.

Are you sure that you really need a print version of this message and/or its attachments? Think about nature.

-.- --

=====================
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: Syntax for creating a string variable out of the last 6 digits of another string variable

Bruce Weaver
Administrator
Well-spotted, Jan.  Thanks.  ;-)


Spousta Jan wrote
Bruce, your solution is almost correct, but does not work because of blanks at the end of the strings. Better is to include the RTRIM function:

STRING UniqueID (A6).
COMPUTE #D1 = LENGTH(rtrim(YourStringVar)) - 5. /* position of first digit .
IF #D1 GE 1 UniqueID = SUBSTR(YourStringVar,#D1,6).
EXECUTE.

Best regards,

Jan

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Bruce Weaver
Sent: Thursday, November 24, 2011 2:59 AM
To: [hidden email]
Subject: Re: Syntax for creating a string variable out of the last 6 digits of another string variable

Look up examples for STRING (to declare the new string variable), LENGTH (to obtain the length of the original string variable), and SUBSTR (to extract the last 6 digits).  I don't have SPSS on this machine, but I think something like this (untested) will work:

STRING UniqueID (A6).
COMPUTE #D1 = LENGTH(YourStringVar) - 5. /* position of first digit .
IF #D1 GE 1 UniqueID = SUBSTR(YourStringVar,#D1,6).
EXECUTE.

HTH.



Nancy Rusinak wrote
>
> Hello - have a string variable with data of differing lengths in it.
>  However, the last  6-digits are a unique identifier.  I'd like to
> create a separate string variable with only those 6 digits.  Anyone
> know some syntax for this?  I'm sure it is an easy fix!  Can't seem to
> use strung or concat functions as the data are different lengths.
> Example below.  Many thanks!
>  Nancy
>
> Jones, Bob~bvm312
> Roberts, Samuel~ccm546
> Smith, Ramona~bnd567
>


-----
--
Bruce Weaver
[hidden email]
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

NOTE: My Hotmail account is not monitored regularly.
To send me an e-mail, please use the address shown above.

--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Syntax-for-creating-a-string-variable-out-of-the-last-6-digits-of-another-string-variable-tp5018788p5019029.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



_____________
Tato zpráva a všechny připojené soubory jsou důvěrné a určené výlučně adresátovi(-ům). Jestliže nejste oprávněným adresátem, je zakázáno jakékoliv zveřejňování, zprostředkování nebo jiné použití těchto informací. Jestliže jste tento mail dostali neoprávněně, prosím, uvědomte odesilatele a smažte zprávu i přiložené soubory. Odesilatel nezodpovídá za jakékoliv chyby nebo opomenutí způsobené tímto přenosem.

Jste si jisti, že opravdu potřebujete vytisknout tuto zprávu a/nebo její přílohy? Myslete na přírodu.


This message and any attached files are confidential and intended solely for the addressee(s). Any publication, transmission or other use of the information by a person or entity other than the intended addressee is prohibited. If you receive this in error please contact the sender and delete the message as well as all attached documents. The sender does not accept liability for any errors or omissions as a result of the transmission.

Are you sure that you really need a print version of this message and/or its attachments? Think about nature.

-.- --

=====================
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
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: Syntax for creating a string variable out of the last 6 digits of another string variable

David Marso
Administrator

string UniqueID (a6).
COMPUTE UniqueID=SUBSTR(YourStringVar,MAX(1,LENGTH(RTRIM(YourStringVar))-5 )).
--

Bruce Weaver wrote
Well-spotted, Jan.  Thanks.  ;-)


Spousta Jan wrote
Bruce, your solution is almost correct, but does not work because of blanks at the end of the strings. Better is to include the RTRIM function:

STRING UniqueID (A6).
COMPUTE #D1 = LENGTH(rtrim(YourStringVar)) - 5. /* position of first digit .
IF #D1 GE 1 UniqueID = SUBSTR(YourStringVar,#D1,6).
EXECUTE.

Best regards,

Jan

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Bruce Weaver
Sent: Thursday, November 24, 2011 2:59 AM
To: [hidden email]
Subject: Re: Syntax for creating a string variable out of the last 6 digits of another string variable

Look up examples for STRING (to declare the new string variable), LENGTH (to obtain the length of the original string variable), and SUBSTR (to extract the last 6 digits).  I don't have SPSS on this machine, but I think something like this (untested) will work:

STRING UniqueID (A6).
COMPUTE #D1 = LENGTH(YourStringVar) - 5. /* position of first digit .
IF #D1 GE 1 UniqueID = SUBSTR(YourStringVar,#D1,6).
EXECUTE.

HTH.



Nancy Rusinak wrote
>
> Hello - have a string variable with data of differing lengths in it.
>  However, the last  6-digits are a unique identifier.  I'd like to
> create a separate string variable with only those 6 digits.  Anyone
> know some syntax for this?  I'm sure it is an easy fix!  Can't seem to
> use strung or concat functions as the data are different lengths.
> Example below.  Many thanks!
>  Nancy
>
> Jones, Bob~bvm312
> Roberts, Samuel~ccm546
> Smith, Ramona~bnd567
>


-----
--
Bruce Weaver
[hidden email]
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

NOTE: My Hotmail account is not monitored regularly.
To send me an e-mail, please use the address shown above.

--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Syntax-for-creating-a-string-variable-out-of-the-last-6-digits-of-another-string-variable-tp5018788p5019029.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



_____________
Tato zpráva a všechny připojené soubory jsou důvěrné a určené výlučně adresátovi(-ům). Jestliže nejste oprávněným adresátem, je zakázáno jakékoliv zveřejňování, zprostředkování nebo jiné použití těchto informací. Jestliže jste tento mail dostali neoprávněně, prosím, uvědomte odesilatele a smažte zprávu i přiložené soubory. Odesilatel nezodpovídá za jakékoliv chyby nebo opomenutí způsobené tímto přenosem.

Jste si jisti, že opravdu potřebujete vytisknout tuto zprávu a/nebo její přílohy? Myslete na přírodu.


This message and any attached files are confidential and intended solely for the addressee(s). Any publication, transmission or other use of the information by a person or entity other than the intended addressee is prohibited. If you receive this in error please contact the sender and delete the message as well as all attached documents. The sender does not accept liability for any errors or omissions as a result of the transmission.

Are you sure that you really need a print version of this message and/or its attachments? Think about nature.

-.- --

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