BookmarkSubscribeRSS Feed
Thalo
Calcite | Level 5

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.

2 REPLIES 2
Reeza
Super User

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

Thalo
Calcite | Level 5

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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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