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
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.