Hi!
I have a dataset like the one below.
I wish to extra the id where the variable "atc" only has "A10A".
So, in this example, I would extract id=282 and id=299.
Does anyone know how to achieve this in SAS? Thank you!!
id | atc |
233 | A10A |
233 | A10A |
233 | C05 |
233 | V90A |
233 | V90A |
233 | V90A |
233 | V90A |
233 | G560 |
233 | G561 |
233 | G562 |
282 | A10A |
282 | A10A |
282 | A10A |
282 | A10A |
299 | A10A |
Your data seems sorted, so let's use that to our advantage.
data want;
set have;
by id ;
if first.id then other=0;
retain other;
if atc ne "A10A" then other=1;
if last.id and not other;
run;
It would be extremely helpful, in this problem and all future problems, if you SHOW US the desired output.
Since you haven't done that, please do so now. Please also explain why rows 1 and 2, where atc="A10A" are not in the output.
Hi!
Thanks for your reply.
The output I'd like to have is:
id |
282 |
299 |
Rows 1 and 2 should not be in the output, because the atc value for id "233" is not restricted to "A10A". For example, id 233 also has atc value of "C05", "V90A" etc.
Your data seems sorted, so let's use that to our advantage.
data want;
set have;
by id ;
if first.id then other=0;
retain other;
if atc ne "A10A" then other=1;
if last.id and not other;
run;
Thanks for you reply Tom. Your solution works!
May I further ask does "if last.lopnr and not other" always do the same as "if last.lopnr and other=0"?
@zihdonv19 wrote:
Thanks for you reply Tom. Your solution works!
May I further ask does "if last.lopnr and not other" always do the same as "if last.lopnr and other=0"?
In this case yes. But in the general case it is more like
if last.lopnr and (other=0 or missing(other));
SAS will evaluate a zero or missing value as FALSE and any other value as TRUE.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.