BookmarkSubscribeRSS Feed
angeliquec
Quartz | Level 8

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;

1 REPLY 1
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1 reply
  • 1149 views
  • 0 likes
  • 2 in conversation