substring

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

substring

Keval Khichadia
Hi,

I have a string variable coursekey (a10) which contains both characters and numbers (i.e NURS5555).

I would like to only keep the numerical part of this variable, but I am not sure how to use the substr function because the numerical part does not always start at the 5th position.

Thanks,

Keval

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

ViAnn Beadle
Are numbers interspersed with letters? Will a letter ever follow a number?
Are there are any blanks or special characters in a string? Give us some
more examples.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Keval Khichadia
Sent: Tuesday, September 16, 2008 1:03 PM
To: [hidden email]
Subject: substring

Hi,

I have a string variable coursekey (a10) which contains both characters and
numbers (i.e NURS5555).

I would like to only keep the numerical part of this variable, but I am not
sure how to use the substr function because the numerical part does not
always start at the 5th position.

Thanks,

Keval

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

Keval Khichadia
In reply to this post by Keval Khichadia
Hi, sorry about not being clear.  In all cases I want to keep everything to the right of the first number.  Letters are not usually interspered with numbers,  but there are some cases like (BIO 18B), where I would only want to keep the 18B. There are blanks in the string to separate letters form numbers but only when the letter portion is less than 4 characters.

Here are some examples:

SPAN1222
SCS 1234
BIO 18B
CP 5155


Thank you for all the help,

Keval



----- Original Message ----
From: ViAnn Beadle <[hidden email]>
To: [hidden email]
Sent: Tuesday, September 16, 2008 12:48:33 PM
Subject: Re: substring

Are numbers interspersed with letters? Will a letter ever follow a number?
Are there are any blanks or special characters in a string? Give us some
more examples.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Keval Khichadia
Sent: Tuesday, September 16, 2008 1:03 PM
To: [hidden email]..EDU
Subject: substring

Hi,

I have a string variable coursekey (a10) which contains both characters and
numbers (i.e NURS5555).

I would like to only keep the numerical part of this variable, but I am not
sure how to use the substr function because the numerical part does not
always start at the 5th position.

Thanks,

Keval

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





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

ViAnn Beadle
This is one way to do it but has a kludgy side effect described below:

