turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- In operator in array

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-16-2016 04:31 PM - edited 08-16-2016 04:31 PM

Why the first observation of the X1 is recoded as 0?

```
data test;
input x1 x2 x3;
cards;
1 2 3
4 5 6
7 8 9
2 . 4
;
run;
data want (drop = i);
set test;
array m{*} x:;
do i = 1 to dim(m);
if 2 in m then
m{i} = 0;
mean = mean(of m{*});
end;
run;
```

Accepted Solutions

Solution

08-17-2016
10:09 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-16-2016 04:59 PM

The key to this program is here:

if 2 in m then m{i} = 0;

When i=1, m{i} refers to the first variable in the array, or X1. That's how X1 gets set to 0.

The shocking (at least to me) part of this is that how SAS interprets "if 2 in m". That actually is checking to see whether any of the array elements is equal to 2.

All Replies

Solution

08-17-2016
10:09 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-16-2016 04:59 PM

The key to this program is here:

if 2 in m then m{i} = 0;

When i=1, m{i} refers to the first variable in the array, or X1. That's how X1 gets set to 0.

The shocking (at least to me) part of this is that how SAS interprets "if 2 in m". That actually is checking to see whether any of the array elements is equal to 2.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-16-2016 05:05 PM

Astounding wrote:

The key to this program is here:

if 2 in m then m{i} = 0;

When i=1, m{i} refers to the first variable in the array, or X1. That's how X1 gets set to 0.

The shocking (at least to me) part of this is that how SAS interprets "if 2 in m". That actually is checking to see whether any of the array elements is equal to 2.

Agreed about the surprise. Especially since IN doesn't normally use variables as the target.

I did note that "if 2 in (M) ..." generates an error message. So we have yet another bit for arrays to examine.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-16-2016 05:02 PM

Because one of the elements of the array M has the value 2 at that time.

If you use "If 3 in M then m{i}=0" note that all 3 elements will be assigned as 0 for the first observation.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-16-2016 05:09 PM - edited 08-16-2016 07:14 PM

You have recieved the answer that IN operator looks for the presence of 2.

Another way to implement your idea is to get the index position of 2 in the array(using WHICHN() function). You are replacing it by zero if 2 is present. This means that zero will be counted as non-missing value in computing the mean. Further, the use of array does not need the do-loop.

If my presumtion is correct here is the way:

`Revised Code to take care of repeated 2s in the Array.`

data test;

input x1 x2 x3;

cards;

1 2 3

4 5 6

2 8 2

2 . 4

;

run;

data want (drop = w);

set test;

array m[*] x:;

do i = 1 to dim(m);

w = whichn(2, of m[*]);

if w then m[w] = .;

end;

mean = mean(of m[*]);

run;

The below code can replace one time.

data want (drop = w);
set test;
array m[*] x:;
w = whichn(2, of m[*]);
if w then m[w] = .;
mean = mean(of m[*]);
run;
proc print data = want;
run;

I have replaced 2 by a missing value for the count of non_missing values in the array. If you need that zero to be counted, it is just place 0 instead of missing value.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-17-2016 10:10 AM

@datasp Thanks. This is helpful.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-17-2016 10:18 AM

You still need a loop to handle multiple variables with a value of 2 in the array.

But instead of iterative DO loop you can use a DO WHILE loop.

```
data want;
set test;
array m x:;
do until (w=0);
w = whichn(2, of m(*));
if w then m(w) = .;
end;
mean = mean(of m(*));
drop w;
run;
```