Hi,
I need help in creating the dataset, which gets the previous year value, or the base value when null.
data have;
input product $ scenario year value;
cards;
MOR 0 2016 800
MOR 0 2017 100
MOR 0 2018 0
MOR 0 2019 0
MOR 1 2017 0
MOR 1 2018 700
MOR 1 2019 600
;
run;
/*If it's Scenario 1, year 2017, and Value = 0, I should get the 2016 value of 800, functioning something like a base value*/
/*Else, I just get the value of the previous year*/
data want;
input product $ scenario year value;
cards;
MOR 0 2016 800
MOR 0 2017 100
MOR 0 2018 100
MOR 0 2019 100
MOR 1 2017 800
MOR 1 2018 700
MOR 1 2019 600
;
run;
There are hundreds of examples on here of the use of lag() function and retained variables. I personally prefer retained variables:
data want (drop=lstvalue); set have; retain lstval; lstval=ifn(value ne 0,value,lstval); if value=0 then value=lstvalue; run;
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!
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.