BookmarkSubscribeRSS Feed
dmarques1998
Obsidian | Level 7

Hi, 

 

I have the following data: 

data example;
  input id var1 var2 var3 var4 var5 var6 var7 var8 var9 var10 var11 var12;
cards;
A	1 2 3 . . . . . 1 1 1 3
B	3 3 2 1 3 2 1 . . . . .
C	. . . . 1 2 3 1 2 3 2 .
D	. . . . 3 1 1 . 3 . 1 .
E	. . . . 1 3 1 3 1 3 . .
F	3 2 1 . . . . . 1 1 1 3

; 

And I want to count the number of missing values, however I want to start counting only when we start having values. For example, I want the following output: 

 

ID Number of missing values: 

A  5

B 5 

C 1

D 3

E 2 

F 5 

 

Thanks

12 REPLIES 12
Kurt_Bremser
Super User
data want;
set  example;
array vars{*} var1-var12;
nomiss = 0;
number_miss = 0;
do _n_ = 1 to dim(vars);
  if vars{_n_} then nomiss = 1;
  if nomiss and vars{_n_} = . then number_miss + 1;
end;
drop nomiss;
run;

Untested, posted from my tablet.

 

Edit: fixed a typo (thrn -> then)

dmarques1998
Obsidian | Level 7
From that way, it is counting all the missing values!
PaigeMiller
Diamond | Level 26

@dmarques1998, did you even try the code from @Kurt_Bremser? It gives the exact answer you want (after correcting a typographical error).

--
Paige Miller
dmarques1998
Obsidian | Level 7
yes, and adapting to my code, give me the exact number of missing values, and not the number of missing values that I want
PaigeMiller
Diamond | Level 26

Okay, you said:

 

For example, I want the following output: 

 

ID Number of missing values: 

A  5

B 5 

C 1

D 3

E 2 

F 5 

and those are the numbers I see when I run the code.

 

Capture.PNG

 

If you're not getting the above answers, then show us the exact code you are using.

--
Paige Miller
dmarques1998
Obsidian | Level 7
this is the Note/warning that appear in the log :

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
30:6
NOTE: Invalid numeric data, 'XXXX' , at line 30 column 6.
NOTE: Invalid numeric data, 'XXXXX' , at line 30 column 6.
WARNING: Limit set by ERRORS= option reached. Further errors of this type will not be printed.
PaigeMiller
Diamond | Level 26

Please show us the ENTIRE log, with nothing chopped out. Please click on the </> icon and paste your log as text into the window that appears.

--
Paige Miller
dmarques1998
Obsidian | Level 7
 
Kurt_Bremser
Super User

Did it EVER occur to you that the type of variables might be important??

I can only develop code for data that I see; if you want to play practical jokes, I'm outta here.

PaigeMiller
Diamond | Level 26

Thanks @dmarques1998 . Obviously, you are not working with the data that you showed in your original post. We would need to see (a portion of) your actual data, provided as you have done previously as SAS data step code.

--
Paige Miller
FreelanceReinh
Jade | Level 19

Hi @dmarques1998,

 

So, you're working with character variables. An alternative to using a DO loop (which is a perfectly valid approach) is to use SAS functions.

 

Example:

data have;
input (id var1-var12)($);
cards;
A	1 2 3 . . . . . 1 1 1 3
B	3 3 2 1 3 2 1 . . . . .
C	. . . . 1 2 3 1 2 3 2 .
D	. . . . 3 1 1 . 3 . 1 .
E	. . . . 1 3 1 3 1 3 . .
F	3 2 1 . . . . . 1 1 1 3
G	. . . . . . . . . . . .
H	1 1 1 1 1 1 1 1 1 1 1 1
;

data want;
set have;
array _v[*] var1-char-var12; /* name range list, assuming that real variable names are not var1 etc. */
if cmiss(of _v[*])=dim(_v) then n_miss=.; /* or whatever you want n_miss to be in this case */
else n_miss=cmiss(of _v[*])-whichc(coalescec(of _v[*]), of _v[*])+1;
run;
Ksharp
Super User
data example;
infile cards expandtabs truncover;
  input id $ var1 var2 var3 var4 var5 var6 var7 var8 var9 var10 var11 var12;
cards;
A	1 2 3 . . . . . 1 1 1 3
B	3 3 2 1 3 2 1 . . . . .
C	. . . . 1 2 3 1 2 3 2 .
D	. . . . 3 1 1 . 3 . 1 .
E	. . . . 1 3 1 3 1 3 . .
F	3 2 1 . . . . . 1 1 1 3
; 
options missing='.';
data want;
 set example;
 want=lengthn(scan(cats(of var:),-1,,'d'));
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 12 replies
  • 1179 views
  • 3 likes
  • 5 in conversation