## Create an array of lag functions for150+ variables

Occasional Contributor
Posts: 9

# Create an array of lag functions for150+ variables

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.

Super User
Posts: 23,700

## Re: Create an array of lag functions for150+ variables

Post example of your data, what you have and what you want.

Occasional Contributor
Posts: 9

## Re: Create an array of lag functions for150+ variables

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;

Discussion stats
• 2 replies
• 1111 views
• 0 likes
• 2 in conversation