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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
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!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.