BookmarkSubscribeRSS Feed
SAS_Muggle
Fluorite | Level 6

I have some data like the below:

 

Group Sort Value 

1 1 50

1 2 55

1 3 60

1 4 70

2 1 40

2 2 60

2 3 40

2 4 90

 

I want to create 2 new variables x and y such that if sort=1 then x=0 and y=value, otherwise x=value from previous row y and y= x + value, so like below:

 

Group Sort Value x y

1 1 50 0 50

1 2 55 50 105

1 3 60 105 165

1 4 70 165 135

2 1 40 0 40

2 2 60 40 100

2 3 40 100 140

..

 

Can anyone help with code. Whenever I run using lag and by sort and group, the new y does update based on the previous calculation. 

3 REPLIES 3
WarrenKuhfeld
Ammonite | Level 13

Be sure to create the lag unconditionally, even if you use the value conditionally.

 

Be sure to read the documentation. If you have expectations on the behavior of lag as a matrix operator, you will find that they don't apply to a function in a general language like the SAS DATA step.

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n0l66p5oqex1f2n1quuopdvtcjqb.h...

Ksharp
Super User
data have;
input Group Sort Value; 
cards;
1 1 50
1 2 55
1 3 60
1 4 70
2 1 40
2 2 60
2 3 40
2 4 90
;
data want;
 set have;
 retain _y;
 if sort=1 then do;x=0;y=value;_y=value;end;
  else do;x=_y;y=x+value;_y=y; end;
 drop _y;
run;
mkeintz
PROC Star

Do you really want to check for SORT=1,  or check for start of a new group.  Of course they are the same if SORT=1 is reliably the first (and only) observation of each incoming group.   There is a way to use LAG, as here:

 

data have;
input Group Sort Value  ; 
cards;
1 1 50
1 2 55
1 3 60
1 4 70
2 1 40
2 2 60
2 3 40
2 4 90
;run;

data want;
  set have;
  by group;
  x+lag(value);  if first.group then x=0;
  y+value;       if first.group then y=value;
run;

Of course you could use  "IF sort=1"  instead of "IF first.group".  

 

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 530 views
  • 2 likes
  • 4 in conversation