Creating a variable dependent on previous variable

Reply
Occasional Contributor
Posts: 5

Creating a variable dependent on previous variable

Hi, I am trying to calculate a varible in SAS which is dependant on the previously calculated varibles.  I thought this would be simple with the following code but it does not compute.

data test1;

set rate;

if id_perd = '1' then Dis_fact= 1/(1+((Swap_Spot_Rate)/12));

else if id_perd > '1' then Dis_fact = (1/(1+((Swap_Spot_Rate)/12)))*lag(Dis_fact);

run;

Is it possible to calculate a varible dependant on the previously calculated varible in sas?  All help would be appreciate.  Thanks! Patrick B.

Respected Advisor
Posts: 3,799

Re: Creating a variable dependent on previous variable

You can probably just RETAIN DIS_FACT instead of LAGing, and just use DIS_FACT on the right instead of LAT(DIS_FACT);

Occasional Contributor
Posts: 5

Re: Creating a variable dependent on previous variable

Posted in reply to data_null__

Thanks for the help!

Super User
Posts: 5,516

Re: Creating a variable dependent on previous variable

Patrick,

It should be as easy as 1 2 3.

First, make sure that RATE does not already contain a variable named DIS_FACT.

Second, get rid of the LAG function entirely.

Third, add this statement to the DATA step:

retain dis_fact;

That should do it.

Occasional Contributor
Posts: 5

Re: Creating a variable dependent on previous variable

Posted in reply to Astounding

Thanks for the help!

Ask a Question
Discussion stats
  • 4 replies
  • 225 views
  • 2 likes
  • 3 in conversation