BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10

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;

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

6 REPLIES 6
Astounding
PROC Star

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.

ballardw
Super User

@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.

ballardw
Super User

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.

KachiM
Rhodochrosite | Level 12

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.

SAS_inquisitive
Lapis Lazuli | Level 10

@KachiM Thanks. This is helpful.

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1801 views
  • 5 likes
  • 5 in conversation