Hi I have a query please advise.
Data I have:
| ID | Tx | Flag |
| 1 | NA | Unknown |
| 1 | yes | Y |
| 1 | yes | Y |
| 2 | NA | Unknown |
| 3 | NA | Unknown |
| 3 | yes | Y |
| 3 | yes | Y |
Data I Want:
| ID | Tx | Flag |
| 1 | yes | Y |
| 1 | yes | Y |
| 2 | NA | Unknown |
| 3 | yes | Y |
| 3 | yes | Y |
I want to delete those records which has Tx as 'NA' and Flag as 'Unknown', however if there is only one record for any ID, I dont want to delete that record even if the condition satisfies.
Ex: For ID 2 there is only one record but it satisfies my condition to delete but I dont want to delete ID 2 as there is only one record.
Please advise.
Thank you
Try this
data have;
input ID Tx $ Flag $;
datalines;
1 NA Unknown
1 yes Y
1 yes Y
2 NA Unknown
3 NA Unknown
3 yes Y
3 yes Y
;
data want;
set have;
by id;
if Tx = 'NA' & Flag = 'Unknown' & sum(first.id, last.id) ne 2 then delete;
run;
Result:
ID Tx Flag 1 yes Y 1 yes Y 2 NA Unknown 3 yes Y 3 yes Y
Try this
data have;
input ID Tx $ Flag $;
datalines;
1 NA Unknown
1 yes Y
1 yes Y
2 NA Unknown
3 NA Unknown
3 yes Y
3 yes Y
;
data want;
set have;
by id;
if Tx = 'NA' & Flag = 'Unknown' & sum(first.id, last.id) ne 2 then delete;
run;
Result:
ID Tx Flag 1 yes Y 1 yes Y 2 NA Unknown 3 yes Y 3 yes Y
Oh gee.I did a slight test of double if. I hope somebody corrects it.
data have;
infile datalines dlm="09"x;
input ID Tx $ Flag $;
datalines;
1 NA Unknown
1 yes Y
1 yes Y
2 NA Unknown
3 NA Unknown
3 yes Y
3 yes Y
;
run;
data want;
set have;
by id;
*Double if statement;
if tx="NA" and upcase(FLAG)="UNKNOWN" then
if not first.id or not last.id then delete;
run;
data want;
set have;
by id;
if (first.id and last.id /* only one obs */) or tx ne "NA" or flag ne "Unknown";
run;
If it is possible that an id has only multiple "NA"/"Unknown" observations, a double DO loop must be used to determine that beforehand.
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.