Hello All,
I have data that takes the following form:
And I am trying to create a variable that defines the following equation:
Variable_j,s = [Assets_j,s - Assets_j,s-1 * (1 + Returns_j,s-1)] / Assets_j,s-1
where j represents firm number and s represents the month. The data above only shows firm 4, but I have lots of data like this for lots of firms.
So how can I create a new column with this information for every month? The equation includes data from the current month as well as data from the prior month.
Any help would be greatly appreciated!!
Thanks,
John
Posting data instead of a picture would have allowed some testing and prevented typos
data performance ;
set input_data ;
by fund;
old_ass = lag( assets ) ;
old_ret = lag( return ) ;
if first.fund then Variable = . ;
else Variable = {Assets - old_Ass * (1 + old_ret ) ) / old_Ass ;
drop old: ;
run ;
PG
depending on column names, something like this might work
data performance ;
set input_data ;
by fund month ;
old_ass = lag( assets ) ;
old_ret = lag( return ) ;
if first.firm then Variable = . ;
else Variable = {Assets - old_Ass * (1 + old_ret ) ) / old_Ass ;
drop old_ass old_ret ;
run ;
Message was edited by: Peter Crawford after review by @reeza
lag vs leg
Look up the lag and retain functions.
In SAS arrays work differently than other languages, so you'll actually need to use different constructs to get what you need.
Posting data instead of a picture would have allowed some testing and prevented typos
data performance ;
set input_data ;
by fund;
old_ass = lag( assets ) ;
old_ret = lag( return ) ;
if first.fund then Variable = . ;
else Variable = {Assets - old_Ass * (1 + old_ret ) ) / old_Ass ;
drop old: ;
run ;
PG
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.