<?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: Help with using index or string functions WITH array function. in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-using-index-or-string-functions-WITH-array-function/m-p/7425#M95</link>
    <description>You might want to watch using some of string related functions like peekc &amp;amp; scan with array data if you are using particulary large sized array's i.e. Element size * element length.&lt;BR /&gt;
&lt;BR /&gt;
Anything resulting in an overall array size of above 32k the functions end up producing unexpected results.&lt;BR /&gt;
&lt;BR /&gt;
i.e. Try changing the number of the elements in the examples above to be 200 and you'll notice that the functions fail to work correctly</description>
    <pubDate>Mon, 28 Jun 2010 15:00:07 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2010-06-28T15:00:07Z</dc:date>
    <item>
      <title>Help with using index or string functions WITH array function.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-using-index-or-string-functions-WITH-array-function/m-p/7419#M89</link>
      <description>I have a dataset with 14000 obs and 200 variables.&lt;BR /&gt;
There is a set of 54 character variables, named goname1-goname54, for which I want to set up an array and then search for a word/phrase within the variable values for goname1-goname54. Previously, I've successfully used both the array and the index functions, but I am unable to figure out how to do both in the same step.&lt;BR /&gt;
For example, I want to create a new variable, called POT, that will be an index of whether any of the variables, goname1-goname54, contain the word 'potassium' (i.e., POT=1 if this word appears, and POT=0 if none of the variables contain this word.)  In an attempt to do so, I've written the following program, but get all zeros for POT (and I know this is not correct):&lt;BR /&gt;
&lt;BR /&gt;
data new;&lt;BR /&gt;
 	set old;&lt;BR /&gt;
	array goname{1:54} $ 255 goname1-goname54;&lt;BR /&gt;
	flag = "potassium";&lt;BR /&gt;
	POT=0;&lt;BR /&gt;
	do i=1 to 54;&lt;BR /&gt;
	POT = index(goname{i}, flag);&lt;BR /&gt;
	end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
I've also tried listing out all of the variable names (i.e., goname1 goname2 goname3...) after the array length statement, and also tried omitting the element length, among other attempts to manipulate the order of the program.&lt;BR /&gt;
Any advice?&lt;BR /&gt;
KDA</description>
      <pubDate>Tue, 11 Mar 2008 14:48:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-using-index-or-string-functions-WITH-array-function/m-p/7419#M89</guid>
      <dc:creator>KDA</dc:creator>
      <dc:date>2008-03-11T14:48:14Z</dc:date>
    </item>
    <item>
      <title>Re: Help with using index or string functions WITH array function.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-using-index-or-string-functions-WITH-array-function/m-p/7420#M90</link>
      <description>an excellent SUGI 29 paper (authors Paul Dorfman and someone else) refers to optimising this challenge. &lt;BR /&gt;
In the paper at &lt;A href="http://www2.sas.com/proceedings/sugi29/264-29.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi29/264-29.pdf&lt;/A&gt; entitled "A-P-P Advanced Data Management Functions" the impressive statement which addresses this challenge is[pre]&lt;BR /&gt;
found = ^^ indexw (peekc (addr(a), 81 ), srchfor) ; * ^^ normalizes to std boolean ;[/pre]&lt;BR /&gt;
Implemented for your challenge, it would look something like [pre]&lt;BR /&gt;
data result_data ;&lt;BR /&gt;
  array goname{54} $ 255 ;&lt;BR /&gt;
  set old_wide_large_enough_data ;&lt;BR /&gt;
  srchfor = "potassium" ;&lt;BR /&gt;
  POT= ^^ indexw( peekc( addr( goname1 ), %eval(54*81) ), srchfor ) ; &lt;BR /&gt;
