Mysteries of CHAR.SUBSTR

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

Mysteries of CHAR.SUBSTR

Robert Walker
Hi Listers,

I am attempting to grab text up until a semi-colon in string variable "Name3". The new variable, "Name3_1", has been assigned an adequate width.

This line of syntax produces an error/warning that the third argument is invalid...

COMPUTE Name3_1 = CHAR.SUBSTR(Name3,1,CHAR.INDEX(Name3,";")-1).

...while this line of code does not (and variable "NoThird" is fully populated with either a "0" or "1"):

IF (NoThird=0 OR NoThird=1) Name3_1 = CHAR.SUBSTR(Name3,1,CHAR.INDEX(Name3,";")-1).

Both statements are followed by EXECUTE.

Aside from my brain, what am I missing here?

Thanks,

Bob Walker
Surveys & Forecasts, LLC
www.safllc.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
Reply | Threaded
Open this post in threaded view
|

Re: Mysteries of CHAR.SUBSTR

Maguin, Eugene
There some gaps in the problem presentation that would be useful to know about.
I assume that you sometimes get the error when using the first statement and that this "Jones, George;who" does not yield an error message but this "Wills, Bob" or "Wills, Bob,yeah him" does yield an error because index returns a value of 0 if the character is not found and 0-1 is a problem for char.substr. As "" or ";" or "," would be.

It'd be nice to know how NoThird is computed.

Gene Maguin



-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Bob Walker
Sent: Friday, October 23, 2015 4:00 PM
To: [hidden email]
Subject: Mysteries of CHAR.SUBSTR

Hi Listers,

I am attempting to grab text up until a semi-colon in string variable "Name3". The new variable, "Name3_1", has been assigned an adequate width.

This line of syntax produces an error/warning that the third argument is invalid...

COMPUTE Name3_1 = CHAR.SUBSTR(Name3,1,CHAR.INDEX(Name3,";")-1).

...while this line of code does not (and variable "NoThird" is fully populated with either a "0" or "1"):

IF (NoThird=0 OR NoThird=1) Name3_1 = CHAR.SUBSTR(Name3,1,CHAR.INDEX(Name3,";")-1).

Both statements are followed by EXECUTE.

Aside from my brain, what am I missing here?

Thanks,

Bob Walker
Surveys & Forecasts, LLC
www.safllc.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: Mysteries of CHAR.SUBSTR

Bruce Weaver
Administrator
I agree with Gene--it would be helpful to have a small dataset that illustrates the problem.  Using the very small dataset I created (see below), both of your methods of computing Name_3 are working just fine (using SPSS 23.0.0.2, 32-bit version, Windows 7 Professional, SP2).  HTH.

DATA LIST list / Name3 (A35).
BEGIN DATA
"Bob Walker; Surveys & Forecasts"
"Gene Maguin; UB"
"Bruce Weaver; Lakehead U"
END DATA.

COMPUTE NoThird = MOD($casenum,2).
FORMATS NoThird(F1).
LIST.

STRING Name3_1v1 Name3_1v2 (A25).
COMPUTE Name3_1v1 = CHAR.SUBSTR(Name3,1,CHAR.INDEX(Name3,";")-1).
IF (NoThird=0 OR NoThird=1) Name3_1v2 = CHAR.SUBSTR(Name3,1,CHAR.INDEX(Name3,";")-1).
LIST.

Output from final LIST:

Name3                               NoThird Name3_1v1                 Name3_1v2
 
Bob Walker; Surveys & Forecasts        1    Bob Walker                Bob Walker
Gene Maguin; UB                        0    Gene Maguin               Gene Maguin
Bruce Weaver; Lakehead U               1    Bruce Weaver              Bruce Weaver
 
Number of cases read:  3    Number of cases listed:  3

Maguin, Eugene wrote
There some gaps in the problem presentation that would be useful to know about.
I assume that you sometimes get the error when using the first statement and that this "Jones, George;who" does not yield an error message but this "Wills, Bob" or "Wills, Bob,yeah him" does yield an error because index returns a value of 0 if the character is not found and 0-1 is a problem for char.substr. As "" or ";" or "," would be.

It'd be nice to know how NoThird is computed.

Gene Maguin



-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Bob Walker
Sent: Friday, October 23, 2015 4:00 PM
To: [hidden email]
Subject: Mysteries of CHAR.SUBSTR

Hi Listers,

I am attempting to grab text up until a semi-colon in string variable "Name3". The new variable, "Name3_1", has been assigned an adequate width.

