I'm trying to see if any elements within a grouped variable is in a set of values.
Either a SAS or PROC SQL solution will work.
Here's a sample dataset:
data input;/*table name*/
length ID 3 var1 $8;/*define char length*/
input ID var1 $;/*$ signifies char*/
datalines;/*start data*/
1 A
1 A
1 B
2 A
2 A
2 A
;/*end data*/
run;
ID var1
1 A
1 A
1 B
2 A
2 A
2 A
I want to see if any of the values in var1
within each group ID
equal "B" or "C". If one of them is either "B" or "C", then I want the output table to equal 1, else 0. In this example, because group ID = 1 has a "B" in var1
, the output of var2
would be equal to 1. For group ID = 2, var2
should equal 0.
In R, I would use any()
and this would be fairly straightforward. I know SAS has a similar function in SAS IML, but I'm not sure it can be used the same way. I'm looking for a relatively simple way to do this that avoids the use of do
loops or subqueries.
Here's the desired output:
data output;/*table name*/
input ID var2;/*$ signifies char*/
datalines;/*start data*/
1 1
2 0
;/*end data*/
run;
ID var2
1 1
2 0
I've found a few resources that are similar, but haven't found anything that hits directly on this question.
Similar SAS Community Question 1