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

I have a following sample dataset and looking to check iteratively for each ID if the combinations on Var2 (eg: CC85 & CC86) or (cc54 & CC57) are present. Each ID can have one or more (than 5) observations. If those combinations are present then create a new variable called 'combo' and assign values like 'Hcc801' and  'Hcc501' respectively.
No combo value for a single ID or if the conditions are not satisfied then the combo = 'NA'.

Please note, there are many other combinations along with CC85 & CC86 or CC54 & CC57 that need to be incorporated.

 

data t1;
input ID Var1 $ Var2 $ Rank;
datalines;
1 N179 CC135 2
2 I214 CC85 1
2 A419 CC86 1
2 A500 CC100 3
3 A100 CC54 1
3 A101 CC57
;
run;

Thank you in advance for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data t1;
input ID Var1 $ Var2 $ Rank;
datalines;
1 N179 CC135 2
2 I214 CC85 1
2 A419 CC86 1
2 A500 CC100 3
3 A100 CC54 1
3 A101 CC57 2
;

proc sql;
create table want as
select *,
case when sum(upcase(var2)='CC85') and  sum(upcase(var2)='CC86') and upcase(var2) in ('CC85' 'CC86') then 'HCC801'
     when sum(upcase(var2)='CC54') and  sum(upcase(var2)='CC57') and upcase(var2) in ('CC54' 'CC57') then 'HCC501'
	 else 'NA'
end as Combo
 from t1
  group by id;
quit;

Ksharp_0-1714620164946.png

 

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Please show us the desired output table.

--
Paige Miller
SAS_nova
Fluorite | Level 6

output should look like below:

  
IDVar1Var2RankCombo
1N179CC1352NA
2I214CC851HCC801
2A419CC861HCC801
2A500CC1003NA
3A100CC541HCC501
3A101CC572HCC501
Ksharp
Super User
data t1;
input ID Var1 $ Var2 $ Rank;
datalines;
1 N179 CC135 2
2 I214 CC85 1
2 A419 CC86 1
2 A500 CC100 3
3 A100 CC54 1
3 A101 CC57 2
;

proc sql;
create table want as
select *,
case when sum(upcase(var2)='CC85') and  sum(upcase(var2)='CC86') and upcase(var2) in ('CC85' 'CC86') then 'HCC801'
     when sum(upcase(var2)='CC54') and  sum(upcase(var2)='CC57') and upcase(var2) in ('CC54' 'CC57') then 'HCC501'
	 else 'NA'
end as Combo
 from t1
  group by id;
quit;

Ksharp_0-1714620164946.png

 

SAS_nova
Fluorite | Level 6
Thank you! I see those grouping makes the entire thing different!

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
  • 4 replies
  • 564 views
  • 1 like
  • 3 in conversation