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