I have worked with SAS for years but am mostly self taught and am trying to improve the robustness and flexibility of some of the legacy code I am working with. Essentially, I need to self join a table with many variables to get YoY changes, but I want to be able to just list out specific variables to include.
A simple version of the data I'm working with is like this
Year State X1 X2 X3 ... X50
2005 TX 17 5 8.... 2
2006 TX 18 9 7.... 3
While it would be easy for me to write a loop that does this join for every variable, that is computationally to much. What I want is a join like below, except easy to change the variables.
PROC SQL;
Create Table YoY_Selected AS
Select a.*
, b.X2/a.X2 AS X2_YOY
, b.X30/a.X30 AS X30_YOY
From My_Table AS a
Left Join My_Table AS b
on a.year=b.year+1
and a.state=b.state;
Quit;
Now, If I had a list, such as %Let YoY_List = X2 X30 X32 X48; Is it possible to write a macro that will it into the bottom half of the above join?
Thank you in advance for any insight or help! I've spent half a day at least on search engines and am coming up empty.
PS) Some time years are missing for some states, hence it needs to be a join (not lag in data step), plus its actually quarters and what I really need to calculate is a moving average.
... View more