DATA Step, Macro, Functions and more

finding first non missing variable

Reply
Super Contributor
Posts: 266

finding first non missing variable

 Hi All,

 

i have data like below:

 

data test;
input a b c;
datalines;
1 0 0
0 1 0
0 1 1
1 1 1
;
run;

 

i need output with same data added one more variable result as

 

a b c Result

1 0 0 A1

0 1 0 B2

0 1 1 B3

1 1 1 A4

 

please suggest to get the result

 

Super Contributor
Posts: 308

Re: finding first non missing variable

Hello,

 

Could you post the code you tried and / or describe what are the diificulties of building your own solution.

 

Super Contributor
Posts: 266

Re: finding first non missing variable

I am not able to imageing anything on it or i can try array and "if " conditions but its not seems good. Can u please suggest if any thing in sort available.

 

Thanks in advance

Super User
Posts: 19,865

Re: finding first non missing variable

[ Edited ]

Your last component appears to be the row and independent of the variable. You can get the row nimbler by using _n_.

 

You say first non missing value but you don't have any missing values in your data set. 

 

Use WHICHN and VNAME to find the variable name

 

Array var_list(3) a b c;

Index = whichn(1, of var_list(*));
First_var = vname(var_list(index));
Super Contributor
Posts: 266

Re: finding first non missing variable

0 as missing value and 1 can be anything.. just to simplify i used 0 and 1,

 

 

Super User
Posts: 19,865

Re: finding first non missing variable

Making sample data that doesn't reflect your situation means having to answer your question twice. 

 

Youll have to loop through the array then to find your first non missing value. You can use LEAVE to exit a loop. 

 

VNAME can still be used to find variable name and _n_ for row number.  

 

Ask a Question
Discussion stats
  • 5 replies
  • 505 views
  • 2 likes
  • 3 in conversation