BookmarkSubscribeRSS Feed
3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

 

Well, you can hold the data over with retain, and then control the output using output statement as given below.  However I would advise you to get more ata there to help.  As it stands with what you have provided there is not enough information to do this properly, i.e. a change in sort alters that data completely.  Normally on these type of questions you would reverse sort the data, then retain last value, much simpler and robust.  So get something in the data which indicates the order and make your life easier.

data have;
  input id d value;
datalines;
1 1 80
1 0 100
1 1 100
1 0 120
1 1 30
1 0 50
;
run;

data want;
  set have end=last;
  retain out_id out_d out_value;
  if _n_=1 then do;
    out_id=id;
    out_d=d;
    out_value=value;
  end;
  else do;
    if d=0 and out_d=1 then do;
      out_value=value;
      output;
    end;
    else output;
  end;
  if last then output;
run;
ari
Quartz | Level 8 ari
Quartz | Level 8
Thanks RW. Yes reverse sorting and retaining is the best solution.
Oligolas
Barite | Level 11
data hlp1;
   set have;
   retain hlp_group;
   if mod(_N_,2) then hlp_group+1;
run;
proc sort data=hlp1 out=hlp2; by hlp_group id d; run;

data hlp3;
   set hlp2;
   by hlp_group id d;
   hlp_lag1_value=lag1(value);
   if not first.hlp_group then value=hlp_lag1_value;
run;

PROC SORT DATA=hlp3 out=want(keep=id d value); by hlp_group id descending d;run;
________________________

- Cheers -

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 2545 views
  • 0 likes
  • 3 in conversation