开发者

Array problem using if and do loop

This is my code:

data INDAT8; set INDAT6;
Array myarray{24,27};
goodgroups=0;

do i=2 to 24 by 2;
   do j=2 to 27;

   if myarray[i,j] gt 1 then myarray[i+1,j] = 'bad';
       else if myarray[i,j] eq 1 and myarray[i+1,j] = 1 then myarray[i+1,j]= 'good';
   end;
end;
run;

proc print data=IND开发者_开发百科AT8; run;

Problem:

I have the data in this format- it is just an example: n=2 X Y info

2      1       good
2      4       bad

3      2      good

4     1       bad
4      4      good

6       2     good
6       3     good

Now, the above data is in sorted manner (total 7 rows). I need to make a group of 2 , 3 or 4 rows separately and generate a graph. In the above data, I made a group of 2 rows. The third row is left alone as there is no other column in 3rd row to form a group. A group can be formed only within the same row. NOT with other rows.

Now, I will check if both the rows have “good” in the info column or not. If both rows have “good” – the group formed is also good , otherwise bad. In the above example, 3rd /last group is “good” group. Rest are all bad group. Once I’m done with all the rows, I will calculate the total no. of Good groups formed/Total no. of groups.

In the above example, the output will be: Total no. of good groups/Total no. of groups => 1/3.

This is the case of n=2(size of group)

Now, for n=3, we make group of 3 rows and for n=4, we make a group of 4 rows and find the good /bad groups in a similar way. If all the rows in a group has “good” block—the result is good block, otherwise bad.

Example: n= 3

2      1       good
2      4       bad
2     6        good

3      2      good

4     1       good
4      4      good
4    6        good

6       2     good
6       3     good

In the above case, I left the 4th row and last 2 rows as I can’t make group of 3 rows with them. The first group result is “bad” and last group result is “good”.

Output: 1/ 2

For n= 4:

2      1       good
2      4       good
2     6        good
2      7       good

3      2      good

4     1       good
4      4      good
4    6        good

6       2     good
6       3     good
6       4     good
6       5     good

In this case, I make a group of 4 and finds the result. The 5th,6th,7th,8th row are left behind or ignored. I made 2 groups of 4 rows and both are “good” blocks. Output: 2/2

So, After getting 3 output values from n=2 , n-3, and n=4 I will plot a graph of these values.

If you can help in any any language using array, if and do loop. it would be great.

I can change my code accordingly.

Update:

The answer for this doesn't have to be in sas. Since it is more algorithm-related than anything, I will accept suggestions in any language as long as they show how to accomplish this using arrays and do.


I am having trouble understanding your problem statement, but from what I can gather here is what I can suggest:

Place data into bins and the process the summary data.

Implementation 1

Assumption: You don't know what the range of the first column will be or distriution will be sparse

  • Create a hash table. The Key will be the item you are doing your grouping on. The value will be the count seen so far.
  • Proces each record. If the key already exists, increment the count (value for that key in the hash). Otherwise add the key and set the value to 1.
  • Continue until you have processed all records
  • Count the number of keys in the hash table and the number of values that are greater than your threshold.

Implementation 2

Assumption: You know the range of the first column and the distriution is reasonably dense

  • Create an array of integers with enough elements so the index can match the column value. Initialize all elements to zero. This array will hold your count for each item you are grouping on
  • Process each record. Examine value of first column. Increment corresponding index in array. (So if you have "2 1 good", do groupCount[2]++)
  • Continue until you have processed all records
  • Walk each element in the array. Count how many items are non zero (meaning they appeared at least once) and how many items meet your threshold.

You can use the same approach for gathering the good and bad counts.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