Hello
I want to calculate number of missing numeric values for each row.
Obs 1 has one missing value.
Why the result get 2 missing values???
data my_data;
input team $ points assists rebounds;
cards;
A 10 2 .
A 17 5 .
A 17 . .
A 18 3 4
A 15 0 5
B . 4 5
B 29 0 8
B . 2 9
C 12 1 9
. 30 1 .
;
run;
data want;
set my_data;
nr_Numeric_Missing_Values=nmiss(of _numeric_);
Run;
Because the _NUMERIC_ variable list also includes your new variable which is missing.
Simplest solution is to jus subtract one.
nr_Numeric_Missing_Values=nmiss(of _numeric_)-1;
If you are worried that NR_NUMERIC_MISSING_VALUES (who uses names like that??) might already exist on the input dataset and have a non missing value then add another statement to set it missing first.
nr_Numeric_Missing_Values=.;
nr_Numeric_Missing_Values=nmiss(of _numeric_)-1;
Because nr_Numeric_Missing_Values itself is initialized as a numeric variable. Therefore, it is captured by the _NUMERIC_ list. The calculation of the number of missing values in the NMISS Function occurs before the value is assigned to the variable. Therefore, you get a result +1 of what you expect in each observation.
This gives you the result you want
data my_data;
input team $ points assists rebounds;
cards;
A 10 2 .
A 17 5 .
A 17 . .
A 18 3 4
A 15 0 5
B . 4 5
B 29 0 8
B . 2 9
C 12 1 9
. 30 1 .
;
run;
data want;
set my_data;
nr_Numeric_Missing_Values = nmiss(points, assists, rebounds);
Run;
Other possible approach with arrays:
data want;
set my_data;
array numeric[*] _numeric_;
nr_Numeric_Missing_Values=nmiss(of numeric[*]);
Run;
Bart
@Ronein That's a hidden gem... Below four methods how you can get around this.
data want;
set my_data;
length nr_Numeric_Missing_Values_1 $2;
nr_Numeric_Missing_Values_1=put(nmiss(of _numeric_),f2. -l);
nr_Numeric_Missing_Values_2=-1;
nr_Numeric_Missing_Values_2=nmiss(of _numeric_);
_n_=nmiss(of _numeric_);
nr_Numeric_Missing_Values_3=_n_;
array t_var{1} _temporary_;
t_var[1]=nmiss(of _numeric_);
nr_Numeric_Missing_Values_4=t_var[1];
Run;
Because the _NUMERIC_ variable list also includes your new variable which is missing.
Simplest solution is to jus subtract one.
nr_Numeric_Missing_Values=nmiss(of _numeric_)-1;
If you are worried that NR_NUMERIC_MISSING_VALUES (who uses names like that??) might already exist on the input dataset and have a non missing value then add another statement to set it missing first.
nr_Numeric_Missing_Values=.;
nr_Numeric_Missing_Values=nmiss(of _numeric_)-1;
Thank you so much.
I just want to understand.
In the data set there are 3 numeric vars (before adding the new Var).
In obs=1 there is one missing value.
In this code: as I understand check missing values in 3 Vars. (But you say that it also check missing in the new VAR??)
And why the new var is also missing?
nr_Numeric_Missing_Values=nmiss(of _numeric_);
Just consider the order of operations.
When you start an iteration of the data step any NEW variables are set to missing (unless you mentioned them in a RETAIN statement). So your new variable is missing. When the function call runs the value is still missing. Only after the assignment statement has run is the value no longer missing.
other option could be setting that "nr_Numeric..." variable to "ever not missing":
data want;
set my_data;
nr_Numeric_Missing_Values=nmiss(of _numeric_);
retain nr_Numeric_Missing_Values 0;
Run;
Bart
Thanks,
Is location of RETAIN important? can it be above SET or below SET?
@Ronein wrote:
Thanks,
Is location of RETAIN important? can it be above SET or below SET?
The RETAIN statement is not executable, it just has impact on how the data step is compiled, so where it is placed has no effect on the performance.
But SAS defines the order of the variables as the compile "sees" them. So placing the RETAIN statement before the SET will make the variables mentioned in it appear before the variables read from the input dataset. Some people like to use this side effect of the RETAIN statement to re-order variables.
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.
Ready to level-up your skills? Choose your own adventure.