BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rajat051984
Fluorite | Level 6

I have the following dataset. 

 

 

data input;
input trans_yearmon 8. rv_acct 5.2  factor 8.2   ;
datalines;
201506 0.21 
201507       -0.62
201508       1.65
201509       0.09
201510       0.28
;
run;

I want rv_act to be = lag(rv_act) + factor.
So the new dataset should look as follows -

 

 

 

  B C D E
1   rv_acct factor rv_acct_formula
2 201506 0.21    
3 201507 -$0.41 -$0.62 C3 = C2 + D3
4 201508 $1.24 $1.65 C4 = C3 + D4
5 201509 $1.33 $0.09 C5 = C4 + D5
6 201510 $1.61 $0.28 C6 = C5 + D6

 

Can you kindly tell me how to do it in SAS ? I was using 'lag operator' but that was not helpful ..  

 

-Rajat

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data input;
input trans_yearmon  rv_acct  factor   ;
datalines;
201506 0.21  .
201507   .    -0.62
201508  .     1.65
201509    .   0.09
201510   .    0.28
;
run;
data want;
 set input;
 retain lag;
 if _n_=1 then lag=rv_acct ;
  else do;
   rv_acct=lag+factor;lag=rv_acct;
  end;
drop lag;
run;

View solution in original post

3 REPLIES 3
Reeza
Super User

Lag is correct methodology. Post your code that didn't work. 

Reeza
Super User

Sorry, just use retain. 

 

Retain rv_acct;

 

rv_account+factor;

 

This should be a new variable in the dataset otherwise you might get unexpected results. 

Ksharp
Super User
data input;
input trans_yearmon  rv_acct  factor   ;
datalines;
201506 0.21  .
201507   .    -0.62
201508  .     1.65
201509    .   0.09
201510   .    0.28
;
run;
data want;
 set input;
 retain lag;
 if _n_=1 then lag=rv_acct ;
  else do;
   rv_acct=lag+factor;lag=rv_acct;
  end;
drop lag;
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1505 views
  • 2 likes
  • 3 in conversation