BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Emet
Fluorite | Level 6

Greetings Everyone,

I want to create a new variable that is equal to number of 6 numeric variables using an array. I need to count non missing values in these variables..

data New;
set old;
Retain CT 0;
Array A[3] a b c ;
Do i = 1 to 3;
if A[i] = .  then CT= . ;
else If A[i] NE . then CT= a+b+c;
end;
Drop i;

run;

 

The code above is working but it gave incorrect large number for the data I am testing..

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
sbxkoenk
SAS Super FREQ

You can also use kind of an implicit array _NUMERIC_.

 

data want;
 set sashelp.class;
 if      _N_=1  then height=.;
 else if _N_=11 then age=.;
 count_non_missing=n(of _NUMERIC_);
run;

Koen

View solution in original post

11 REPLIES 11
PaigeMiller
Diamond | Level 26

UNTESTED CODE — counting number of non-missing values in variables A B C

 

data new;
    set old;
    count_non_missing=n(a,b,c);
run;
--
Paige Miller
Emet
Fluorite | Level 6

So, I shouldn't use an array..

Thanks

sbxkoenk
SAS Super FREQ

Hello,

 

Method provided by @PaigeMiller works fine. See below.

I also added another way of doing the same thing with an array (so you have two times "the same" in one data step).

Choose the method of your preference.

data want(drop=i);
 set sashelp.class;
 if      _N_=1  then height=.;
 else if _N_=11 then age=.;
 array numvars{3} age height weight;
 numvars_not_missing=0;
 do i=1 to dim(numvars);
  if numvars(i) ^= . then numvars_not_missing=numvars_not_missing+1;
 end;
 count_non_missing=n(age,height,weight);
run;

Koen

sbxkoenk
SAS Super FREQ

You can also use kind of an implicit array _NUMERIC_.

 

data want;
 set sashelp.class;
 if      _N_=1  then height=.;
 else if _N_=11 then age=.;
 count_non_missing=n(of _NUMERIC_);
run;

Koen

PaigeMiller
Diamond | Level 26

@sbxkoenk wrote:

You can also use kind of an implicit array _NUMERIC_.

 

data want;
 set sashelp.class;
 if      _N_=1  then height=.;
 else if _N_=11 then age=.;
 count_non_missing=n(of _NUMERIC_);
run;

Koen


This only works if the question is how to count the number of non-missing values from ALL numeric variables in the data set. This was never specified as a criterion in this problem. Using _NUMERIC_ would give the wrong answer in many cases.

--
Paige Miller
sbxkoenk
SAS Super FREQ

Hello @PaigeMiller ,

 


@PaigeMiller wrote:


This only works if the question is how to count the number of non-missing values from ALL numeric variables in the data set. This was never specified as a criterion in this problem. Using _NUMERIC_ would give the wrong answer in many cases.


Absolutely true.

I have read the original post again, and I was perhaps a little too hasty in my response. But anyway, it is apparently what the questioner ( @Emet )  wanted, because she has accepted the answer as a solution.
I will add a "disclaimer" next time : that _NUMERIC_ is looking at ALL numeric variables.

 

Thanks,

Koen

Tom
Super User Tom
Super User

I think what you are trying to say is that you don't have to define an array and can instead just use the N() function with the list of variables directly.

 

Also you can use the OF keyword to allow the use of variable lists in a function call.

Such as:

n(of v1-v10)
n(of a b c)
n(of array_name[*])

 

Emet
Fluorite | Level 6

Thanks for the responses across the thread.. Your comments are very helpful as I am a new SAS user.

Actually, I am interested to use arrays and I wanted to know if functions like N, Nmiss, Sum would simply work.

e.g. B= sum(A [i] )

it didn't work this way but I will try adding of as Tom suggested.

 

Best,

 

Tom
Super User Tom
Super User

@Emet wrote:

Thanks for the responses across the thread.. Your comments are very helpful as I am a new SAS user.

Actually, I am interested to use arrays and I wanted to know if functions like N, Nmiss, Sum would simply work.

e.g. B= sum(A [i] )

it didn't work this way but I will try adding of as Tom suggested.

 

Best,

 


If you call the SUM() function with only one argument then you should just be using an assignment statement.  So your statement is the same as :

b=a[i];

If you want to sum all of the variables in an array you can use * as the index to get a variable list that consists of all of the variables in the array.  To use a variable list in a function call you need the OF keyword.

sum_a = sum(of a[*]);
Tom
Super User Tom
Super User

The code cannot work as posted since you cannot define an array named A that contains a variable named A.  The names used for arrays have to be distinct from the names used for variables in the data step.

 

You don't need the RETAIN, unless you want to make a grand total over all of the observations.

 

You don't want total of A,B and C if the goal is just to count non-missing values.  Plus if did want to actually sum the values of A, B and C do not use the addition operator.  If you use the addition operator then if any of them have a missing value the result is a missing value.  So total those three variables you can use the SUM() function and missing values will be ignored.  total=sum(a,b,c)  or perhaps total=sum(0,a,b,c).

 

If you want to test for missing values it is more precise to use MISSING() function instead of comparing to the missing value.  That is because SAS has 27 other special missing values that the MISSING() function will detect but your equality test will not.

 

To find the number of non missing values from a set of values you can use the N() function.

data new;
  set old;
  CT=n(a,b,c);
run;

To use a list of variables in a function call you can use the OF keyword.

  CT=n(of a b c);
Emet
Fluorite | Level 6

Super helpful! Thanks

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 11 replies
  • 3210 views
  • 7 likes
  • 4 in conversation