- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
So, I shouldn't use an array..
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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[*])
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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[*]);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Super helpful! Thanks