I need to do a lag function for an id observation which I have working manually, but the number of lag I need keeps changing. So can I use an array to help.
I use an array to create the variables at first:
data amort4; set amort2;
array idT (151) $50. id1-id151;
do i = 1 to 151;
end;
run;
Second step is
Then I am manually writing an lag for each (id)
data amort5; set amort4;
id1 = lag1(id);
id2 = lag2(id);
id3 = lag3(id);
id4 = lag4(id); /* this goes all the way to 151 with lots of manual typing */
id151 = lag151(id);
run;
Third step is:
Then I basically sort closet to farthest to get the id moved over. I know this is doing things the long way
data amort6; *length idf $50. ; set amort5;
if id1 ^= "" then idf = id1;
else if id2 ^= " " then idf = id2;
else if id3 ^= " " then idf = id3;
else if id4 ^= " " then idf = id4; /* .... this goes on until id151 */
else if id150 ^= " " then idf = id150;
else idf = id151;
This gets me an id for every row. Which is alot of manual coding. I would like to be able to use an array or two to set the number of vars created in only one spot.
Post example of your data, what you have and what you want.
I figured it out never mind:
%macro check;
data amort5;
set amort2;
%do i = 1 %to 400;
id&i = lag&i(id);
%end;
%mend check;
%check;
%macro still;
data amort6; set amort5;
if id1 ^= " " then id = id1;
%do i = 2 %to 400;
else if id&i ^= " " then id = id&i;
%end;
drop id2-id400
%mend still;
%still;
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 16. 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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: