ID | value |
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 |
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:
ID | value | desired |
1 | 0 | 0 |
1 | 0 | 0 |
1 | 100 | 100 |
1 | 200 | 200 |
2 | 0 | 0 |
2 | 0 | 0 |
2 | 200 | 200 |
2 | 0 | 200 |
2 | 0 | 200 |
3 | 0 | 0 |
3 | 100 | 100 |
3 | 50 | 100 |
3 | 0 | 100 |
3 | 200 | 200 |
My dataset has 153 million records, is there an easy way to do this using data step functions?
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.
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;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.