DATA Step, Macro, Functions and more

first non missing value

Reply
Contributor
Posts: 59

first non missing value

i have a datset 


data test;
input store sales;
cards;
1 .
1 200
1 300
1 .
2 .
2 300
2 150
2 560
3 900
3 560
3 .
;
run;

i want the first non missing value of each store?

Regular Contributor
Posts: 234

Re: first non missing value

[ Edited ]
data test;
input store sales;
cards;
1 .
1 200
1 300
1 .
2 .
2 300
2 150
2 560
3 900
3 560
3 .
;
run;

data want;
set test;

if not missing(sales) then
output;
run;

data want2;
set want;
by store;

if first.store;
run;
Respected Advisor
Posts: 3,124

Re: first non missing value

[ Edited ]

Here is a one-step solution, for the sake of efficiency:

data want;

set test;

by store sales notsorted;

ct=ifn(first.store,0,ct);

ct+(first.sales and not missing(sales));

if ct=1;

drop ct;

run;

Super User
Posts: 5,083

Re: first non missing value

It's easy with a WHERE statement:

 

data want;

set have;

by store;

if first.store;

where sales > .;

run;

Ask a Question
Discussion stats
  • 3 replies
  • 243 views
  • 1 like
  • 4 in conversation