BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
HEB1
Calcite | Level 5

Hello,

my data looks like that:

year         ID       INDICAOTR

1990        Y               1

1991         x               0

1993        x                1

1994         x              0

1995         x                0

1996          x               2

I look for a code that will change the rows ABOVE same ID to have the last indicator. what I want my data to look like is:

year         ID       INDICAOTR

1990        y               1

1991         x               1

1993         x                1

1994         x             2

1995         x                2

1996          x               2

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Assuming your actual data is more like this where YEAR is nested within ID

data have;
  input id $ year indicator;
cards;
x 1991 0
x 1993 1
x 1994 0
x 1995 0
x 1996 2
y 1990 1
;

Then you could use a double DOW loop.  The first loop to find the next non zero indicator.  And the second the read back the same observations and write them out with the new calculated variable.

data want;
  do _n_=1 by 1 until (last.id or indicator);
    set have;
    by id year;
  end;
  new_indicator=indicator;
  do _n_=1 to _n_;
    set have;
    output;
  end;
  drop indicator;
  rename new_indicator=indicator;
run;

Results:

Obs    id    year    indicator

 1     x     1991        1
 2     x     1993        1
 3     x     1994        2
 4     x     1995        2
 5     x     1996        2
 6     y     1990        1

View solution in original post

3 REPLIES 3
Astounding
PROC Star

Here's a program that produces the results in your post.

data want;
   do until (last.id or indicaotr ne 0);
      set have;
      by id notsorted;
   end;
   final_indicaotr = indicaotr;
   do until (last.id or indicaotr ne 0);
      set have;
      by id notsorted;
      output;
   end;
run;

I hope I picked out the right result from your example.  Try it and see.

Tom
Super User Tom
Super User

Assuming your actual data is more like this where YEAR is nested within ID

data have;
  input id $ year indicator;
cards;
x 1991 0
x 1993 1
x 1994 0
x 1995 0
x 1996 2
y 1990 1
;

Then you could use a double DOW loop.  The first loop to find the next non zero indicator.  And the second the read back the same observations and write them out with the new calculated variable.

data want;
  do _n_=1 by 1 until (last.id or indicator);
    set have;
    by id year;
  end;
  new_indicator=indicator;
  do _n_=1 to _n_;
    set have;
    output;
  end;
  drop indicator;
  rename new_indicator=indicator;
run;

Results:

Obs    id    year    indicator

 1     x     1991        1
 2     x     1993        1
 3     x     1994        2
 4     x     1995        2
 5     x     1996        2
 6     y     1990        1
HEB1
Calcite | Level 5
THANK YOU SO MUCH!!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 511 views
  • 0 likes
  • 3 in conversation