Syntax not working

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

Syntax not working

FernandezLanier, Adriana (DCJS)

Good morning. For the last few days I’ve been trying to figure out why the syntax below does not work. My colleague wrote the syntax to do the following “Translated this means if the last (9th) character of a right justified left padded variable is numeric then take the first 9 characters else

Take the first eight characters.”  Typically we use an identifier variable (NYSID) that includes 8 numeric digits and the last is an alpha (e.g 23882939L). It’s much easier to drop the alpha digit and work with a numeric number. However, it’s possible that the identifier can vary in length as well as not include the last alpha (agencies forget). My friend wrote the syntax below to account for all this…but I keep getting the following error (see below).  Any suggestions would be greatly appreciated. Thanks, A

 

 

DO IF char.substr(char.LPAD(NYSID,9),9) in ("1","2","3","4","5","6","7","8","9","0")

COMPUTE VAR00005=char.substr(CHAR.LPAD(NYSID,1,9))

else

COMPUTE VAR00005=char.substr(CHAR.LPAD(NYSID,1,8))

End if.

 

 

Error # 4205.  Command name: DO IF

The syntax calls for a logical expression but a numeric or character

expression was found.

This command not executed.

 

 

P Please consider the environment before printing this e-mail.

 



This e-mail, including any attachments, may be confidential, privileged or otherwise legally protected. It is intended only for the addressee. If you received this e-mail in error or from someone who was not authorized to send it to you, do not disseminate, copy or otherwise use this e-mail or its attachments. Please notify the sender immediately by reply e-mail and delete the e-mail from your system.
Reply | Threaded
Open this post in threaded view
|

Re: Syntax not working

Maguin, Eugene
The problem is in the Do if statement, thats what the error message says. I
think you should be using the Any function. I understand what your colleague
means but spss won't because there is no In function in spss. Look at the
Any and Range functions in the sytax reference (page 114 in V17).
This is untested but this is what I'd start with and see what happens.


