DATA Step, Macro, Functions and more

Data step reatin first non zero value by group

Reply
Contributor
Posts: 30

Data step reatin first non zero value by group

IDvalue
10
10
1100
1200
20
20
2200
20
20
30
3100
350
30
3200

 

Hi ,

 My data looks like this. Variable 'value' is an amount field and its a LTD field, once it gets populated it should never decrease or become zero. Dollar amount in the 'value' field could only grow. So the requirement is to retain greater value in each by group (ID).

 

desired output:

 

IDvaluedesired
100
100
1100100
1200200
200
200
2200200
20200
20200
300
3100100
350100
30100
3200200

 

 

My dataset has 153 million records, is there an easy way to do this using data step functions?

Super User
Posts: 11,343

Re: Data step reatin first non zero value by group

data have;
 input ID  value ;
datalines;
1 0 
1 0 
1 100 
1 200 
2 0 
2 0 
2 200 
2 0 
2 0 
3 0 
3 100 
3 50 
3 0 
3 200 
;
run;

data want;
  set have;
  by id notsorted;
  retain  desired;
  if first.id then desired=0;
  if value gt desired  then desired=value;
run;

I used the NOTSORTED option for your ID variable as it is not clear whether the data is actually sorted by ID or that ID may occur in different sequences of records.

 

Super User
Posts: 10,028

Re: Data step reatin first non zero value by group

data have;
 input ID  value ;
datalines;
1 0 
1 0 
1 100 
1 200 
2 0 
2 0 
2 200 
2 0 
2 0 
3 0 
3 100 
3 50 
3 0 
3 200 
;
run;
data want;
 set have;
 by id;
 retain want . ;
 if first.id then call missing(want);
 want=max(want,value);
run;
Ask a Question
Discussion stats
  • 2 replies
  • 111 views
  • 0 likes
  • 3 in conversation