ID | Type | Has_A1_in_Type |
1 | A1 A3 | A1 |
2 | A1 A3 | A1 |
3 | A1 A3 | A1 |
4 | A1 A3 | A1 |
5 | A1 A3 | A1 |
6 | A1 A3 | A1 |
7 | ||
8 | A3 | |
9 | A1 A3 | A1 |
10 | A3 | |
11 | A1 A3 | A1 |
12 | A3 A1 | A1 |
13 | A2 | |
14 | A3 | |
15 | A1 | A1 |
In this table I want to create a third column which will be A1 if 2nd column has A1 anywhere or else blank . Any one has any idea how to do this? I used index function but did not work.
Thank you.
data have;
infile cards truncover;
input ID Type & $;
cards;
1 A1 A3 A1
2 A1 A3 A1
3 A1 A3 A1
4 A1 A3 A1
5 A1 A3 A1
6 A1 A3 A1
7
8 A3
9 A1 A3 A1
10 A3
11 A1 A3 A1
12 A3 A1 A1
13 A2
14 A3
15 A1 A1
;
data want;
set have;
Has_A1_in_Type=ifc(findw(type,'A1')>0,'A1',' ');
run;
In a data step
if index(type,'A1')> 0 then Has_A1_in_type='A1';
However I might suggest if you are looking to count the number of times A1 occurs that instead of a character value of A1 that you use a numeric value such as
Has_A1= index(type,'A1')>0;
which will have a value of 0 when not present. Then summing on Has_A1 gives you count, mean a percentage.
And show the code and values that index does not work with. If you have values like A10 then you likely want INDEXW not index.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.