Do if
(any(char.substr(char.LPAD(NYSID,9),9),"1","2","3","4","5","6","7","8","9","
0")).

Improvements (assuming I understand your data structure).
First, I think you may be able to use Range instead of Any where the low
value is '0' and the high value is '9'. Note the little note about 'national
collating sequence' in the syntax ref and which is undefined (and with no
reference to it).

Second, as I understand your data, the value is either going to be
'123456789' or '12345678A'. If so, then this will work:

Do if (any(char.substr(NYSID,1,9),"1","2","3","4","5","6","7","8","9","0")).

Actually, if your data are right justified so that the left characters are
blanks if the NYSID is less than 9 chars, the Lpad is also not needed.

Gene Maguin

PS. I don't think the Lpad is needed at all in any of the compute
statements.



>>Good morning. For the last few days I've been trying to figure out why the
syntax below does not work. My colleague wrote the syntax to do the
following "Translated this means if the last (9th) character of a right
justified left padded variable is numeric then take the first 9 characters
else

Take the first eight characters."  Typically we use an identifier variable
(NYSID) that includes 8 numeric digits and the last is an alpha (e.g
23882939L). It's much easier to drop the alpha digit and work with a numeric
number. However, it's possible that the identifier can vary in length as
well as not include the last alpha (agencies forget). My friend wrote the
syntax below to account for all this.but I keep getting the following error
(see below).  Any suggestions would be greatly appreciated. Thanks, A





DO IF char.substr(char.LPAD(NYSID,9),9) in
("1","2","3","4","5","6","7","8","9","0")

COMPUTE VAR00005=char.substr(CHAR.LPAD(NYSID,1,9))

else

COMPUTE VAR00005=char.substr(CHAR.LPAD(NYSID,1,8))

End if.





Error # 4205.  Command name: DO IF

The syntax calls for a logical expression but a numeric or character

expression was found.

This command not executed.





P Please consider the environment before printing this e-mail.




________________________________

This e-mail, including any attachments, may be confidential, privileged or
otherwise legally protected. It is intended only for the addressee. If you
received this e-mail in error or from someone who was not authorized to send
it to you, do not disseminate, copy or otherwise use this e-mail or its
attachments. Please notify the sender immediately by reply e-mail and delete
the e-mail from your system.

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

Ruben Geert van den Berg
In reply to this post by FernandezLanier, Adriana (DCJS)
Dear Adriana,

If the non numeric character is always in the 9th. position, then the two syntax examples below should work. I guess RANGE could be used as well (but I forgot how) as long as you convert the 9th. character to numeric as in the second example.

HTH,

Ruben van den Berg

Methodologist

TNS NIPO

E: [hidden email]

P: +31 20 522 5738

I: www.tns-nipo.com


*Create test data.

data list free/testvar(a12).
begin data
23882939L
12345678
123456789
end data.

*Example 1, with ANY.

do if any(sub(testvar,9,1), "1","2","3","4","5","6","7","8","9","0").
comp newvar=num(sub(testvar,1,8),f8).
else.
comp newvar=num(testvar,f8).
end if.
exe.

*Example 2, any non numeric character will be system missing if converted to numeric.

do if mis(num(sub(testvar,9,1)),f1)=1.
comp newvar2=num(sub(testvar,1,8),f8).
else.
comp newvar2=num(testvar,f8).
end if.
exe.




Date: Mon, 26 Apr 2010 10:27:36 -0400
From: [hidden email]
Subject: Syntax not working
To: [hidden email]

Good morning. For the last few days I’ve been trying to figure out why the syntax below does not work. My colleague wrote the syntax to do the following “Translated this means if the last (9th) character of a right justified left padded variable is numeric then take the first 9 characters else

Take the first eight characters.”  Typically we use an identifier variable (NYSID) that includes 8 numeric digits and the last is an alpha (e.g 23882939L). It’s much easier to drop the alpha digit and work with a numeric number. However, it’s possible that the identifier can vary in length as well as not include the last alpha (agencies forget). My friend wrote the syntax below to account for all this…but I keep getting the following error (see below).  Any suggestions would be greatly appreciated. Thanks, A

 

 

DO IF char.substr(char.LPAD(NYSID,9),9) in ("1","2","3","4","5","6","7","8","9","0")

COMPUTE VAR00005=char.substr(CHAR.LPAD(NYSID,1,9))

else

COMPUTE VAR00005=char.substr(CHAR.LPAD(NYSID,1,8))

End if.

 

 

Error # 4205.  Command name: DO IF

The syntax calls for a logical expression but a numeric or character

expression was found.

This command not executed.

 

 

P Please consider the environment before printing this e-mail.

 



This e-mail, including any attachments, may be confidential, privileged or otherwise legally protected. It is intended only for the addressee. If you received this e-mail in error or from someone who was not authorized to send it to you, do not disseminate, copy or otherwise use this e-mail or its attachments. Please notify the sender immediately by reply e-mail and delete the e-mail from your system.


Express yourself instantly with MSN Messenger! MSN Messenger
Reply | Threaded
Open this post in threaded view
|

Correction (RE: Syntax not working)

Ruben Geert van den Berg
In reply to this post by FernandezLanier, Adriana (DCJS)
I apologize, the syntax I posted previously didn't turn out to work. Just not my lucky day I guess... Please find the corrected version below.

HTH,

Ruben van den Berg

Methodologist

TNS NIPO

E: [hidden email]

P: +31 20 522 5738

I: www.tns-nipo.com


data list free/testvar(a12).
begin data
23882939L
12345678
123456789
end data.

*Example 1, with ANY.

do if not(any(sub(testvar,9,1), "1","2","3","4","5","6","7","8","9","0")).
comp newvar=num(con('0',sub(testvar,1,8)),f9).
else.
comp newvar=num(testvar,f9).
end if.
exe.

*Example 2, any non numeric character will be system missing if converted to numeric (with an ignorable warning, though).

do if mis(num(sub(testvar,9,1),f1))=1.
comp newvar2=num(con('0',sub(testvar,1,8)),f9).
else.
comp newvar2=num(testvar,f9).
end if.
exe.





Date: Mon, 26 Apr 2010 10:27:36 -0400
From: [hidden email]
Subject: Syntax not working
To: [hidden email]

Good morning. For the last few days I’ve been trying to figure out why the syntax below does not work. My colleague wrote the syntax to do the following “Translated this means if the last (9th) character of a right justified left padded variable is numeric then take the first 9 characters else

Take the first eight characters.”  Typically we use an identifier variable (NYSID) that includes 8 numeric digits and the last is an alpha (e.g 23882939L). It’s much easier to drop the alpha digit and work with a numeric number. However, it’s possible that the identifier can vary in length as well as not include the last alpha (agencies forget). My friend wrote the syntax below to account for all this…but I keep getting the following error (see below).  Any suggestions would be greatly appreciated. Thanks, A

 

 

DO IF char.substr(char.LPAD(NYSID,9),9) in ("1","2","3","4","5","6","7","8","9","0")

COMPUTE VAR00005=char.substr(CHAR.LPAD(NYSID,1,9))

else

COMPUTE VAR00005=char.substr(CHAR.LPAD(NYSID,1,8))

End if.

 

 

Error # 4205.  Command name: DO IF

The syntax calls for a logical expression but a numeric or character

expression was found.

This command not executed.

 

 

P Please consider the environment before printing this e-mail.

 



This e-mail, including any attachments, may be confidential, privileged or otherwise legally protected. It is intended only for the addressee. If you received this e-mail in error or from someone who was not authorized to send it to you, do not disseminate, copy or otherwise use this e-mail or its attachments. Please notify the sender immediately by reply e-mail and delete the e-mail from your system.


New Windows 7: Find the right PC for you. Learn more.
Reply | Threaded
Open this post in threaded view
|

Re: Syntax not working

hillel vardi
In reply to this post by FernandezLanier, Adriana (DCJS)
Shalom

Here is anther way to handle your problem using inly the substr function .

data list free/testvar(a12).
begin data
23882939L
12345678
123456789
end data.

do if(substr(testvar,9,1) ge 'A').
comp newvar=num(con('0',sub(testvar,1,8)),f9).
else.
comp newvar=num(testvar,f9).
end if.
exe.

Hillel Vardi
BGU


FernandezLanier, Adriana (DCJS) wrote:

>
> Good morning. For the last few days I’ve been trying to figure out why
> the syntax below does not work. My colleague wrote the syntax to do
> the following “Translated this means if the last (9^th ) character of
> a right justified left padded variable is numeric then take the first
> 9 characters else
>
> Take the first eight characters.” Typically we use an identifier
> variable (NYSID) that includes 8 numeric digits and the last is an
> alpha (e.g 23882939L). It’s much easier to drop the alpha digit and
> work with a numeric number. However, it’s possible that the identifier
> can vary in length as well as not include the last alpha (agencies
> forget). My friend wrote the syntax below to account for all this…but
> I keep getting the following error (see below). Any suggestions would
> be greatly appreciated. Thanks, A
>
> DO IF char.substr(char.LPAD(NYSID,9),9) in
> ("1","2","3","4","5","6","7","8","9","0")
>
> COMPUTE VAR00005=char.substr(CHAR.LPAD(NYSID,1,9))
>
> else
>
> COMPUTE VAR00005=char.substr(CHAR.LPAD(NYSID,1,8))
>
> End if.
>
> Error # 4205. Command name: DO IF
>
> The syntax calls for a logical expression but a numeric or character
>
> expression was found.
>
> This command not executed.
>
> P Please consider the environment before printing this e-mail.
>
>
> ------------------------------------------------------------------------
> This e-mail, including any attachments, may be confidential,
> privileged or otherwise legally protected. It is intended only for the
> addressee. If you received this e-mail in error or from someone who
> was not authorized to send it to you, do not disseminate, copy or
> otherwise use this e-mail or its attachments. Please notify the sender
> immediately by reply e-mail and delete the e-mail from your system.

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

Richard Ristow
In reply to this post by FernandezLanier, Adriana (DCJS)
At 10:27 AM 4/26/2010, FernandezLanier, Adriana (DCJS) wrote:

I’ve been trying to figure out why the syntax below does not work.
 
DO IF char.substr(char.LPAD(NYSID,9),9) in ("1","2","3","4","5","6","7","8","9","0")
COMPUTE VAR00005=char.substr(CHAR.LPAD(NYSID,1,9))
else
COMPUTE VAR00005=char.substr(CHAR.LPAD(NYSID,1,8))
End if.

You've had good advice on the right way to get the effects you want. As for why this code doesn't work, see what Gene Maguin wrote about "IN".  "IN", used the way your colleague wrote it, is an operator in SAS, but not in SPSS. As Gene said, the corresponding SPSS syntax is,

DO IF ANY(char.substr(char.LPAD(NYSID,9),9),"1","2","3","4","5","6","7","8","9","0").

though, as David Marso wrote, INDEX does it much better:

DO IF INDEX("1234567890",char.substr(char.LPAD(NYSID,9),9)) NE 0.

If that were fixed, though, I think your colleague's code would still have problems with delimiting commands. There are two sets of syntax rules for delimiting SPSS commands (from the Command Syntax Reference (chapter "Universals", section "Commands),

Interactive Mode
  • Each command should end with a period as a command terminator.
  • The command terminator must be the last nonblank character in a command.
  • In the absence of a period as the command terminator, a blank line is interpreted as a command terminator.
  • Commands can begin in any column of a command line and continue for as many lines as needed.
Batch (Production) Mode
  • All commands in the command file must begin in column 1. You can use [periods (.)], or plus (+) or minus (–) signs in the first column if you want to indent the command specification
  • If multiple lines are used for a command, column 1 of each continuation line must be blank.
Your colleague's syntax (corrected) would probably work in batch mode, but in interactive mode (if run from a syntax window),

DO IF INDEX("1234567890",char.substr(char.LPAD(NYSID,9),9)) NE 0
COMPUTE VAR00005=char.substr(CHAR.LPAD(NYSID,1,9))
else
COMPUTE VAR00005=char.substr(CHAR.LPAD(NYSID,1,8))
End if.

is one long statement -- an invalid one, of course. Terminate commands with periods:

DO IF INDEX("1234567890",char.substr(char.LPAD(NYSID,9),9)) NE 0.
COMPUTE VAR00005=char.substr(CHAR.LPAD(NYSID,1,9)).
else.
COMPUTE VAR00005=char.substr(CHAR.LPAD(NYSID,1,8)).
End if.

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

Richard Ristow
At 10:04 AM 4/30/2010, FernandezLanier, Adriana (DCJS) wrote, off-list:

So far I haven’t had any luck with anyone’s suggestions.  I think it’s mainly because the original variable, “NYSID”, is a string and can vary in length [emphasis added](see below).  Any other suggestions would be greatly appreciated.

This differs from the requested effect originally posted, namely "if the last (9th) character of a right justified left padded variable is numeric then take the first 9 characters else take the first eight characters." [emphasis also added] Respondents, of course, answered the question as posted.

If I now understand correctly, that the desideratum is to remove the last character of the string if it is not a digit, there are plenty of ways, of which this is one (tested):

|-----------------------------|---------------------------|
|Output Created               |06-MAY-2010 12:13:48       |
|-----------------------------|---------------------------|
NYSID

00065063N
0101521J
0106126R
1234567890
15A

Number of cases read:  5    Number of cases listed:  5

 
STRING  EditedVar(A12).
COMPUTE EditedVar  =LTRIM(NYSID).
COMPUTE #ItsLength =LENGTH(RTRIM(EditedVar)).
IF   INDEX("0123456789",SUBSTR(EditedVar,  #ItsLength,1)) EQ 0
        EditedVar     = SUBSTR(EditedVar,1,#ItsLength-1).
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |06-MAY-2010 12:13:48       |
|-----------------------------|---------------------------|
NYSID        EditedVar

00065063N    00065063
0101521J     0101521
0106126R     0106126
1234567890   1234567890
15A          15

Number of cases read:  5    Number of cases listed:  5
=============================
APPENDIX: Test data, and code
=============================
DATA LIST LIST/
   NYSID
   (A12).
BEGIN DATA  
   00065063N
   0101521J
   0106126R
   1234567890
   15A
END DATA.
LIST.

STRING  EditedVar(A12).
COMPUTE EditedVar  =LTRIM(NYSID).
COMPUTE #ItsLength =LENGTH(RTRIM(EditedVar)).
IF   INDEX("0123456789",SUBSTR(EditedVar,  #ItsLength,1)) EQ 0
        EditedVar     = SUBSTR(EditedVar,1,#ItsLength-1).
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
|

Automatic reply: Syntax not working

Genevieve Odoom

Hello,
Thank you for your email. I will be out of the office on Thursday, May 6th, returning on Monday, May 10th and will respond to your email upon my return.

Thanks!
Genevieve Odoom
Policy and Program Analyst
OANHSS
Suite 700 - 7050 Weston Rd. Woodbridge,
ON L4L 8G7
Tel: (905) 851-8821 x 241 Fax: (905) 851-0744
[hidden email]
www.oanhss.org<https://mail.oanhss.org/ecp/Organize/www.oanhss.org>

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

FernandezLanier, Adriana (DCJS)
In reply to this post by Richard Ristow

Your syntax worked successfully. Thank you so much. I will share it with my colleagues.

 

P Please consider the environment before printing this e-mail.

 

From: Richard Ristow [mailto:[hidden email]]
Sent: Thursday, May 06, 2010 12:17 PM
To: FernandezLanier, Adriana (DCJS); SPSS discussion list
Subject: RE: Syntax not working

 

At 10:04 AM 4/30/2010, FernandezLanier, Adriana (DCJS) wrote, off-list:


So far I haven’t had any luck with anyone’s suggestions.  I think it’s mainly because the original variable, “NYSID”, is a string and can vary in length [emphasis added](see below).  Any other suggestions would be greatly appreciated.


This differs from the requested effect originally posted, namely "if the last (9th) character of a right justified left padded variable is numeric then take the first 9 characters else take the first eight characters." [emphasis also added] Respondents, of course, answered the question as posted.

If I now understand correctly, that the desideratum is to remove the last character of the string if it is not a digit, there are plenty of ways, of which this is one (tested):

|-----------------------------|---------------------------|
|Output Created               |06-MAY-2010 12:13:48       |
|-----------------------------|---------------------------|
NYSID

00065063N
0101521J
0106126R
1234567890
15A

Number of cases read:  5    Number of cases listed:  5

 
STRING  EditedVar(A12).
COMPUTE EditedVar  =LTRIM(NYSID).
COMPUTE #ItsLength =LENGTH(RTRIM(EditedVar)).
IF   INDEX("0123456789",SUBSTR(EditedVar,  #ItsLength,1)) EQ 0
        EditedVar     = SUBSTR(EditedVar,1,#ItsLength-1).
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |06-MAY-2010 12:13:48       |
|-----------------------------|---------------------------|
NYSID        EditedVar

00065063N    00065063
0101521J     0101521
0106126R     0106126
1234567890   1234567890
15A          15

Number of cases read:  5    Number of cases listed:  5
=============================
APPENDIX: Test data, and code
=============================
DATA LIST LIST/
   NYSID
   (A12).
BEGIN DATA  
   00065063N
   0101521J
   0106126R
   1234567890
   15A
END DATA.
LIST.

STRING  EditedVar(A12).
COMPUTE EditedVar  =LTRIM(NYSID).
COMPUTE #ItsLength =LENGTH(RTRIM(EditedVar)).
IF   INDEX("0123456789",SUBSTR(EditedVar,  #ItsLength,1)) EQ 0
        EditedVar     = SUBSTR(EditedVar,1,#ItsLength-1).
LIST.

 



This e-mail, including any attachments, may be confidential, privileged or otherwise legally protected. It is intended only for the addressee. If you received this e-mail in error or from someone who was not authorized to send it to you, do not disseminate, copy or otherwise use this e-mail or its attachments. Please notify the sender immediately by reply e-mail and delete the e-mail from your system.