BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dr2014
Quartz | Level 8

I need to select obervations which are groupd by variable 'id', where the values for the 2nd variable 'Assignment' is identical i.e 'Z' at all instances of the 'date variable for the id i.e id=2. Please see table below of the original dataset and final dataset needed. Any suggestions on how I can go about this? Please advise.

 

Original_dataset

 

 

ID

Assignment

Date

1

Z

date1

1

D

date2

1

B

date3

1

B

date4

2

Z

date1

2

Z

date2

2

Z

date5

3

Z

date2

3

M

date4

3

Z

date6

 

 

 

Final dataset

 

 

ID

Assignment

Date

2

Z

date1

2

Z

date2

2

Z

date5

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

i think you want all observations for any ID which only has assignment='Z' (i.e. count of non-z's =0).  If so, then this will work:

 

data want;
  do notz=0 by 0 until (last.id);
    set have;
    by id ;
    if assignment^='Z' then notz=notz+1;
  end;

  do until (last.id);
    set have;
    by id;
    if notz=0 then output;
  end;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

4 REPLIES 4
ballardw
Super User

I'm not seeing and grouping going on just selection.

 

Data want;

   set have;

    where Id=2 and assignment='Z';

run;

 

That may be Id='2' above, I cannot tell whether your Id variable is numeric or character.

ChrisNZ
Tourmaline | Level 20

Like this?

 


proc sql;
select *    
from HAVE
group by ID
having count(unique ASSIGNMENT)=1;
quit;
mkeintz
PROC Star

i think you want all observations for any ID which only has assignment='Z' (i.e. count of non-z's =0).  If so, then this will work:

 

data want;
  do notz=0 by 0 until (last.id);
    set have;
    by id ;
    if assignment^='Z' then notz=notz+1;
  end;

  do until (last.id);
    set have;
    by id;
    if notz=0 then output;
  end;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
dr2014
Quartz | Level 8

Thanks @    . Sorry I wasn; able to reply earlier. I was running into an issue with my login. I did solve the concern just like you did                

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 1315 views
  • 0 likes
  • 4 in conversation