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-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
  • 9 replies
  • 905 views
  • 3 likes
  • 4 in conversation