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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.