run;[/pre]&lt;BR /&gt;
The array is defined before the data is SET to ensure these variables are all together for the PEEKC() function.&lt;BR /&gt;
&lt;BR /&gt;
Much more clarification can be found in that paper, including a pointer to the alternative to peekc() for 64bit platforms.&lt;BR /&gt;
&lt;BR /&gt;
PeterC.</description>
      <pubDate>Tue, 11 Mar 2008 15:15:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-using-index-or-string-functions-WITH-array-function/m-p/7420#M90</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-03-11T15:15:03Z</dc:date>
    </item>
    <item>
      <title>Re: Help with using index or string functions WITH array function.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-using-index-or-string-functions-WITH-array-function/m-p/7421#M91</link>
      <description>KDA,&lt;BR /&gt;
&lt;BR /&gt;
You could just insert the line IF POT = 1 THEN LEAVE into the do loop. VIZ:&lt;BR /&gt;
&lt;BR /&gt;
data new;&lt;BR /&gt;
set old;&lt;BR /&gt;
array goname{1:54} $ 255 goname1-goname54;&lt;BR /&gt;
flag = "potassium";&lt;BR /&gt;
POT=0;&lt;BR /&gt;
do i=1 to 54;&lt;BR /&gt;
POT = index(goname{i}, flag);&lt;BR /&gt;
IF POT = 1 THEN LEAVE;&lt;BR /&gt;
end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
This stops POT subsequently being reset to 0 which may be all that's stopping the code working now.&lt;BR /&gt;
&lt;BR /&gt;
Regards,&lt;BR /&gt;
&lt;BR /&gt;
BPD</description>
      <pubDate>Mon, 28 Jun 2010 08:48:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-using-index-or-string-functions-WITH-array-function/m-p/7421#M91</guid>
      <dc:creator>BPD</dc:creator>
      <dc:date>2010-06-28T08:48:31Z</dc:date>
    </item>
    <item>
      <title>Re: Help with using index or string functions WITH array function.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-using-index-or-string-functions-WITH-array-function/m-p/7422#M92</link>
      <description>Hi KDA&lt;BR /&gt;
&lt;BR /&gt;
I have no doubt that Peter and Paul's solution can't be beaten in regards of performance. &lt;BR /&gt;
Peter: Thanks for posting this link. Very interesting.&lt;BR /&gt;
&lt;BR /&gt;
I believe the code below would also work for the example given:&lt;BR /&gt;
&lt;BR /&gt;
data new;&lt;BR /&gt;
set old;&lt;BR /&gt;
flag = "potassium";&lt;BR /&gt;
pot=  find(cats(of goname1-goname54),flag)&amp;gt;0;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick</description>
      <pubDate>Mon, 28 Jun 2010 12:08:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-using-index-or-string-functions-WITH-array-function/m-p/7422#M92</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2010-06-28T12:08:09Z</dc:date>
    </item>
    <item>
      <title>Re: Help with using index or string functions WITH array function.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-using-index-or-string-functions-WITH-array-function/m-p/7423#M93</link>
      <description>Hi. I think you should add&lt;BR /&gt;
[pre]&lt;BR /&gt;
retain flag;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Because flag variable is not come from dataset, so it will be set missing when data step enter the next iteration.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Mon, 28 Jun 2010 12:35:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-using-index-or-string-functions-WITH-array-function/m-p/7423#M93</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2010-06-28T12:35:22Z</dc:date>
    </item>
    <item>
      <title>Re: Help with using index or string functions WITH array function.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-using-index-or-string-functions-WITH-array-function/m-p/7424#M94</link>
      <description>Hi:&lt;BR /&gt;
  You are correct, that flag is initialized to MISSING for each iteration of the DATA step at the top of the program, however, flag is also assigned the value 'potassium' on each iteration of the DATA step program. So, the original code was OK. The problem was more likely one of the other issues noted.&lt;BR /&gt;
&lt;BR /&gt;
  It would only be better to use a retain for the FLAG variable if there was also a statement that assigned the value to FLAG only 1 time...something like:&lt;BR /&gt;
[pre]&lt;BR /&gt;
  retain flag;&lt;BR /&gt;
  if _n_ = 1 then flag = 'potassium';&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
