<?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 Indexing Array Elements Using DO Loops in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Having-Trouble-Indexing-Array-Elements-Using-DO-Loops/m-p/546190#M151195</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input var1 (char1-char6) ($);
cards;
1228 EXAM101 A C D B Z 
;

data want;
set have;
array c(*) _char_;
array t(5)$1;
array num(5);
temp=cats(of char2-char6);
call pokelong(temp,addrlong(t(1)),5);
call sortc( of t(*));
do i=1 to dim(num);
num(i)=whichc(c(i+1), of t(*));
end;
drop i t:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 26 Mar 2019 15:08:43 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2019-03-26T15:08:43Z</dc:date>
    <item>
      <title>Having Trouble Indexing Array Elements Using DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Having-Trouble-Indexing-Array-Elements-Using-DO-Loops/m-p/546153#M151177</link>
      <description>&lt;P&gt;Hello SAS users,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have 91 data sets with 1 numeric variable and at least 26 character variables per data set. I created a macro to conduct processing of all data sets.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One part of my macro is not working as intended. What I want to do is to specify an array statement that references only the character variables, then use DO LOOPS to do some processing for all other character variables listed after the first character variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*Convert fields of item responses from character to numeric variables*/&lt;BR /&gt;DATA Raw.&amp;amp;MODULE._Examinee_Data;&lt;BR /&gt;SET Raw.&amp;amp;MODULE._Examinee_Data_POS;&lt;BR /&gt;ARRAY CHAR[*]&amp;nbsp; _CHARACTER_;&lt;BR /&gt;DO I = 2 to dim(CHAR);&lt;BR /&gt;IF CHAR[I] = 'A' THEN CHAR[I] = '1';&lt;BR /&gt;IF CHAR[I] = 'B' THEN CHAR[I] = '2';&lt;BR /&gt;IF CHAR[I] = 'C' THEN CHAR[I] = '3';&lt;BR /&gt;IF CHAR[I] = 'D' THEN CHAR[I] = '4';&lt;BR /&gt;IF CHAR[I] = 'E' THEN CHAR[I] = '5';&lt;BR /&gt;IF CHAR[I] = 'Z' THEN CHAR[I] = '0';&lt;BR /&gt;END;&lt;BR /&gt;DO J = 2 to dim(CHAR);&lt;BR /&gt;CHAR[J] = input(CHAR[J], 32.);&lt;BR /&gt;END;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem I have is that the values for the I and J indexing variables are 2 more than the actual number of array elements processed using the DO LOOP.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data I have below - the first character variable of the array contains the text of EXAM101, and the values for the remaining character variables are listed to the right of the value for the first character variable.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1228 EXAM101 A C D B Z&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data I want below - the I and J indexing variables should each have values of 5, but my code results in values of 7 which is what I don't want.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1228 EXAM101 1 3 4 2 0 5 5&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone explain to me what I am doing wrong, and post a solution?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Mar 2019 14:12:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Having-Trouble-Indexing-Array-Elements-Using-DO-Loops/m-p/546153#M151177</guid>
      <dc:creator>ADouglas</dc:creator>
      <dc:date>2019-03-26T14:12:46Z</dc:date>
    </item>
    <item>
      <title>Re: Having Trouble Indexing Array Elements Using DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Having-Trouble-Indexing-Array-Elements-Using-DO-Loops/m-p/546158#M151180</link>
      <description>&lt;P&gt;First thing to learn is how a DO loop works.&amp;nbsp; Here's a test program you can run.&amp;nbsp; Once you are comfortable with the results, we can talk about fixing the program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
do i=1 to 5;
   put 'Inside:  ' i=;
end;
put 'Outside:  ' i=;
run;

data _null_;
do i=1 to 5 by 3;
   put 'Inside:  ' i=;
end;
put 'Outside:  ' i=;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Mar 2019 14:22:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Having-Trouble-Indexing-Array-Elements-Using-DO-Loops/m-p/546158#M151180</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-03-26T14:22:06Z</dc:date>
    </item>
    <item>
      <title>Re: Having Trouble Indexing Array Elements Using DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Having-Trouble-Indexing-Array-Elements-Using-DO-Loops/m-p/546164#M151182</link>
      <description>&lt;P&gt;How about running:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc contents data =&amp;nbsp; Raw.&amp;amp;MODULE._Examinee_Data_POS;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and share the result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You cannot change a variable type from character to numeric once it is created.&lt;/P&gt;
