- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@KachiM Thanks. This is helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;