cynthia&lt;BR /&gt;
&lt;BR /&gt;
ps...you can prove to yourself that potassium gets set on every iteration of the DATA step by using a test program that reads ANY data and uses similar logic:&lt;BR /&gt;
[pre]&lt;BR /&gt;
3097  data new;&lt;BR /&gt;
3098    length flag $9;&lt;BR /&gt;
3099    set sashelp.class;&lt;BR /&gt;
3100    put 'before assignment statement: ' _n_= flag=;&lt;BR /&gt;
3101                                                  &lt;BR /&gt;
3102    flag = "potassium";&lt;BR /&gt;
3103    put 'after assignment statement...' ;&lt;BR /&gt;
3104    put _n_= name= flag=;&lt;BR /&gt;
3105  run;&lt;BR /&gt;
                                   &lt;BR /&gt;
before assignment statement: _N_=1 flag=&lt;BR /&gt;
after assignment statement...&lt;BR /&gt;
_N_=1 Name=Alfred flag=potassium&lt;BR /&gt;
before assignment statement: _N_=2 flag=&lt;BR /&gt;
after assignment statement...&lt;BR /&gt;
_N_=2 Name=Alice flag=potassium&lt;BR /&gt;
before assignment statement: _N_=3 flag=&lt;BR /&gt;
after assignment statement...&lt;BR /&gt;
_N_=3 Name=Barbara flag=potassium&lt;BR /&gt;
before assignment statement: _N_=4 flag=&lt;BR /&gt;
after assignment statement...&lt;BR /&gt;
_N_=4 Name=Carol flag=potassium&lt;BR /&gt;
before assignment statement: _N_=5 flag=&lt;BR /&gt;
after assignment statement...&lt;BR /&gt;
_N_=5 Name=Henry flag=potassium&lt;BR /&gt;
before assignment statement: _N_=6 flag=&lt;BR /&gt;
after assignment statement...&lt;BR /&gt;
_N_=6 Name=James flag=potassium&lt;BR /&gt;
before assignment statement: _N_=7 flag=&lt;BR /&gt;
after assignment statement...&lt;BR /&gt;
_N_=7 Name=Jane flag=potassium&lt;BR /&gt;
before assignment statement: _N_=8 flag=&lt;BR /&gt;
after assignment statement...&lt;BR /&gt;
_N_=8 Name=Janet flag=potassium&lt;BR /&gt;
before assignment statement: _N_=9 flag=&lt;BR /&gt;
after assignment statement...&lt;BR /&gt;
_N_=9 Name=Jeffrey flag=potassium&lt;BR /&gt;
before assignment statement: _N_=10 flag=&lt;BR /&gt;
after assignment statement...&lt;BR /&gt;
_N_=10 Name=John flag=potassium&lt;BR /&gt;
before assignment statement: _N_=11 flag=&lt;BR /&gt;
after assignment statement...&lt;BR /&gt;
_N_=11 Name=Joyce flag=potassium&lt;BR /&gt;
before assignment statement: _N_=12 flag=&lt;BR /&gt;
after assignment statement...&lt;BR /&gt;
_N_=12 Name=Judy flag=potassium&lt;BR /&gt;
before assignment statement: _N_=13 flag=&lt;BR /&gt;
after assignment statement...&lt;BR /&gt;
_N_=13 Name=Louise flag=potassium&lt;BR /&gt;
before assignment statement: _N_=14 flag=&lt;BR /&gt;
after assignment statement...&lt;BR /&gt;
_N_=14 Name=Mary flag=potassium&lt;BR /&gt;
before assignment statement: _N_=15 flag=&lt;BR /&gt;
after assignment statement...&lt;BR /&gt;
_N_=15 Name=Philip flag=potassium&lt;BR /&gt;
before assignment statement: _N_=16 flag=&lt;BR /&gt;
after assignment statement...&lt;BR /&gt;
_N_=16 Name=Robert flag=potassium&lt;BR /&gt;
before assignment statement: _N_=17 flag=&lt;BR /&gt;
after assignment statement...&lt;BR /&gt;
_N_=17 Name=Ronald flag=potassium&lt;BR /&gt;
before assignment statement: _N_=18 flag=&lt;BR /&gt;
after assignment statement...&lt;BR /&gt;
_N_=18 Name=Thomas flag=potassium&lt;BR /&gt;
before assignment statement: _N_=19 flag=&lt;BR /&gt;
after assignment statement...&lt;BR /&gt;
_N_=19 Name=William flag=potassium&lt;BR /&gt;
NOTE: There were 19 observations read from the data set SASHELP.CLASS.&lt;BR /&gt;
NOTE: The data set WORK.NEW has 19 observations and 6 variables.&lt;BR /&gt;
NOTE: DATA statement used (Total process time):&lt;BR /&gt;
      real time           0.03 seconds&lt;BR /&gt;
      cpu time            0.03 seconds&lt;BR /&gt;
