BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
TheNovice
Quartz | Level 8

There is data that I have sorted using the following code:

 

proc sort data=RESTORED ; by ID ent_seq_no descending col_actv_code ; quit;

 

I get the following. below is an example of a type of group in the data. What I want to do is delete these records( all of them) if the last actv_code = 'S'. I only want to delete the group of ID's if the last code is S. I cannot figure out how.

 

ID      ENT_SEQ_NO  ACTV_CODE
0935     913240851          S
0935     913863008            K
0935     915287558          S
0935     915733042          K
0935     919660717          S

 

 

Data r ;
set restored;
by ban ent_seq_no descending col_actv_code;
if not last.ban then delete;
run;

 

Any direction is appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Using the variable names from your sample data, this would be one way:

 

data want;

   do until (last.id);

      set have;

      by id;

   end;

   if actv_code='S' then output_flag='N';

   else output_flag='Y';

   do until (last.id);

      set have;

      by id;

      if output_flag='Y' then output;

   end;

   drop output_flag;

run;

View solution in original post

3 REPLIES 3
heffo
Pyrite | Level 9

This could work:

data _null_;
	set restored end=lastRow;
	length toBeDeleted $ 500;
	by ID;
	if last.id and upcase(col_actv_code) = "S" then do;
			toBeDeleted = cat(strip(toBeDeleted)," '",strip(ID),"' ");
	end;
	if lastRow then call symputx ("toBeDeleted",toBeDeleted);
run;

proc sql noprint;
	create table Restored_2 as 
	select * from Restored 
	where ID not in (&toBeDeleted);
quit;

Potentially you could sort your data the other way around, so that the first row is the S and if so, then you make sure that you don't have an output command for the rest of the rows in that id. But you then have to resort the data again afterwards.

proc sort data=RESTORED out=RESTORED_opp; 
	by ID descending ent_seq_no col_actv_code ; 
run;

data restored_2;
	set restored_opp;
	by ID;
	retain _Keep_ID 1;
	if first.id and upcase(col_actv_code) = "S" then do;
		_Keep_ID = 0;
	end;
	if _Keep_ID = 1 then output;
	if last.id then _Keep_ID = 1;
run;

 

Astounding
PROC Star

Using the variable names from your sample data, this would be one way:

 

data want;

   do until (last.id);

      set have;

      by id;

   end;

   if actv_code='S' then output_flag='N';

   else output_flag='Y';

   do until (last.id);

      set have;

      by id;

      if output_flag='Y' then output;

   end;

   drop output_flag;

run;

TheNovice
Quartz | Level 8

Thank you so much!! this worked. Sorry for the delay in response

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 700 views
  • 2 likes
  • 3 in conversation