I've searched high and low and read all of the syntax reference documents with no success,
I know you can replace a string within a variable via REPLACE (page 337 of the syntax ref that I have) But I can't find any reference to a way of using part of a string variable as an argument in a do if statement. Essentially I'm trying to achieve what this pseudo code says: DO IF (CarName CONTAINS 'Honda'). COMPUTE CarType = 1. ELSE IF (CarName CONTAINS 'Nissan'). COMPUTE CarType = 2. ELSE. COMPUTE CarType = 3. END IF. Is there a way of doing this? I am basically looking for a keyword/piece of syntax that would act in the same way that the LIKE keyword works in SQL or the StringVar.Contains("Something") from Visual basic. Most of you have years of experience and have worked with SPSS probably since before I was a twinkle in my father's eye, so I'd appreciate very much your help. "Lo there do I see my father. Lo there do I see my mother and my sisters and my brothers. Lo there do I see the line of my people, back to the beginning. Lo, they do call to me, they bid me take my place among them, in the Halls of Valhalla, where the brave may live...forever." |
Administrator
|
Try this:
DO IF CHAR.INDEX(CarName,'Honda') GT 0. COMPUTE CarType = 1. ELSE IF CHAR.INDEX(CarName,'Nissan') GT 0. COMPUTE CarType = 2. ELSE. COMPUTE CarType = 3. END IF.
--
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/). |
Tried that on Spss 20 on windows xp, and it ran, created the variable but then failed to fill it it raised this error in the output
>Error # 4070. Command name: END IF >The command does not follow an unclosed DO IF command. Maybe the DO IF >command was not recognized because of an error. Use the level-of-control >shown to the left of the SPSS Statistics commands to determine the range of >LOOPs and DO IFs. >Execution of this command stops.But the syntax reference says it does.... Also the data inn the CarName contains more than just the brand, here is some: Honda S2000 Ford Focus Honda Civic 2002 Ford Escort 1991 Ford Orion Citroen Xsara 2003 L Reg Ford Mondeo Nissan Micra Nissan Skyline GTR Citroen Xantia "Lo there do I see my father. Lo there do I see my mother and my sisters and my brothers. Lo there do I see the line of my people, back to the beginning. Lo, they do call to me, they bid me take my place among them, in the Halls of Valhalla, where the brave may live...forever." |
I think there's an error in your first sentence about here " . . . failed to fill it it raised . . ." but I guess you're saying that CarType remained at sysmis rather than being changed to 1 or 2 or 3. You say there was an error; what was it??? Could it be that you did not also include a command that causes the data to be read, e.g., Execute, or any statistics command such as Frequencies?
Gene Maguin -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Tom1234 Sent: Thursday, September 19, 2013 8:26 AM To: [hidden email] Subject: Re: Syntax for 'Do if Var contains 'STRING X' ? Tried that on Spss 20 on windows xp, and it ran, created the variable but then failed to fill it it raised this error in the output But the syntax reference says it does.... Also the data inn the CarName contains more than just the brand, here is some: Honda S2000 Ford Focus Honda Civic 2002 Ford Escort 1991 Ford Orion Citroen Xsara 2003 L Reg Ford Mondeo Nissan Micra Nissan Skyline GTR Citroen Xantia ----- "Lo there do I see my father. Lo there do I see my mother and my sisters and my brothers. Lo there do I see the line of my people, back to the beginning. Lo, they do call to me, they bid me take my place among them, in the Halls of Valhalla, where the brave may live...forever." -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Syntax-for-Do-if-Var-contains-STRING-X-tp5722096p5722099.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 |
Administrator
|
In reply to this post by Tom1234
Bruce's syntax looks fine.
Maybe you aren't running the entire block of code? I'm not sure what "Also the data inn the CarName contains more than just the brand, here is some:" has to do with anything. The key concept here is the INDEX function. Grok that!
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?" |
I figured out what wrong, when the index is called it is case sensitive, where much of the data is entered by hand and not always in the same manner, so I used this is:
DO IF CHAR.INDEX(lower(CarName), 'honda' )GT 0. COMPUTE CarType = 1. (Thanks to Taras Koniukhov for that one) Oh and David, as the first time I ran the script it didn't work I thought perhaps it was searching for the string on it's own etc. so I posted some data here to make sure that wasn't an issue. And yes I was running the entire block of code. "Lo there do I see my father. Lo there do I see my mother and my sisters and my brothers. Lo there do I see the line of my people, back to the beginning. Lo, they do call to me, they bid me take my place among them, in the Halls of Valhalla, where the brave may live...forever." |
In reply to this post by Tom1234
Try this, works for me!
COMPUTE CarType =0. IF (CHAR.INDEX(CarName ,'Honda')) CarType =1. IF (CHAR.INDEX(CarName ,'Nissan')) CarType =2. EXECUTE. -- Sent from: http://spssx-discussion.1045642.n5.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 |
In reply to this post by Tom1234
Hey you can try this:
COMPUTE CarType=0. IF (INDEX(CarName ,'Honda')>0) CarType =1. IF (INDEX(CarName ,'Nissan')>0) CarType =2. EXECUTE. Basically, if CarName contains the word specified in brackets, the INDEX syntax should yield "true", i.e. 1. Veljko -- Sent from: http://spssx-discussion.1045642.n5.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 |
Free forum by Nabble | Edit this page |