&lt;BR /&gt;
[/pre]</description>
      <pubDate>Mon, 28 Jun 2010 14:11:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-using-index-or-string-functions-WITH-array-function/m-p/7424#M94</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-06-28T14:11:12Z</dc:date>
    </item>
    <item>
      <title>Re: Help with using index or string functions WITH array function.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-using-index-or-string-functions-WITH-array-function/m-p/7425#M95</link>
      <description>You might want to watch using some of string related functions like peekc &amp;amp; scan with array data if you are using particulary large sized array's i.e. Element size * element length.&lt;BR /&gt;
&lt;BR /&gt;
Anything resulting in an overall array size of above 32k the functions end up producing unexpected results.&lt;BR /&gt;
&lt;BR /&gt;
i.e. Try changing the number of the elements in the examples above to be 200 and you'll notice that the functions fail to work correctly</description>
      <pubDate>Mon, 28 Jun 2010 15:00:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-using-index-or-string-functions-WITH-array-function/m-p/7425#M95</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-06-28T15:00:07Z</dc:date>
    </item>
    <item>
      <title>Re: Help with using index or string functions WITH array function.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-using-index-or-string-functions-WITH-array-function/m-p/7426#M96</link>
      <description>Hi Patrick&lt;BR /&gt;
&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; Peter: Thanks for posting this link. Very interesting.&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; I believe the code below would also work for the example given:&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; data new;&lt;BR /&gt;
&amp;gt; set old;&lt;BR /&gt;
&amp;gt; flag = "potassium";&lt;BR /&gt;
&amp;gt; pot=  find(cats(of goname1-goname54),flag)&amp;gt;0;&lt;BR /&gt;
&lt;BR /&gt;
*  issues like trailing blanks in FLAG, and rejecting substrings during the search,  are addressed in SAS9.2 with the  findW() function. &lt;BR /&gt;
For SAS9 platforms where FINDW() is not available, the following work-around looks tedious ;&lt;BR /&gt;
 pot = find( '|'!! catx( '||', of goname1-goname54) !!'|', cats('|',flag,'|') )&amp;gt;0 ;&lt;BR /&gt;
&lt;BR /&gt;
&amp;gt; run;&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
 &lt;BR /&gt;
The enhanced functions in SAS9.2 are really making a difference. &lt;BR /&gt;
  &lt;BR /&gt;
regards&lt;BR /&gt;
peterC</description>
      <pubDate>Thu, 01 Jul 2010 08:37:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-using-index-or-string-functions-WITH-array-function/m-p/7426#M96</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2010-07-01T08:37:28Z</dc:date>
    </item>
    <item>
      <title>Re: Help with using index or string functions WITH array function.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-using-index-or-string-functions-WITH-array-function/m-p/7427#M97</link>
      <description>Hi.&lt;BR /&gt;
You are right .I think i am too sensitive.</description>
      <pubDate>Fri, 02 Jul 2010 03:31:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-using-index-or-string-functions-WITH-array-function/m-p/7427#M97</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2010-07-02T03:31:10Z</dc:date>
    </item>
  </channel>
</rss>

