BookmarkSubscribeRSS Feed
sivakoya
Obsidian | Level 7
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?

2 REPLIES 2
ballardw
Super User
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.

 

Ksharp
Super User
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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1890 views
  • 0 likes
  • 3 in conversation