<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: having trouble with index function using 2 variables. in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/having-trouble-with-index-function-using-2-variables/m-p/105620#M29466</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Tom, if you wouldn't mind, I'm trying to understand this. Clearly it works. But when I create another variable in proc sql,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;LENGTH(var2) as lenvar&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It returns 4 for each of the VAR2 values (all of them look like CA09) which makes sense.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But if TRIM is removing something, what can possibly be removed if the LENGTH of CA09 was already 4?&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Dave&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 24 Oct 2012 18:26:51 GMT</pubDate>
    <dc:creator>ucdcrush</dc:creator>
    <dc:date>2012-10-24T18:26:51Z</dc:date>
    <item>
      <title>having trouble with index function using 2 variables.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/having-trouble-with-index-function-using-2-variables/m-p/105617#M29463</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi everyone, I'm having trouble with a very basic thing (I'm new to SAS) and I'm hoping there is an easy answer.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is how the dataset looks:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;var1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var2 &lt;BR /&gt;CA09|CA10&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CA09 &lt;BR /&gt;CA06|CA09&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CA09 &lt;BR /&gt;CA06&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CA09 &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am attempting to create a variable called "ismatch" which I intended to help identify rows where var2 is found within var1. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I use proc sql: &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql; &lt;BR /&gt;create table newtable as &lt;BR /&gt;select *, index(var1, var2) as ismatch; &lt;BR /&gt;quit; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What this produces, based on the above table is: &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;var1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ismatch &lt;BR /&gt;CA09|CA10&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CA09&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0 &lt;BR /&gt;CA06|CA09&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CA09&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6 &lt;BR /&gt;CA06&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CA09&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0 &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;... &lt;/P&gt;&lt;P&gt;2nd and 3rd row are OK. But the first row is coming up 0 despite CA09 being at the beginning of the string. I've tested it more, and it seems that the index function will only identify CA09 when it is the rightmost part of var1 (i.e., it would not find CA09 within CA06|CA09|CA11).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Yet, if I hardcode "CA09" in place of var2, the code works perfectly and finds CA09 anywhere in the string (as intended). I've also tried it in a data step and it seems to behave the same way. All examples of the INDEX function I've found use a hardcoded string, but is it possible to use a variable there?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Or is there a better way to find one string in another when both are variables?&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Oct 2012 18:01:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/having-trouble-with-index-function-using-2-variables/m-p/105617#M29463</guid>
      <dc:creator>ucdcrush</dc:creator>
      <dc:date>2012-10-24T18:01:47Z</dc:date>
    </item>
    <item>
      <title>Re: having trouble with index function using 2 variables.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/having-trouble-with-index-function-using-2-variables/m-p/105618#M29464</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Try trimming the blanks off the end of VAR2 when using the index function.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;891&amp;nbsp; data have ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;892&amp;nbsp;&amp;nbsp;&amp;nbsp; length var1 $20 var2 $10 ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;893&amp;nbsp;&amp;nbsp;&amp;nbsp; input var1 var2;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;894&amp;nbsp;&amp;nbsp;&amp;nbsp; loc1 = index(var1,var2);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;895&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;STRONG&gt;loc2 = index(var1,trim(var2));&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;896&amp;nbsp;&amp;nbsp;&amp;nbsp; put (loc1 loc2 var1 var2) (=) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;897&amp;nbsp; cards;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;loc1=0 loc2=1 var1=CA09|CA10 var2=CA09&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;loc1=6 loc2=6 var1=CA06|CA09 var2=CA09&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;loc1=0 loc2=0 var1=CA06 var2=CA09&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;NOTE: The data set WORK.HAVE has 3 observations and 4 variables.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;NOTE: DATA statement used (Total process time):&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; real time&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.01 seconds&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cpu time&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.01 seconds&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Oct 2012 18:11:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/having-trouble-with-index-function-using-2-variables/m-p/105618#M29464</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2012-10-24T18:11:19Z</dc:date>
    </item>
    <item>
      <title>Re: having trouble with index function using 2 variables.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/having-trouble-with-index-function-using-2-variables/m-p/105619#M29465</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you Tom!!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Oct 2012 18:20:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/having-trouble-with-index-function-using-2-variables/m-p/105619#M29465</guid>
      <dc:creator>ucdcrush</dc:creator>
      <dc:date>2012-10-24T18:20:11Z</dc:date>
    </item>
    <item>
      <title>Re: having trouble with index function using 2 variables.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/having-trouble-with-index-function-using-2-variables/m-p/105620#M29466</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Tom, if you wouldn't mind, I'm trying to understand this. Clearly it works. But when I create another variable in proc sql,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;LENGTH(var2) as lenvar&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It returns 4 for each of the VAR2 values (all of them look like CA09) which makes sense.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But if TRIM is removing something, what can possibly be removed if the LENGTH of CA09 was already 4?&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Dave&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Oct 2012 18:26:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/having-trouble-with-index-function-using-2-variables/m-p/105620#M29466</guid>
      <dc:creator>ucdcrush</dc:creator>
      <dc:date>2012-10-24T18:26:51Z</dc:date>
    </item>
    <item>
      <title>Re: having trouble with index function using 2 variables.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/having-trouble-with-index-function-using-2-variables/m-p/105621#M29467</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;All character variables in SAS are fixed length fields.&amp;nbsp; The LENGTH() function is showing you the length of the trimmed value, not the defined length of the variable.&amp;nbsp; To see the defined length of the variable run PROC CONTENTS on your dataset&amp;nbsp; (or use the DESCRIBE TABLE statement in SQL).&amp;nbsp; You seem to be more familiar with SQL syntax so to define the length of variable created by a SQL select statement you can add the LENGTH keyword.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;create table new as&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp; select 'A'&amp;nbsp; as newvar length=5 &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , *&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp; from sashelp.class&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For most operations SAS will automatically trim the trailing spaces.&amp;nbsp; INDEX() is an exception where the spaces are preserved.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For example:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;929&amp;nbsp; data _null_;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;930&amp;nbsp;&amp;nbsp;&amp;nbsp; if 'A&amp;nbsp;&amp;nbsp; ' = 'A' then put 'yes';&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;931&amp;nbsp; run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;yes&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Oct 2012 18:38:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/having-trouble-with-index-function-using-2-variables/m-p/105621#M29467</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2012-10-24T18:38:34Z</dc:date>
    </item>
    <item>
      <title>Re: having trouble with index function using 2 variables.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/having-trouble-with-index-function-using-2-variables/m-p/105622#M29468</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you again sir!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Oct 2012 18:49:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/having-trouble-with-index-function-using-2-variables/m-p/105622#M29468</guid>
      <dc:creator>ucdcrush</dc:creator>
      <dc:date>2012-10-24T18:49:07Z</dc:date>
    </item>
  </channel>
</rss>

