BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ronein
Onyx | Level 15

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

9 REPLIES 9
PeterClemmensen
Tourmaline | Level 20

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;
yabwon
Onyx | Level 15

Other possible approach with arrays:

data want;
set my_data;
array numeric[*] _numeric_;
nr_Numeric_Missing_Values=nmiss(of numeric[*]);
Run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Patrick
Opal | Level 21

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

 

 

Tom
Super User Tom
Super User

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;
Ronein
Onyx | Level 15

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_);

 

Tom
Super User Tom
Super User

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.

yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Ronein
Onyx | Level 15

Thanks,

Is location of RETAIN important? can it be above SET or below SET? 

Tom
Super User Tom
Super User

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 3492 views
  • 8 likes
  • 5 in conversation