This line of syntax produces an error/warning that the third argument is invalid...

COMPUTE Name3_1 = CHAR.SUBSTR(Name3,1,CHAR.INDEX(Name3,";")-1).

...while this line of code does not (and variable "NoThird" is fully populated with either a "0" or "1"):

IF (NoThird=0 OR NoThird=1) Name3_1 = CHAR.SUBSTR(Name3,1,CHAR.INDEX(Name3,";")-1).

Both statements are followed by EXECUTE.

Aside from my brain, what am I missing here?

Thanks,

Bob Walker
Surveys & Forecasts, LLC
www.safllc.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
--
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: Mysteries of CHAR.SUBSTR

Robert Walker
Thanks guys,

Your comments forced me to go back and look more closely at the raw data file (always a good idea). I found the source of the discrepancy.

I was using SAMPLE to test my CHAR.SUBSTR syntax, as the base file was very large. There were only a handful of occurrences in the full file without a second semi-colon. Using SAMPLE did not always capture one of those oddball records.

I've created some IF-THEN logic to avoid the error and that seems to have solved the dilemma.

Many thanks!!

Bob Walker
Surveys & Forecasts, LLC
www.safllc.com

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Bruce Weaver
Sent: Friday, October 23, 2015 5:58 PM
To: [hidden email]
Subject: Re: Mysteries of CHAR.SUBSTR

I agree with Gene--it would be helpful to have a small dataset that illustrates the problem.  Using the very small dataset I created (see below), both of your methods of computing Name_3 are working just fine (using SPSS 23.0.0.2, 32-bit version, Windows 7 Professional, SP2).  HTH.

DATA LIST list / Name3 (A35).
BEGIN DATA
"Bob Walker; Surveys & Forecasts"
"Gene Maguin; UB"
"Bruce Weaver; Lakehead U"
END DATA.

COMPUTE NoThird = MOD($casenum,2).
FORMATS NoThird(F1).
LIST.

STRING Name3_1v1 Name3_1v2 (A25).
COMPUTE Name3_1v1 = CHAR.SUBSTR(Name3,1,CHAR.INDEX(Name3,";")-1).
IF (NoThird=0 OR NoThird=1) Name3_1v2 =
CHAR.SUBSTR(Name3,1,CHAR.INDEX(Name3,";")-1).
LIST.

Output from final LIST:

Name3                               NoThird Name3_1v1                
Name3_1v2
 
Bob Walker; Surveys & Forecasts        1    Bob Walker                Bob
Walker
Gene Maguin; UB                        0    Gene Maguin               Gene
Maguin
Bruce Weaver; Lakehead U               1    Bruce Weaver              Bruce
Weaver
 
Number of cases read:  3    Number of cases listed:  3


Maguin, Eugene wrote

> There some gaps in the problem presentation that would be useful to
> know about.
> I assume that you sometimes get the error when using the first
> statement and that this "Jones, George;who" does not yield an error
> message but this "Wills, Bob" or "Wills, Bob,yeah him" does yield an
> error because index returns a value of 0 if the character is not found
> and 0-1 is a problem for char.substr. As "" or ";" or "," would be.
>
> It'd be nice to know how NoThird is computed.
>
> Gene Maguin
>
>
>
> -----Original Message-----
> From: SPSSX(r) Discussion [mailto:

> SPSSX-L@.UGA

> ] On Behalf Of Bob Walker
> Sent: Friday, October 23, 2015 4:00 PM
> To:

> SPSSX-L@.UGA

> Subject: Mysteries of CHAR.SUBSTR
>
> Hi Listers,
>
> I am attempting to grab text up until a semi-colon in string variable
> "Name3". The new variable, "Name3_1", has been assigned an adequate width.
>
> This line of syntax produces an error/warning that the third argument
> is invalid...
>
> COMPUTE Name3_1 = CHAR.SUBSTR(Name3,1,CHAR.INDEX(Name3,";")-1).
>
> ...while this line of code does not (and variable "NoThird" is fully
> populated with either a "0" or "1"):
>
> IF (NoThird=0 OR NoThird=1) Name3_1 =
> CHAR.SUBSTR(Name3,1,CHAR.INDEX(Name3,";")-1).
>
> Both statements are followed by EXECUTE.
>
> Aside from my brain, what am I missing here?
>
> Thanks,
>
> Bob Walker
> Surveys & Forecasts, LLC
> www.safllc.com
>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

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

> LISTSERV@.UGA

>  (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
[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/Mysteries-of-CHAR-SUBSTR-tp5730850p5730855.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