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

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
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

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.

 

 

--
Paige Miller
zihdonv19
Obsidian | Level 7

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.

Tom
Super User Tom
Super User

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;
zihdonv19
Obsidian | Level 7

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"?

Tom
Super User Tom
Super User

@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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 488 views
  • 0 likes
  • 3 in conversation