{"id":340,"date":"2013-09-30T19:14:16","date_gmt":"2013-09-30T19:14:16","guid":{"rendered":"http:\/\/www.pythonforspss.org\/?p=340"},"modified":"2015-01-27T07:54:17","modified_gmt":"2015-01-27T07:54:17","slug":"spss-index-function","status":"publish","type":"post","link":"https:\/\/www.spss-tutorials.com\/spss-index-function\/","title":{"rendered":"SPSS CHAR.INDEX Function &#8211; Find Characters in String"},"content":{"rendered":"<!--body-->\n\n<p>The SPSS CHAR.INDEX function returns the position of the first occurrence of a given expression within a <a href=\"https:\/\/www.spss-tutorials.com\/spss-string-variables-basics\/ \">string<\/a>. If the expression does not occur in the string, it returns a zero. Please note that string values are case sensitive.<\/p>\n\n<span class='img w600'>\n\t<img src='https:\/\/spss-tutorials.com\/img\/spss-index-function.png' alt='SPSS Index Function' title='SPSS Index Function'><span class='small'>SPSS Index Function Example<\/span>\n<\/span>\n\n\n<h2>SPSS CHAR.INDEX Example<\/h2>\n\n<p>Say we have data holding some email addresses and we'd like to see which domains are used most. For each email address, the domain is everything after the <code>@<\/code> sign. The <a href=\"https:\/\/www.spss-tutorials.com\/spss-syntax\/ \">syntax<\/a> below demonstrates how to do so. We'll first find the position of the first (and only)<code>@<\/code> in step 2. Next, we'll substitute that into a <code>SUBSTR<\/code> function in step 4.<\/p>\n\n\n<h2>SPSS CHAR.INDEX Syntax Example I<\/h2>\n\n<div class='code'><strong>*1. Create data.<br \/><\/strong><br \/>data list free\/email (a20).<br \/>begin data<br \/>stefan@hotmail.com anneke123@gmail.com ruben@yahoo.com erik1970@bkb.nl maarten1979bkb.nl<br \/>end data.<br \/><br \/><strong>*2. Find position of first &#34;@&#34;.<br \/><\/strong><br \/>compute first_a = char.index(email,&#39;@&#39;).<br \/>exe.<br \/><br \/><strong>*3. Declare new string variable for domain.<br \/><\/strong><br \/>string domain(a15).<br \/><br \/><strong>*4. Extract domain from email address.<br \/><\/strong><br \/>compute domain = char.substr(email,char.index(email,&#39;@&#39;) + 1).<br \/>exe.<br \/><br \/><strong>*5. Correction for email without &#34;@&#34;.<br \/><\/strong><br \/>if char.index(email,&#39;@&#39;) = 0 domain = &#39;&#39;.<br \/>exe.<\/div><!--class='code'-->\n<p>Note that there's an error in the data since the last email address doesn't contain any <code>@<\/code>. Therefore, <code>first_@<\/code> is zero for this case. This makes step 4 come up with an incorrect <code>domain<\/code>, hence the correction at the end.<span class='inline-comment'>A better option here is to use a single <a href=\"https:\/\/www.spss-tutorials.com\/spss-if-command\/ \">IF<\/a> command that computes the domain only if <code>@<\/code> is present in the email address.<\/span><\/p>\n\n\n<h2 id='divisor'>SPSS CHAR.INDEX - the Divisor<\/h2>\n\n<p>A little known feature of SPSS' CHAR.INDEX function is an optional third argument known as the divisor. The divisor divides the search expression into substrings of length <em>n<\/em>. The position of the first occurrence of one of these substrings is returned. For example, in \n<span class = 'code'>CHAR.INDEX(variable,'0123456789',1)<\/span> \nthe divisor is <code>1<\/code>. This breaks <code>0123456789<\/code> into substrings of length 1, rendering the digits <code>0<\/code> through <code>9<\/code>. The position of the first digit is now returned.<\/p>\n\n<p>The next syntax example extracts all digits from a string. It combines the use of the divisor with LOOP, <a href=\"https:\/\/www.spss-tutorials.com\/substring\/ \">SUBSTR<\/a> and CONCAT in order to do so. The last step uses <a href=\"https:\/\/www.spss-tutorials.com\/spss-alter-type-command\/\">ALTER TYPE<\/a> for converting it into a <a href=\"https:\/\/www.spss-tutorials.com\/spss-numeric-variables-basics\/\">numeric variable<\/a>.<\/p>\n\n\n<h2>SPSS CHAR.INDEX Syntax Example II<\/h2>\n\n<div class='code'><strong>*1. Check whether any number is present in email.<br \/><\/strong><br \/>compute number_present = char.index(email,&#39;0123456789&#39;,1) > 0.<br \/>exe.<br \/><br \/><strong>*2. Declare new string.<br \/><\/strong><br \/>string numbers(a20).<br \/><br \/><strong>*3. Loop through characters and pass each digit into string.<br \/><\/strong><br \/>loop #pos =1  to char.length(email).<br \/>if char.index(char.substr(email,#pos,1),&#39;0123456789&#39;,1) > 0 numbers = concat(numbers,char.substr(email,#pos,1)).<br \/>end loop.<br \/>exe.<br \/><br \/><strong>*4. Convert string to numeric variable.<br \/><\/strong><br \/>alter type numbers(f1.0).<\/div><!--class='code'-->\n\n","protected":false},"excerpt":{"rendered":"<p>The SPSS CHAR.INDEX function returns the position of the first occurrence of a given expression within a string. If this expression does not occur in the string, it returns a zero.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[],"class_list":["post-340","post","type-post","status-publish","format-standard","hentry","category-spss-string-functions"],"_links":{"self":[{"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/posts\/340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/comments?post=340"}],"version-history":[{"count":0,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/posts\/340\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/media?parent=340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/categories?post=340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/tags?post=340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}