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

Hi there,

 

Trying to think the best way to do below.. I have multiple records for ID's and each ID has an end state every month ( F or B)

where ID's are always F, i want to tag them as Good. Where always B, I want to tag them as Bad and mixed when it is a mix

 

Would appreciate any and all inputs

 

IDSTATETYPE
1FGOOD
1FGOOD
1FGOOD
2FMIXED
2FMIXED
2FMIXED
2BMIXED
3BBAD
3BBAD
3BBAD
3BBAD
4FGOOD
4FGOOD
4FGOOD
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

SQL is nice here, aggregate functions work on character columns. So if the max is equal to the min then all values are the same. 

 

proc sql;
create table want as 
select *, case when max(state) = min(state) & max(state)= "F" then "Good"
                       when max(state) = min(state) & max(state)= "B" then "Bad"
                       else "MIXED" END as Type
from have
group by ID;
quit;

             

 

EDIT: Fixed an issue with the SQL 

View solution in original post

9 REPLIES 9
Reeza
Super User

SQL is nice here, aggregate functions work on character columns. So if the max is equal to the min then all values are the same. 

 

proc sql;
create table want as 
select *, case when max(state) = min(state) & max(state)= "F" then "Good"
                       when max(state) = min(state) & max(state)= "B" then "Bad"
                       else "MIXED" END as Type
from have
group by ID;
quit;

             

 

EDIT: Fixed an issue with the SQL 

TheNovice
Quartz | Level 8

Thank you so much Reeza and Ksharp, 

 

I combined your solutions because i needed to modify the outcome a bit. I did not know that the sum function could be used this way. 

 

case when max(state) = min(state) & max(state)= "F" then "Good"
when max(state) = min(state) & max(state)= "B" then "Bad"
when sum(state= 'F') > SUM(state= 'B') THEN 'G>B'
when sum(state= 'F') < SUM(state= 'B') THEN 'G<B'
when sum(state= 'F') = SUM(state= 'B') THEN 'G=B'
else '' end as Type

TheNovice
Quartz | Level 8
Hi Ksharp, nope. just a coincidence. Thanks for the reply!
TheNovice
Quartz | Level 8
FYI<, thanks for directing me. your post showed me a different way to use the sum function and i modified the outcome a bit
tarheel13
Rhodochrosite | Level 12
proc sql;
	create table type as 
	
	select ID,count(ID) as count, sum(state='F') as F, sum(state='B') as B,
	case when count(ID) = sum(state='F') then 'GOOD'
	when count(ID)=sum(state='B') then 'BAD'
	else 'MIXED' end as type
		from import1
		group by ID;
		
	select i.*, type
		from import2  as i
		left join type as t
		on t.ID=i.ID;
		
quit;
TheNovice
Quartz | Level 8
Hi Irackley, thank you for the post but it didn't work. Could you explain the import1 and import2 datasets?
tarheel13
Rhodochrosite | Level 12
You didn’t post your data as datalines so I had to import it myself
TheNovice
Quartz | Level 8
Got it and I ran the query and it worked as well. thanks!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 1560 views
  • 3 likes
  • 4 in conversation