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

Hi...I would like to keep the record with the Type of Plan A only if is the only Type for the ID otherwise delete the record. Any Suggestions. Thanks

 

 

data Have;
    length ID 8 Type $ 6;
    format ID best12. Type  $char6.;
    informat ID best12. Type $char6.;
    infile datalines4 dlm='7F'x missover dsd;
    input ID : best32. Type : $char6.;
datalines4;
100Plan A
100Plan B
100Plan C
101Plan A
101Plan B
102Plan A
102Plan C
103Plan A
104Plan B
105Plan C
106Plan B
106Plan C
;;;;

data Want;
    length ID 8 Type $ 6;
    format ID best12. Type  $char6.;
    informat ID best12. Type $char6.;
    infile datalines4 dlm='7F'x missover dsd;
    input ID : best32. Type : $char6.;
datalines4;
100Plan B
100Plan C
101Plan B
102Plan C
103Plan A
104Plan B
105Plan C
106Plan B
106Plan C
;;;;
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Try this:

 

data want;
   set have;
   by id ;
   if type='Plan A' then if not(last.id) then delete;
run;

 

 

If there is only one record for a by group then the First. and Last. are both true (1). In some cases you may want an explicit comparison of

   if type=<value> then if (first.id and not(last.id)) then delete;

as your example was the "first" Type value by sort and presented order. 

View solution in original post

3 REPLIES 3
ballardw
Super User

Try this:

 

data want;
   set have;
   by id ;
   if type='Plan A' then if not(last.id) then delete;
run;

 

 

If there is only one record for a by group then the First. and Last. are both true (1). In some cases you may want an explicit comparison of

   if type=<value> then if (first.id and not(last.id)) then delete;

as your example was the "first" Type value by sort and presented order. 

twildone
Pyrite | Level 9
Thanks BallardW…..it worked perfectly.
Kurt_Bremser
Super User

Merge the dataset with itself:

  1. data want;
    merge
      have (in=a)
      have (
        in=b
        keep=id type /* in case there are more variables */
        rename=(type=_type)
        where=(_type ne "A")
      )
    ;
    by id;
    if b and type = "A" then delete;
    run;
    Untested, posted from my tablet.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—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
  • 943 views
  • 0 likes
  • 3 in conversation