&lt;P&gt;So if&lt;/P&gt;
&lt;P&gt;DO J = 2 to dim(CHAR);&lt;BR /&gt;CHAR[J] = input(CHAR[J], 32.);&lt;BR /&gt;END;&lt;/P&gt;
&lt;P&gt;is supposed to create numeric values it will fail. You should have a message about numeric values converted to character in the log.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Mar 2019 14:30:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Having-Trouble-Indexing-Array-Elements-Using-DO-Loops/m-p/546164#M151182</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-03-26T14:30:25Z</dc:date>
    </item>
    <item>
      <title>Re: Having Trouble Indexing Array Elements Using DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Having-Trouble-Indexing-Array-Elements-Using-DO-Loops/m-p/546168#M151184</link>
      <description>&lt;P&gt;Ok. I tried it. I understand how DO LOOPS work now. But, I'm not sure how the index variable has 2 more than the number of variables processed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 26 Mar 2019 14:37:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Having-Trouble-Indexing-Array-Elements-Using-DO-Loops/m-p/546168#M151184</guid>
      <dc:creator>ADouglas</dc:creator>
      <dc:date>2019-03-26T14:37:55Z</dc:date>
    </item>
    <item>
      <title>Re: Having Trouble Indexing Array Elements Using DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Having-Trouble-Indexing-Array-Elements-Using-DO-Loops/m-p/546169#M151185</link>
      <description>&lt;P&gt;Two of those other variables must be numeric instead of character for the values of your index variables to be 5 instead of 7.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input id name $ (v1-v5) ($);
cards;
1228 EXAM101 A C D B Z
;

data test;
  set have;
  array chars _character_;
  do i=2 to dim(chars);
  end;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs     id      name      v1    v2    v3    v4    v5    i

 1     1228    EXAM101    A     C     D     B     Z     7&lt;/PRE&gt;
&lt;P&gt;If you have 6 character variables then DIM(chars) will be 6 and I will be set to 7 after the DO loop since having a value larger than 6 is what caused the looping to stop.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Mar 2019 14:38:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Having-Trouble-Indexing-Array-Elements-Using-DO-Loops/m-p/546169#M151185</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-03-26T14:38:09Z</dc:date>
    </item>
    <item>
      <title>Re: Having Trouble Indexing Array Elements Using DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Having-Trouble-Indexing-Array-Elements-Using-DO-Loops/m-p/546190#M151195</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input var1 (char1-char6) ($);
cards;
1228 EXAM101 A C D B Z 
;

data want;
set have;
array c(*) _char_;
array t(5)$1;
array num(5);
temp=cats(of char2-char6);
call pokelong(temp,addrlong(t(1)),5);
call sortc( of t(*));
do i=1 to dim(num);
num(i)=whichc(c(i+1), of t(*));
end;
drop i t:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Mar 2019 15:08:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Having-Trouble-Indexing-Array-Elements-Using-DO-Loops/m-p/546190#M151195</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-03-26T15:08:43Z</dc:date>
    </item>
    <item>
      <title>Re: Having Trouble Indexing Array Elements Using DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Having-Trouble-Indexing-Array-Elements-Using-DO-Loops/m-p/546192#M151196</link>
      <description>&lt;P&gt;Thanks I forgot this detail about the indexing variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Take care,&lt;/P&gt;&lt;P&gt;Aaron&lt;/P&gt;</description>
      <pubDate>Tue, 26 Mar 2019 15:13:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Having-Trouble-Indexing-Array-Elements-Using-DO-Loops/m-p/546192#M151196</guid>
      <dc:creator>ADouglas</dc:creator>
      <dc:date>2019-03-26T15:13:27Z</dc:date>
    </item>
    <item>
      <title>Re: Having Trouble Indexing Array Elements Using DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Having-Trouble-Indexing-Array-Elements-Using-DO-Loops/m-p/546231#M151213</link>
      <description>&lt;P&gt;I thought you want ABCDEZ to be changed to numbers as: 1,2,3,4,5,0&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here are two ways for doing it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input id name $ (v1-v5) ($);
cards;
1228 EXAM101 A C D B Z
1300 TEST102 Z C B D E
1400 EXAM103 A Z B C D
;
run;

data want;
   set have;
   array n[*] n1 - n5;
   array v[*] v1 - v5;
   length str $6;
   str = 'ZABCDE';
   do i = 1 to 5;
      k = findc(str, v[i]) - 1;
      n[i] = k;
   end;
drop v: i k str;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;This will change v: to n:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to rename n: to v: then it is attainable by using the RENAME as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(rename = (n1 - n5 = v1 - v5));
   set have;
   array n[*] n1 - n5;
   array v[*] v1 - v5;
   length str $6;
   str = 'ZABCDE';
   do i = 1 to 5;
      k = findc(str, v[i]) - 1;
      n[i] = k;
   end;
drop v: i k str;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Instead of FINDC() function, you can use RANK() function. 'A' will be 65, 'B' is 66 and so on. Here is the use of RANK():&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(rename = (n1 - n5 = v1 - v5));
   set have;
   array n[*] n1 - n5;
   array v[*] v1 - v5;
   do i = 1 to 5;
      k = rank(v[i]) - 64;
      if k = 26 then k = 0;
      n[i] = k;
   end;
drop v: i k;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Mar 2019 16:22:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Having-Trouble-Indexing-Array-Elements-Using-DO-Loops/m-p/546231#M151213</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-03-26T16:22:42Z</dc:date>
    </item>
  </channel>
</rss>

