BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
MaxiHösi
Fluorite | Level 6

Hello everyone,

I have already tried everything possible but i can't make any progress here. I want to replicate a formula in SAS that originates from Excel. The problem is that the formula relies on values that were calculated in the previous row. To make matters more complicated, there are different type of cars in the table. The table is structured as follows: There is one column with the car_types, one column  with the quarters, one column  with the values, one with the factor and in the 5th column I want to output the calculated value as result.

 

The formula is as follows:

result = Value1 + 0.6 * Value2 (where value2 is composed of the sum from the row above of Value1 + 0.6 * value2).

 

This is what i have:

car_typeQuartersValue 1factor

Skoda

Q3 2022

-0.020.6
SkodaQ4 2022-0.240.6
SkodaQ1 20230.140.6
SkodaQ2 20230.450.6
Vauxhall.........

This is what i want:

car_typeQuartersValue 1Value 2 factorresult

Skoda

Q3 2022

-0.020.6-0.02
SkodaQ4 2022-0.24-0.020.6-0.252
SkodaQ1 20230.14-0.2520.6-0.0112
SkodaQ2 20230.45-0.01120.60.44328
Vauxhall...............
...............

...

 

When a new car_type starts, the initial value of value2 is 0.

 

Do you have any idea how i can implement this in sas?

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @MaxiHösi,

 

Use the retained value of result for Value2:

data have;
input car_type :$20. Quarters :yyq. Value1 factor;
format Quarters nldateyqm.;
cards;
Skoda    2022Q3 -0.02 0.6
Skoda    2022Q4 -0.24 0.6
Skoda    2023Q1  0.14 0.6
Skoda    2023Q2  0.45 0.6
Vauxhall 2022Q1  0.31 0.4
Vauxhall 2022Q2 -0.27 0.4
;

data want;
set have;
by car_type Quarters;
Value2=ifn(first.car_type,0,result);
result=Value1+factor*Value2;
retain result;
run;

View solution in original post

2 REPLIES 2
FreelanceReinh
Jade | Level 19

Hello @MaxiHösi,

 

Use the retained value of result for Value2:

data have;
input car_type :$20. Quarters :yyq. Value1 factor;
format Quarters nldateyqm.;
cards;
Skoda    2022Q3 -0.02 0.6
Skoda    2022Q4 -0.24 0.6
Skoda    2023Q1  0.14 0.6
Skoda    2023Q2  0.45 0.6
Vauxhall 2022Q1  0.31 0.4
Vauxhall 2022Q2 -0.27 0.4
;

data want;
set have;
by car_type Quarters;
Value2=ifn(first.car_type,0,result);
result=Value1+factor*Value2;
retain result;
run;
MaxiHösi
Fluorite | Level 6

Thank you for your  quick response. Since I had missing values, I had to use sum(...), but otherwise, everything worked wonderfully.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 385 views
  • 1 like
  • 2 in conversation