set mxwarns 0.
data list fixed /stringvar 1-12 (a).
begin data
abc124b
1bbbbbc
aaad1123
1bbb3vv
abc1abc1
1234567890
abc 1def
1a2b3c4d53
end data.
string newstring (a12).
compute hit=0.
loop #i=1 to 12.
do if not(sysmis(number(substr(stringvar,#i,1),f1))) and hit=0.
compute newstring=substr(stringvar,#i).
compute hit=1.
end if.
end loop.
list var stringvar newstring.

The loop goes through the string 1 character at a time. The number function
is used to try to convert the character. If the character is not a number
the result is set to system-missing. This usually generates a warning in the
output but set mxwarns 0 suppresses all but one message. This is fine if you
are running this in interactive mode but will stop processing if you name
this file on the INCLUDE command. You can use the INSERT command instead and
specify ERROR=CONTINUE to continue processing.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Keval Khichadia
Sent: Tuesday, September 16, 2008 3:59 PM
To: [hidden email]
Subject: Re: substring

Hi, sorry about not being clear.  In all cases I want to keep everything to
the right of the first number.  Letters are not usually interspered with
numbers,  but there are some cases like (BIO 18B), where I would only want
to keep the 18B. There are blanks in the string to separate letters form
numbers but only when the letter portion is less than 4 characters.

Here are some examples:

SPAN1222
SCS 1234
BIO 18B
CP 5155


Thank you for all the help,

Keval



----- Original Message ----
From: ViAnn Beadle <[hidden email]>
To: [hidden email]
Sent: Tuesday, September 16, 2008 12:48:33 PM
Subject: Re: substring

Are numbers interspersed with letters? Will a letter ever follow a number?
Are there are any blanks or special characters in a string? Give us some
more examples.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Keval Khichadia
Sent: Tuesday, September 16, 2008 1:03 PM
To: [hidden email]..EDU
Subject: substring

Hi,

I have a string variable coursekey (a10) which contains both characters and
numbers (i.e NURS5555).

I would like to only keep the numerical part of this variable, but I am not
sure how to use the substr function because the numerical part does not
always start at the 5th position.

Thanks,

Keval

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





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

Maguin, Eugene
Wouldn't a way to get around the warning problem be to test whether the
character is either between '0' and '9' in the sort order or less than 'A'
in the sort order. Isn't there also a way to convert a character to an ascii
number using some odd format and test that against some value. It seems like
Richard was an expert with this.

Gene Maguin

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
ViAnn Beadle
Sent: Tuesday, September 16, 2008 11:57 PM
To: [hidden email]
Subject: Re: substring


This is one way to do it but has a kludgy side effect described below:

set mxwarns 0.
data list fixed /stringvar 1-12 (a).
begin data
abc124b
1bbbbbc
aaad1123
1bbb3vv
abc1abc1
1234567890
abc 1def
1a2b3c4d53
end data.
string newstring (a12).
compute hit=0.
loop #i=1 to 12.
do if not(sysmis(number(substr(stringvar,#i,1),f1))) and hit=0.
compute newstring=substr(stringvar,#i).
compute hit=1.
end if.
end loop.
list var stringvar newstring.

The loop goes through the string 1 character at a time. The number function
is used to try to convert the character. If the character is not a number
the result is set to system-missing. This usually generates a warning in the
output but set mxwarns 0 suppresses all but one message. This is fine if you
are running this in interactive mode but will stop processing if you name
this file on the INCLUDE command. You can use the INSERT command instead and
specify ERROR=CONTINUE to continue processing.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Keval Khichadia
Sent: Tuesday, September 16, 2008 3:59 PM
To: [hidden email]
Subject: Re: substring

Hi, sorry about not being clear.  In all cases I want to keep everything to
the right of the first number.  Letters are not usually interspered with
numbers,  but there are some cases like (BIO 18B), where I would only want
to keep the 18B. There are blanks in the string to separate letters form
numbers but only when the letter portion is less than 4 characters.

Here are some examples:

SPAN1222
SCS 1234
BIO 18B
CP 5155


Thank you for all the help,

Keval



----- Original Message ----
From: ViAnn Beadle <[hidden email]>
To: [hidden email]
Sent: Tuesday, September 16, 2008 12:48:33 PM
Subject: Re: substring

Are numbers interspersed with letters? Will a letter ever follow a number?
Are there are any blanks or special characters in a string? Give us some
more examples.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Keval Khichadia
Sent: Tuesday, September 16, 2008 1:03 PM
To: [hidden email]..EDU
Subject: substring

Hi,

I have a string variable coursekey (a10) which contains both characters and
numbers (i.e NURS5555).

I would like to only keep the numerical part of this variable, but I am not
sure how to use the substr function because the numerical part does not
always start at the 5th position.

Thanks,

Keval

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





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

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

Richard Ristow
In reply to this post by Keval Khichadia
At 03:03 PM 9/16/2008, Keval Khichadia wrote:

>I have a string variable coursekey (a10) which contains both
>characters and numbers (i.e NURS5555). I would like to only keep the
>numerical part, but the numerical part does not always start at the
>5th position.

And at 05:58 PM 9/16/2008, expanded:

>In all cases I want to keep everything to the right of the first
>number.  Letters are not usually interspered with numbers,  but
>there are some cases like (BIO 18B), where I would only want to keep the 18B.

At 11:57 PM 9/16/2008, ViAnn Beadle wrote:

>This is one way to do it but has a kludgy side effect described below:
>...
>do if not(sysmis(number(substr(stringvar,#i,1),f1))) and hit=0.
>...
>
>The number function is used to try to convert the character. If the
>character is not a number the result is system-missing. This usually
>generates a warning in the output

You can also test whether a character is a digit, without raising a
warning, using INDEX (not tested):

>do if not (INDEX('0123456789'substr(stringvar,#i,1))) and hit=0.

But there's also a 'cute' solution without a loop, using the
rarely-used third argument to INDEX:

STRING CharPart NumPart (A12).
COMPUTE #FirstDig = INDEX(stringvar,'0123456789',1).
DO IF   #FirstDig GT 0.
.  COMPUTE CharPart = SUBSTR(stringvar,1,#FirstDig-1).
.  COMPUTE NumPart  = SUBSTR(stringvar,  #FirstDig).
ELSE.
.  COMPUTE CharPart = stringvar.
.  COMPUTE NumPart  = ''    /* Not really necessary */.
END IF.
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |17-SEP-2008 16:14:09       |
|-----------------------------|---------------------------|
[Cutey]
stringvar    CharPart     NumPart

abc124b      abc          124b
1bbbbbc                   1bbbbbc
aaad1123     aaad         1123
1bbb3vv                   1bbb3vv
abc1abc1     abc          1abc1
1234567890                1234567890
abc 1def     abc          1def
1a2b3c4d53                1a2b3c4d53
POSTER       POSTER
SPAN1222     SPAN         1222
SCS 1234     SCS          1234
BIO 18B      BIO          18B
CP 5155      CP           5155

Number of cases read:  13    Number of cases listed:  13
=============================
APPENDIX: Test data, and code
=============================
set mxwarns 0.
data list fixed /stringvar 1-12 (a).
begin data
abc124b
1bbbbbc
aaad1123
1bbb3vv
abc1abc1
1234567890
abc 1def
1a2b3c4d53
POSTER
SPAN1222
SCS 1234
BIO 18B
CP 5155
end data.
DATASET NAME     TestData.
DATASET COPY     VBeadle.
DATASET ACTIVATE VBeadle WINDOW=FRONT.
string newstring (a12).
compute hit=0.
loop #i=1 to 12.
do if not(sysmis(number(substr(stringvar,#i,1),f1))) and hit=0.
compute newstring=substr(stringvar,#i).
compute hit=1.
end if.
end loop.
list var stringvar newstring.

DATASET ACTIVATE TestData.
DATASET COPY     Cutey.
DATASET ACTIVATE Cutey    WINDOW=FRONT.

STRING CharPart NumPart (A12).
COMPUTE #FirstDig = INDEX(stringvar,'0123456789',1).
DO IF   #FirstDig GT 0.
.  COMPUTE CharPart = SUBSTR(stringvar,1,#FirstDig-1).
.  COMPUTE NumPart  = SUBSTR(stringvar,  #FirstDig).
ELSE.
.  COMPUTE CharPart = stringvar.
.  COMPUTE NumPart  = ''    /* Not really necessary */.
END IF.
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
Reply | Threaded
Open this post in threaded view
|

Re: substring

Peck, Jon
How about this version?

data list fixed /stringvar 1-12 (a).
begin data
abc124b
1bbbbbc
aaad1123
1bbb3vv
abc1abc1
1234567890
abc 1def
1a2b3c4d53
end data.
string newstring (a12).
loop #i=1 to 12 if (not range(substr(stringvar, #i, 1), '0','9')).
end loop.
compute newstring=substr(stringvar,#i).
list var stringvar newstring.

-Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Richard Ristow
Sent: Wednesday, September 17, 2008 2:18 PM
To: [hidden email]
Subject: Re: [SPSSX-L] substring

At 03:03 PM 9/16/2008, Keval Khichadia wrote:

>I have a string variable coursekey (a10) which contains both
>characters and numbers (i.e NURS5555). I would like to only keep the
>numerical part, but the numerical part does not always start at the
>5th position.

And at 05:58 PM 9/16/2008, expanded:

>In all cases I want to keep everything to the right of the first
>number.  Letters are not usually interspered with numbers,  but
>there are some cases like (BIO 18B), where I would only want to keep the 18B.

At 11:57 PM 9/16/2008, ViAnn Beadle wrote:

>This is one way to do it but has a kludgy side effect described below:
>...
>do if not(sysmis(number(substr(stringvar,#i,1),f1))) and hit=0.
>...
>
>The number function is used to try to convert the character. If the
>character is not a number the result is system-missing. This usually
>generates a warning in the output

You can also test whether a character is a digit, without raising a
warning, using INDEX (not tested):

>do if not (INDEX('0123456789'substr(stringvar,#i,1))) and hit=0.

But there's also a 'cute' solution without a loop, using the
rarely-used third argument to INDEX:

STRING CharPart NumPart (A12).
COMPUTE #FirstDig = INDEX(stringvar,'0123456789',1).
DO IF   #FirstDig GT 0.
.  COMPUTE CharPart = SUBSTR(stringvar,1,#FirstDig-1).
.  COMPUTE NumPart  = SUBSTR(stringvar,  #FirstDig).
ELSE.
.  COMPUTE CharPart = stringvar.
.  COMPUTE NumPart  = ''    /* Not really necessary */.
END IF.
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |17-SEP-2008 16:14:09       |
|-----------------------------|---------------------------|
[Cutey]
stringvar    CharPart     NumPart

abc124b      abc          124b
1bbbbbc                   1bbbbbc
aaad1123     aaad         1123
1bbb3vv                   1bbb3vv
abc1abc1     abc          1abc1
1234567890                1234567890
abc 1def     abc          1def
1a2b3c4d53                1a2b3c4d53
POSTER       POSTER
SPAN1222     SPAN         1222
SCS 1234     SCS          1234
BIO 18B      BIO          18B
CP 5155      CP           5155

Number of cases read:  13    Number of cases listed:  13
=============================
APPENDIX: Test data, and code
=============================
set mxwarns 0.
data list fixed /stringvar 1-12 (a).
begin data
abc124b
1bbbbbc
aaad1123
1bbb3vv
abc1abc1
1234567890
abc 1def
1a2b3c4d53
POSTER
SPAN1222
SCS 1234
BIO 18B
CP 5155
end data.
DATASET NAME     TestData.
DATASET COPY     VBeadle.
DATASET ACTIVATE VBeadle WINDOW=FRONT.
string newstring (a12).
compute hit=0.
loop #i=1 to 12.
do if not(sysmis(number(substr(stringvar,#i,1),f1))) and hit=0.
compute newstring=substr(stringvar,#i).
compute hit=1.
end if.
end loop.
list var stringvar newstring.

DATASET ACTIVATE TestData.
DATASET COPY     Cutey.
DATASET ACTIVATE Cutey    WINDOW=FRONT.

STRING CharPart NumPart (A12).
COMPUTE #FirstDig = INDEX(stringvar,'0123456789',1).
DO IF   #FirstDig GT 0.
.  COMPUTE CharPart = SUBSTR(stringvar,1,#FirstDig-1).
.  COMPUTE NumPart  = SUBSTR(stringvar,  #FirstDig).
ELSE.
.  COMPUTE CharPart = stringvar.
.  COMPUTE NumPart  = ''    /* Not really necessary */.
END IF.
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

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

ViAnn Beadle
In reply to this post by Richard Ristow
Very nice

-----Original Message-----
From: Richard Ristow [mailto:[hidden email]]
Sent: Wednesday, September 17, 2008 2:18 PM
To: Keval Khichadia; [hidden email]
Cc: ViAnn Beadle
Subject: Re: substring

At 03:03 PM 9/16/2008, Keval Khichadia wrote:

>I have a string variable coursekey (a10) which contains both
>characters and numbers (i.e NURS5555). I would like to only keep the
>numerical part, but the numerical part does not always start at the
>5th position.

And at 05:58 PM 9/16/2008, expanded:

>In all cases I want to keep everything to the right of the first
>number.  Letters are not usually interspered with numbers,  but
>there are some cases like (BIO 18B), where I would only want to keep the
18B.

At 11:57 PM 9/16/2008, ViAnn Beadle wrote:

>This is one way to do it but has a kludgy side effect described below:
>...
>do if not(sysmis(number(substr(stringvar,#i,1),f1))) and hit=0.
>...
>
>The number function is used to try to convert the character. If the
>character is not a number the result is system-missing. This usually
>generates a warning in the output

You can also test whether a character is a digit, without raising a
warning, using INDEX (not tested):

>do if not (INDEX('0123456789'substr(stringvar,#i,1))) and hit=0.

But there's also a 'cute' solution without a loop, using the
rarely-used third argument to INDEX:

STRING CharPart NumPart (A12).
COMPUTE #FirstDig = INDEX(stringvar,'0123456789',1).
DO IF   #FirstDig GT 0.
.  COMPUTE CharPart = SUBSTR(stringvar,1,#FirstDig-1).
.  COMPUTE NumPart  = SUBSTR(stringvar,  #FirstDig).
ELSE.
.  COMPUTE CharPart = stringvar.
.  COMPUTE NumPart  = ''    /* Not really necessary */.
END IF.
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |17-SEP-2008 16:14:09       |
|-----------------------------|---------------------------|
[Cutey]
stringvar    CharPart     NumPart

abc124b      abc          124b
1bbbbbc                   1bbbbbc
aaad1123     aaad         1123
1bbb3vv                   1bbb3vv
abc1abc1     abc          1abc1
1234567890                1234567890
abc 1def     abc          1def
1a2b3c4d53                1a2b3c4d53
POSTER       POSTER
SPAN1222     SPAN         1222
SCS 1234     SCS          1234
BIO 18B      BIO          18B
CP 5155      CP           5155

Number of cases read:  13    Number of cases listed:  13
=============================
APPENDIX: Test data, and code
=============================
set mxwarns 0.
data list fixed /stringvar 1-12 (a).
begin data
abc124b
1bbbbbc
aaad1123
1bbb3vv
abc1abc1
1234567890
abc 1def
1a2b3c4d53
POSTER
SPAN1222
SCS 1234
BIO 18B
CP 5155
end data.
DATASET NAME     TestData.
DATASET COPY     VBeadle.
DATASET ACTIVATE VBeadle WINDOW=FRONT.
string newstring (a12).
compute hit=0.
loop #i=1 to 12.
do if not(sysmis(number(substr(stringvar,#i,1),f1))) and hit=0.
compute newstring=substr(stringvar,#i).
compute hit=1.
end if.
end loop.
list var stringvar newstring.

DATASET ACTIVATE TestData.
DATASET COPY     Cutey.
DATASET ACTIVATE Cutey    WINDOW=FRONT.

STRING CharPart NumPart (A12).
COMPUTE #FirstDig = INDEX(stringvar,'0123456789',1).
DO IF   #FirstDig GT 0.
.  COMPUTE CharPart = SUBSTR(stringvar,1,#FirstDig-1).
.  COMPUTE NumPart  = SUBSTR(stringvar,  #FirstDig).
ELSE.
.  COMPUTE CharPart = stringvar.
.  COMPUTE NumPart  = ''    /* Not really necessary */.
END IF.
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
Reply | Threaded
Open this post in threaded view
|

Character, ASCII, hex (was, re: substring)

Richard Ristow
In reply to this post by Maguin, Eugene
(Cf. thread "Substring", begun Tue, 16 Sep 2008 12:03:01 -0700)

At 04:17 PM 9/17/2008, Gene Maguin wrote:

>Wouldn't a way to get around the warning problem be to test whether
>the character is either between '0' and '9' in the sort order or
>less than 'A' in the sort order.

Right. Jon Peck posted a 'RANGE' solution like this. I like the
'third argument to INDEX' solution, because it makes INDEX handle
*both* loops -- through the string, and through the list of test
characters -- in one call.

>Isn't there also a way to convert a character to an ascii number
>using some odd format and test that against some value. It seems
>like Richard was an expert with this.

I don't think it would make this problem any easier, but, by popular
demand (you're popular, Gene), here's code to convert back and forth
between characters, ASCII numeric values, and hex. Methods are
originally from Raynald Levesque.

. To get the ASCII numeric value of a character, convert it using
function NUMBER and format PIB1.
. To go the other way, from an ASCII numeric value to the
corresponding character, use function STRING, with format PIB1.
. To get the hex for an ASCII numeric value as a two-character
string, use function STRING with format PIBHEX02.

>*  I.  Get numeric code from characters, .
>*      and characters from numeric.      .
>GET FILE=TestChar.
>
>NUMERIC ASCII    (F3)
>        /ASCIIhex (PIBHEX02).
>STRING  HEX      (A3).
>STRING  RECOVER  (A1).
>
>COMPUTE ASCII     = NUMBER(CHAR,PIB1).
>COMPUTE ASCIIhex  = ASCII.
>COMPUTE HEX       = CONCAT('x',STRING(ASCII,PIBHEX02)).
>COMPUTE RECOVER   = STRING(ASCII,PIB1).
>
>LIST.
>|-----------------------------|---------------------------|
>|Output Created               |31-JAN-2007 21:08:21       |
>|-----------------------------|---------------------------|
>[TestChar]
>C:\Documents and Settings\Richard\My Documents\Temporary\SPSS
>   \2007-01-31 Hynes - Deleting embedded control characters
>TestChar.Sav
>
>CHAR ASCII ASCIIhex HEX RECOVER
>
>L      76     4C    x4C L
>O      79     4F    x4F O
>V      86     56    x56 V
>E      69     45    x45 E
>        32     20    x20
>1      49     31    x31 1
>4      52     34    x34 4
>
>Number of cases read:  7    Number of cases listed:  7

 From posting
Date:    Wed, 31 Jan 2007 23:27:49 -0500
From:    Richard Ristow <[hidden email]>
Subject: Re: Deleting embedded control characters
To:      [hidden email]

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

Hal 9000
In reply to this post by ViAnn Beadle
I prefer Richard's cute solution. It's more to the point. The poster
refers to variations of a common academic naming convention that all
here should be very familiar with!

The answer has only 3 lines of codes (including the dreaded ".exe").

Most Sincerely,
-Gary

;)


On Wed, Sep 17, 2008 at 1:34 PM, ViAnn Beadle <[hidden email]> wrote:

> Very nice
>
> -----Original Message-----
> From: Richard Ristow [mailto:[hidden email]]
> Sent: Wednesday, September 17, 2008 2:18 PM
> To: Keval Khichadia; [hidden email]
> Cc: ViAnn Beadle
> Subject: Re: substring
>
> At 03:03 PM 9/16/2008, Keval Khichadia wrote:
>
>>I have a string variable coursekey (a10) which contains both
>>characters and numbers (i.e NURS5555). I would like to only keep the
>>numerical part, but the numerical part does not always start at the
>>5th position.
>
> And at 05:58 PM 9/16/2008, expanded:
>
>>In all cases I want to keep everything to the right of the first
>>number.  Letters are not usually interspered with numbers,  but
>>there are some cases like (BIO 18B), where I would only want to keep the
> 18B.
>
> At 11:57 PM 9/16/2008, ViAnn Beadle wrote:
>
>>This is one way to do it but has a kludgy side effect described below:
>>...
>>do if not(sysmis(number(substr(stringvar,#i,1),f1))) and hit=0.
>>...
>>
>>The number function is used to try to convert the character. If the
>>character is not a number the result is system-missing. This usually
>>generates a warning in the output
>
> You can also test whether a character is a digit, without raising a
> warning, using INDEX (not tested):
>
>>do if not (INDEX('0123456789'substr(stringvar,#i,1))) and hit=0.
>
> But there's also a 'cute' solution without a loop, using the
> rarely-used third argument to INDEX:
>
> STRING CharPart NumPart (A12).
> COMPUTE #FirstDig = INDEX(stringvar,'0123456789',1).
> DO IF   #FirstDig GT 0.
> .  COMPUTE CharPart = SUBSTR(stringvar,1,#FirstDig-1).
> .  COMPUTE NumPart  = SUBSTR(stringvar,  #FirstDig).
> ELSE.
> .  COMPUTE CharPart = stringvar.
> .  COMPUTE NumPart  = ''    /* Not really necessary */.
> END IF.
> LIST.
>
> List
> |-----------------------------|---------------------------|
> |Output Created               |17-SEP-2008 16:14:09       |
> |-----------------------------|---------------------------|
> [Cutey]
> stringvar    CharPart     NumPart
>
> abc124b      abc          124b
> 1bbbbbc                   1bbbbbc
> aaad1123     aaad         1123
> 1bbb3vv                   1bbb3vv
> abc1abc1     abc          1abc1
> 1234567890                1234567890
> abc 1def     abc          1def
> 1a2b3c4d53                1a2b3c4d53
> POSTER       POSTER
> SPAN1222     SPAN         1222
> SCS 1234     SCS          1234
> BIO 18B      BIO          18B
> CP 5155      CP           5155
>
> Number of cases read:  13    Number of cases listed:  13
> =============================
> APPENDIX: Test data, and code
> =============================
> set mxwarns 0.
> data list fixed /stringvar 1-12 (a).
> begin data
> abc124b
> 1bbbbbc
> aaad1123
> 1bbb3vv
> abc1abc1
> 1234567890
> abc 1def
> 1a2b3c4d53
> POSTER
> SPAN1222
> SCS 1234
> BIO 18B
> CP 5155
> end data.
> DATASET NAME     TestData.
> DATASET COPY     VBeadle.
> DATASET ACTIVATE VBeadle WINDOW=FRONT.
> string newstring (a12).
> compute hit=0.
> loop #i=1 to 12.
> do if not(sysmis(number(substr(stringvar,#i,1),f1))) and hit=0.
> compute newstring=substr(stringvar,#i).
> compute hit=1.
> end if.
> end loop.
> list var stringvar newstring.
>
> DATASET ACTIVATE TestData.
> DATASET COPY     Cutey.
> DATASET ACTIVATE Cutey    WINDOW=FRONT.
>
> STRING CharPart NumPart (A12).
> COMPUTE #FirstDig = INDEX(stringvar,'0123456789',1).
> DO IF   #FirstDig GT 0.
> .  COMPUTE CharPart = SUBSTR(stringvar,1,#FirstDig-1).
> .  COMPUTE NumPart  = SUBSTR(stringvar,  #FirstDig).
> ELSE.
> .  COMPUTE CharPart = stringvar.
> .  COMPUTE NumPart  = ''    /* Not really necessary */.
> END IF.
> 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
>

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