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 ;;;;
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.
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.
Merge the dataset with itself:
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.It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.