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 -

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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