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

Hi everyone,

I'm trying to multiply my ratio column with lagged have value to get to the want colum. What approach should I take?

 

data have;
input year ratio have want;
cards;
2021 1 5  5
2022 2 . 10
2023 3 . 30
2024 4 . 120
2025 5 . 600
;
run;

Thank you for your time.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Why is the value of HAVE missing for all but the first observation?

data have;
  input year ratio have expect;
cards;
2021 1 5  5
2022 2 . 10
2023 3 . 30
2024 4 . 120
2025 5 . 600
;

data want;
  set have;
  retain prod;
  if _n_=1 then prod=have;
  else prod=prod*ratio;
run;

What if RATIO on the first observation is not 1 what do you want for the new running product variable?  If you want it to be HAVE*RATIO then remove the ELSE keyword.

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

... multiply my ratio column with lagged have value ...

 

So doing the math, on row 4 the lagged have value would have to be 30. I don't really see how the lagged have value gets to be 30. Please explain further.

--
Paige Miller
ted_lariat
Fluorite | Level 6

Apologies, I'll clarify. 

data have;
input year ratio have have2 want;
cards;
2021 1 5 . 5
2022 2 . 5 10
2023 3 . 10 30
2024 4 . 30 120
2025 5 . 120 600
;
run;

I've updated the sample code. I only have ratio and have, I'm trying to generate have2, and finally want. It's been a while since I've written a sas code, so I'm a bit rusty.

 

To elaborate further, 

for 2022, I want to multiply, ratio2022 with have2021

for 2023, I want to multiply, ratio 2023 with have 2022 so on.

Tom
Super User Tom
Super User

Why is the value of HAVE missing for all but the first observation?

data have;
  input year ratio have expect;
cards;
2021 1 5  5
2022 2 . 10
2023 3 . 30
2024 4 . 120
2025 5 . 600
;

data want;
  set have;
  retain prod;
  if _n_=1 then prod=have;
  else prod=prod*ratio;
run;

What if RATIO on the first observation is not 1 what do you want for the new running product variable?  If you want it to be HAVE*RATIO then remove the ELSE keyword.

ted_lariat
Fluorite | Level 6
thanks, it is working like a charm. much appreciated.

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 714 views
  • 2 likes
  • 3 in conversation